使 Delphi 应用程序完全全屏的最佳方法是什么?

发布于 2024-07-05 14:03:02 字数 464 浏览 9 评论 0原文

使 Delphi 应用程序(Delphi 2007 for Win32)完全全屏、删除应用程序边框并覆盖 Windows 任务栏的最佳方法是什么?

我正在寻找类似于 Internet Explorer (IE) 在按 F11 时执行的操作。

我希望这是用户的运行时选项,而不是我自己的设计时决定。

正如已接受的答案中提到的,

BorderStyle := bsNone; 

这是做到这一点的一部分。 奇怪的是,在使用该行时,我不断收到 E2010 不兼容类型:'TFormBorderStyle' 和 'TBackGroundSymbol' 错误(另一种类型已定义 bsNone)。

为了克服这个问题,我必须使用:

BorderStyle := Forms.bsNone;

What is the best way to make a Delphi application (Delphi 2007 for Win32) go completely full screen, removing the application border and covering the Windows Taskbar?

I am looking for something similar to what Internet Explorer (IE) does when you hit F11.

I wish this to be a run time option for the user not a design time decision by my good self.

As mentioned in the accepted answer

BorderStyle := bsNone; 

was part of the way to do it. Strangely I kept getting an E2010 Incompatible types: 'TFormBorderStyle' and 'TBackGroundSymbol' error when using that line (another type had bsNone defined).

To overcome this I had to use:

BorderStyle := Forms.bsNone;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

清晨说晚安 2024-07-12 14:03:02

嗯,这一直对我有用。 好像简单了一些...

procedure TForm52.Button1Click(Sender: TObject);
begin
  BorderStyle := bsNone;
  WindowState := wsMaximized;
end;

Well, this has always worked for me. Seems a bit simpler...

procedure TForm52.Button1Click(Sender: TObject);
begin
  BorderStyle := bsNone;
  WindowState := wsMaximized;
end;
绮筵 2024-07-12 14:03:02

谷歌搜索发现了以下附加方法:(

尽管我想我会先尝试罗迪的方法)

手动填充屏幕(来自:关于 D​​elphi)

procedure TSomeForm.FormShow(Sender: TObject) ;
var
   r : TRect;
begin
   Borderstyle := bsNone;
   SystemParametersInfo
      (SPI_GETWORKAREA, 0, @r,0) ;
   SetBounds
     (r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top) ;
end;

Roddy 主题的变体

FormStyle := fsStayOnTop;
BorderStyle := bsNone;
Left := 0;
Top := 0;
Width := Screen.Width;
Height := Screen.Height;

WinAPI 方式(来自 TeamB 的 Peter Below)

private  // in form declaration
    Procedure WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
      message WM_GETMINMAXINFO;

Procedure TForm1.WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
  Begin
    inherited;
    With msg.MinMaxInfo^.ptMaxTrackSize Do Begin
      X := GetDeviceCaps( Canvas.handle, HORZRES ) + (Width - ClientWidth);
      Y := GetDeviceCaps( Canvas.handle, VERTRES ) + (Height - ClientHeight
);
    End;
  End;

procedure TForm1.Button2Click(Sender: TObject);
Const
  Rect: TRect = (Left:0; Top:0; Right:0; Bottom:0);
  FullScreen: Boolean = False;
begin
  FullScreen := not FullScreen;  
  If FullScreen Then Begin
    Rect := BoundsRect;
    SetBounds(
      Left - ClientOrigin.X,
      Top - ClientOrigin.Y,
      GetDeviceCaps( Canvas.handle, HORZRES ) + (Width - ClientWidth),
      GetDeviceCaps( Canvas.handle, VERTRES ) + (Height - ClientHeight ));
  //  Label2.caption := IntToStr(GetDeviceCaps( Canvas.handle, VERTRES ));
  End
  Else
    BoundsRect := Rect;
end; 

A Google search turned up the following, additional methods:

(though I think I'd try Roddy's method first)

Manually fill the screen (from: About Delphi)

procedure TSomeForm.FormShow(Sender: TObject) ;
var
   r : TRect;
begin
   Borderstyle := bsNone;
   SystemParametersInfo
      (SPI_GETWORKAREA, 0, @r,0) ;
   SetBounds
     (r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top) ;
end;

Variation on a theme by Roddy

FormStyle := fsStayOnTop;
BorderStyle := bsNone;
Left := 0;
Top := 0;
Width := Screen.Width;
Height := Screen.Height;

The WinAPI way (by Peter Below from TeamB)

private  // in form declaration
    Procedure WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
      message WM_GETMINMAXINFO;

Procedure TForm1.WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
  Begin
    inherited;
    With msg.MinMaxInfo^.ptMaxTrackSize Do Begin
      X := GetDeviceCaps( Canvas.handle, HORZRES ) + (Width - ClientWidth);
      Y := GetDeviceCaps( Canvas.handle, VERTRES ) + (Height - ClientHeight
);
    End;
  End;

procedure TForm1.Button2Click(Sender: TObject);
Const
  Rect: TRect = (Left:0; Top:0; Right:0; Bottom:0);
  FullScreen: Boolean = False;
begin
  FullScreen := not FullScreen;  
  If FullScreen Then Begin
    Rect := BoundsRect;
    SetBounds(
      Left - ClientOrigin.X,
      Top - ClientOrigin.Y,
      GetDeviceCaps( Canvas.handle, HORZRES ) + (Width - ClientWidth),
      GetDeviceCaps( Canvas.handle, VERTRES ) + (Height - ClientHeight ));
  //  Label2.caption := IntToStr(GetDeviceCaps( Canvas.handle, VERTRES ));
  End
  Else
    BoundsRect := Rect;
end; 
迷雾森÷林ヴ 2024-07-12 14:03:02

最大化表单并隐藏标题栏。 最大化线是从内存中完成的,但我很确定 WindowState 是您想要的属性。

还有这篇文章,但这对我来说似乎太复杂了。

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   //maximize the window
   WindowState := wsMaximized;
   //hide the title bar
   SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);
   ClientHeight := Height;
end;

编辑:这是一个完整的示例,带有“全屏”和“恢复”选项。 为了最大程度地清晰起见,我已将不同的部分分解为小程序,因此可以将其大大压缩为几行。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnGoFullScreen: TButton;
    btnNotFullScreen: TButton;
    btnShowTitleBar: TButton;
    btnHideTitleBar: TButton;
    btnQuit: TButton;
    procedure btnGoFullScreenClick(Sender: TObject);
    procedure btnShowTitleBarClick(Sender: TObject);
    procedure btnHideTitleBarClick(Sender: TObject);
    procedure btnNotFullScreenClick(Sender: TObject);
    procedure btnQuitClick(Sender: TObject);
  private
    SavedLeft : integer;
    SavedTop : integer;
    SavedWidth : integer;
    SavedHeight : integer;
    SavedWindowState : TWindowState;
    procedure FullScreen;
    procedure NotFullScreen;
    procedure SavePosition;
    procedure HideTitleBar;
    procedure ShowTitleBar;
    procedure RestorePosition;
    procedure MaximizeWindow;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnQuitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.btnGoFullScreenClick(Sender: TObject);
begin
  FullScreen;
end;

procedure TForm1.btnNotFullScreenClick(Sender: TObject);
begin
  NotFullScreen;
end;

procedure TForm1.btnShowTitleBarClick(Sender: TObject);
begin
  ShowTitleBar;
end;

procedure TForm1.btnHideTitleBarClick(Sender: TObject);
begin
  HideTitleBar;
end;

procedure TForm1.FullScreen;
begin
  SavePosition;
  HideTitleBar;
  MaximizeWindow;
end;

procedure TForm1.HideTitleBar;
begin
  SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);
  ClientHeight := Height;
end;

procedure TForm1.MaximizeWindow;
begin
  WindowState := wsMaximized;
end;

procedure TForm1.NotFullScreen;
begin
  RestorePosition;
  ShowTitleBar;
end;

procedure TForm1.RestorePosition;
begin
  //this proc uses what we saved in "SavePosition"
  WindowState := SavedWindowState;
  Top := SavedTop;
  Left := SavedLeft;
  Width := SavedWidth;
  Height := SavedHeight;
end;

procedure TForm1.SavePosition;
begin
  SavedLeft := Left;
  SavedHeight := Height;
  SavedTop := Top;
  SavedWidth := Width;
  SavedWindowState := WindowState;
end;

procedure TForm1.ShowTitleBar;
begin
  SetWindowLong(Handle,gwl_Style,GetWindowLong(Handle,gwl_Style) or ws_Caption or ws_border);
  Height := Height + GetSystemMetrics(SM_CYCAPTION);
  Refresh;
end;

end.

Maximize the form and hide the title bar. The maximize line is done from memory, but I'm pretty sure WindowState is the property you want.

There's also this article, but that seems too complicated to me.

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   //maximize the window
   WindowState := wsMaximized;
   //hide the title bar
   SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);
   ClientHeight := Height;
end;

Edit: Here's a complete example, with "full screen" and "restore" options. I've broken out the different parts into little procedures for maximum clarity, so this could be greatly compressed into just a few lines.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnGoFullScreen: TButton;
    btnNotFullScreen: TButton;
    btnShowTitleBar: TButton;
    btnHideTitleBar: TButton;
    btnQuit: TButton;
    procedure btnGoFullScreenClick(Sender: TObject);
    procedure btnShowTitleBarClick(Sender: TObject);
    procedure btnHideTitleBarClick(Sender: TObject);
    procedure btnNotFullScreenClick(Sender: TObject);
    procedure btnQuitClick(Sender: TObject);
  private
    SavedLeft : integer;
    SavedTop : integer;
    SavedWidth : integer;
    SavedHeight : integer;
    SavedWindowState : TWindowState;
    procedure FullScreen;
    procedure NotFullScreen;
    procedure SavePosition;
    procedure HideTitleBar;
    procedure ShowTitleBar;
    procedure RestorePosition;
    procedure MaximizeWindow;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnQuitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.btnGoFullScreenClick(Sender: TObject);
begin
  FullScreen;
end;

procedure TForm1.btnNotFullScreenClick(Sender: TObject);
begin
  NotFullScreen;
end;

procedure TForm1.btnShowTitleBarClick(Sender: TObject);
begin
  ShowTitleBar;
end;

procedure TForm1.btnHideTitleBarClick(Sender: TObject);
begin
  HideTitleBar;
end;

procedure TForm1.FullScreen;
begin
  SavePosition;
  HideTitleBar;
  MaximizeWindow;
end;

procedure TForm1.HideTitleBar;
begin
  SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);
  ClientHeight := Height;
end;

procedure TForm1.MaximizeWindow;
begin
  WindowState := wsMaximized;
end;

procedure TForm1.NotFullScreen;
begin
  RestorePosition;
  ShowTitleBar;
end;

procedure TForm1.RestorePosition;
begin
  //this proc uses what we saved in "SavePosition"
  WindowState := SavedWindowState;
  Top := SavedTop;
  Left := SavedLeft;
  Width := SavedWidth;
  Height := SavedHeight;
end;

procedure TForm1.SavePosition;
begin
  SavedLeft := Left;
  SavedHeight := Height;
  SavedTop := Top;
  SavedWidth := Width;
  SavedWindowState := WindowState;
end;

procedure TForm1.ShowTitleBar;
begin
  SetWindowLong(Handle,gwl_Style,GetWindowLong(Handle,gwl_Style) or ws_Caption or ws_border);
  Height := Height + GetSystemMetrics(SM_CYCAPTION);
  Refresh;
end;

end.
听风吹 2024-07-12 14:03:02

将这样的代码放入表单 onShow 事件中:

  WindowState:=wsMaximized;

并将以下代码放入 OnCanResize 中:

  if (newwidth<width) and (newheight<height) then
    Resize:=false;

Put to the form onShow event such code:

  WindowState:=wsMaximized;

And to the OnCanResize this:

  if (newwidth<width) and (newheight<height) then
    Resize:=false;
腹黑女流氓 2024-07-12 14:03:02

如何像 MDI 应用程序一样约束 Mainform 中的子表单,但又不会令人头疼! (注意:此页面上的回复帮助我完成了这项工作,所以这就是我在这里发布我的解决方案的原因)

private
{ Private declarations }
  StickyAt: Word;
  procedure WMWINDOWPOSCHANGING(Var Msg: TWMWINDOWPOSCHANGING); Message M_WINDOWPOSCHANGING;
  Procedure WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;

稍后......

    procedure TForm2.WMWINDOWPOSCHANGING(var Msg: TWMWINDOWPOSCHANGING);
    var
      A, B: Integer;
      iFrameSize: Integer;
      iCaptionHeight: Integer;
      iMenuHeight: Integer;
    begin

      iFrameSize := GetSystemMetrics(SM_CYFIXEDFRAME);
      iCaptionHeight := GetSystemMetrics(SM_CYCAPTION);
      iMenuHeight := GetSystemMetrics(SM_CYMENU);

      // inside the Mainform client area
      A := Application.MainForm.Left + iFrameSize;
      B := Application.MainForm.Top + iFrameSize + iCaptionHeight + iMenuHeight;

      with Msg.WindowPos^ do
      begin

        if x <= A + StickyAt then
          x := A;

        if x + cx >= A + Application.MainForm.ClientWidth - StickyAt then
          x := (A + Application.MainForm.ClientWidth) - cx + 1;

       if y <= B + StickyAt then
         y := B;

       if y + cy >= B + Application.MainForm.ClientHeight - StickyAt then
         y := (B + Application.MainForm.ClientHeight) - cy + 1;

      end;
end;

还有更多......

Procedure TForm2.WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
var
  iFrameSize: Integer;
  iCaptionHeight: Integer;
  iMenuHeight: Integer;
Begin
  inherited;
  iFrameSize := GetSystemMetrics(SM_CYFIXEDFRAME);
  iCaptionHeight := GetSystemMetrics(SM_CYCAPTION);
  iMenuHeight := GetSystemMetrics(SM_CYMENU);
  With msg.MinMaxInfo^.ptMaxPosition Do
  begin
    // position of top when maximised
    X := Application.MainForm.Left + iFrameSize + 1;
    Y := Application.MainForm.Top + iFrameSize + iCaptionHeight + iMenuHeight + 1;
  end;
  With msg.MinMaxInfo^.ptMaxSize Do
  Begin
     // width and height when maximized
     X := Application.MainForm.ClientWidth;
     Y := Application.MainForm.ClientHeight;
  End;
  With msg.MinMaxInfo^.ptMaxTrackSize Do
  Begin
     // maximum size when maximised
     X := Application.MainForm.ClientWidth;
     Y := Application.MainForm.ClientHeight;
  End;
  // to do: minimum size (maybe)
End;

How to constrain a sub-form within the Mainform like it was an MDI app., but without the headaches! (Note: The replies on this page helped me get this working, so that's why I posted my solution here)

private
{ Private declarations }
  StickyAt: Word;
  procedure WMWINDOWPOSCHANGING(Var Msg: TWMWINDOWPOSCHANGING); Message M_WINDOWPOSCHANGING;
  Procedure WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;

later...

    procedure TForm2.WMWINDOWPOSCHANGING(var Msg: TWMWINDOWPOSCHANGING);
    var
      A, B: Integer;
      iFrameSize: Integer;
      iCaptionHeight: Integer;
      iMenuHeight: Integer;
    begin

      iFrameSize := GetSystemMetrics(SM_CYFIXEDFRAME);
      iCaptionHeight := GetSystemMetrics(SM_CYCAPTION);
      iMenuHeight := GetSystemMetrics(SM_CYMENU);

      // inside the Mainform client area
      A := Application.MainForm.Left + iFrameSize;
      B := Application.MainForm.Top + iFrameSize + iCaptionHeight + iMenuHeight;

      with Msg.WindowPos^ do
      begin

        if x <= A + StickyAt then
          x := A;

        if x + cx >= A + Application.MainForm.ClientWidth - StickyAt then
          x := (A + Application.MainForm.ClientWidth) - cx + 1;

       if y <= B + StickyAt then
         y := B;

       if y + cy >= B + Application.MainForm.ClientHeight - StickyAt then
         y := (B + Application.MainForm.ClientHeight) - cy + 1;

      end;
end;

and yet more...

Procedure TForm2.WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
var
  iFrameSize: Integer;
  iCaptionHeight: Integer;
  iMenuHeight: Integer;
Begin
  inherited;
  iFrameSize := GetSystemMetrics(SM_CYFIXEDFRAME);
  iCaptionHeight := GetSystemMetrics(SM_CYCAPTION);
  iMenuHeight := GetSystemMetrics(SM_CYMENU);
  With msg.MinMaxInfo^.ptMaxPosition Do
  begin
    // position of top when maximised
    X := Application.MainForm.Left + iFrameSize + 1;
    Y := Application.MainForm.Top + iFrameSize + iCaptionHeight + iMenuHeight + 1;
  end;
  With msg.MinMaxInfo^.ptMaxSize Do
  Begin
     // width and height when maximized
     X := Application.MainForm.ClientWidth;
     Y := Application.MainForm.ClientHeight;
  End;
  With msg.MinMaxInfo^.ptMaxTrackSize Do
  Begin
     // maximum size when maximised
     X := Application.MainForm.ClientWidth;
     Y := Application.MainForm.ClientHeight;
  End;
  // to do: minimum size (maybe)
End;
半﹌身腐败 2024-07-12 14:03:02

就我而言,唯一有效的解决方案是:

procedure TFormHelper.FullScreenMode;
begin
  BorderStyle := bsNone;
  ShowWindowAsync(Handle, SW_MAXIMIZE);
end;

In my case, the only working solution is:

procedure TFormHelper.FullScreenMode;
begin
  BorderStyle := bsNone;
  ShowWindowAsync(Handle, SW_MAXIMIZE);
end;
冰雪梦之恋 2024-07-12 14:03:02

您需要确保表单位置为 poDefaultPosOnly。

Form1.Position := poDefaultPosOnly;
Form1.FormStyle := fsStayOnTop;
Form1.BorderStyle := bsNone;
Form1.Left := 0;
Form1.Top := 0;
Form1.Width := Screen.Width;
Form1.Height := Screen.Height;

已在 Win7 x64 上测试并运行。

You need to make sure Form position is poDefaultPosOnly.

Form1.Position := poDefaultPosOnly;
Form1.FormStyle := fsStayOnTop;
Form1.BorderStyle := bsNone;
Form1.Left := 0;
Form1.Top := 0;
Form1.Width := Screen.Width;
Form1.Height := Screen.Height;

Tested and works on Win7 x64.

木森分化 2024-07-12 14:03:02

尝试:

Align = alClient    
FormStyle = fsStayOnTop

这始终与主显示器对齐;

Try:

Align = alClient    
FormStyle = fsStayOnTop

This always align to the primary monitor;

神经大条 2024-07-12 14:03:02

嗯。 看着这些回复,我似乎记得大约 8 年前我编写游戏代码时处理过这个问题。 为了使调试更容易,我使用普通的 Delphi 表单的设备上下文作为全屏显示的源。

重点是,DirectX 能够全屏运行任何设备上下文 - 包括由您的表单分配的设备上下文。

因此,要为应用程序提供“真正的”全屏功能,请查找 Delphi 的 DirectX 库,它可能包含您开箱即用所需的内容。

Hm. Looking at the responses I seem to remember dealing with this about 8 years ago when I coded a game. To make debugging easier, I used the device-context of a normal, Delphi form as the source for a fullscreen display.

The point being, that DirectX is capable of running any device context fullscreen - including the one allocated by your form.

So to give an app "true" fullscreen capabilities, track down a DirectX library for Delphi and it will probably contain what you need out of the box.

浅唱ヾ落雨殇 2024-07-12 14:03:02

VCL

这里有两个按钮单击事件,用于进入全屏和退出全屏:

procedure TForm1.GoFullScreen(Sender: TObject);
begin
  BorderStyle := bsNone;
  WindowState := wsMaximized;
end;

procedure TForm1.ExitFullScreen(Sender: TObject);
begin
  BorderStyle := bsSizable;
  WindowState := wsNormal;
end;

FMX

对于那些希望在 FireMonkey (FMX) 中执行相同操作的人来说。 更改 BorderStyleWindowState 的 VCL 方法在 FMX 中会起作用,但正确的方法是使用窗体上的 FullScreen 属性机动MX:

procedure TForm1.GoFullScreen(Sender: TObject);
begin
  FullScreen := True;
end;

procedure TForm1.ExitFullScreen(Sender: TObject);
begin
  FullScreen := False;
end;

VCL

Here are two button click events to go into FullScreen and to exit FullScreen:

procedure TForm1.GoFullScreen(Sender: TObject);
begin
  BorderStyle := bsNone;
  WindowState := wsMaximized;
end;

procedure TForm1.ExitFullScreen(Sender: TObject);
begin
  BorderStyle := bsSizable;
  WindowState := wsNormal;
end;

FMX

And for those finding themselves here looking to do the same in FireMonkey (FMX). The VCL method of changing BorderStyle and WindowState will kind of work in FMX, but the correct way is to use the FullScreen property on the form in FMX:

procedure TForm1.GoFullScreen(Sender: TObject);
begin
  FullScreen := True;
end;

procedure TForm1.ExitFullScreen(Sender: TObject);
begin
  FullScreen := False;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文