【问题标题】:What is the best way to make a Delphi Application completely full screen?使 Delphi 应用程序完全全屏的最佳方法是什么?
【发布时间】:2008-08-18 10:56:42
【问题描述】:

让一个delphi应用程序(这里是win32的delphi 2007)完全全屏,去除应用程序边框并覆盖windows任务栏的最佳方法是什么?

我正在寻找类似于按下 F11 时 IE 所做的事情。

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

正如接受的答案中提到的那样

BorderStyle := bsNone; 

是这样做的一部分。奇怪的是,我在使用该行时不断收到E2010 Incompatible types: 'TFormBorderStyle' and 'TBackGroundSymbol' 错误(另一种类型定义了bsNone)。

为了克服这个问题,我不得不使用:

BorderStyle := Forms.bsNone;

【问题讨论】:

    标签: delphi delphi-2007


    【解决方案1】:

    嗯,这一直对我有用。好像简单了点……

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

    【讨论】:

    • 哇,如果我没有自己测试过,我不会相信它。我知道设置WindowsState := wsMaximized 将不会覆盖任务栏(它将只是一个最大化的窗口)。因此,假设删除边框会导致窗口最大化而没有边框并不是不合理的。但是在某个地方(VCL 或 Windows)存在一些未记录的技巧,即删除最大化窗口的边框会导致它调整大小以填充整个屏幕。 (与 Raymond 的文章相反,您将表单显式调整为屏幕尺寸)
    • 此处为 Windows 7(64 位)+ Delphi XE 7:不起作用。 Windows 任务栏仍然显示。在 Windows 8.1 上测试:与 W7 相同 + 可以从右侧拉出的操作栏仍然可用。
    • 编辑:在 Windows 7 上 有效(我忘了删除表单上的 Align := alClient)。但在 Windows 8.1 上,您可以从右侧拉出的操作栏仍然可用。有什么解决办法吗?
    【解决方案2】:

    Google 搜索发现了以下其他方法:

    (虽然我想我会先尝试罗迪的方法)

    Manually fill the screen(来自:关于德尔福)

    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;
    

    The WinAPI way(来自 TeamB 的 Peter 下面)

    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; 
    

    【讨论】:

      【解决方案3】:

      最大化表格和hide the title bar。最大化线是从内存中完成的,但我很确定 WindowState 是您想要的属性。

      还有this的文章,不过我觉得太复杂了。

      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.
      

      【讨论】:

        【解决方案4】:

        把 onShow 事件的形式写成这样的代码:

          WindowState:=wsMaximized;
        

        对于 OnCanResize 这个:

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

        【讨论】:

          【解决方案5】:

          如何在主窗体中约束子窗体,就像它是一个 MDI 应用程序一样。但没有令人头疼的问题! (注意:此页面上的回复帮助我完成了这项工作,所以我在这里发布了我的解决方案)

          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;
          

          【讨论】:

            【解决方案6】:

            就我而言,唯一可行的解​​决方案是:

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

            【讨论】:

              【解决方案7】:

              您需要确保表单位置为 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 上测试并运行。

              【讨论】:

              • 如果你有双显示器怎么办?
              【解决方案8】:

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

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

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

              【讨论】:

                【解决方案9】:

                试试:

                Align = alClient    
                FormStyle = fsStayOnTop
                

                这始终与主监视器对齐;

                【讨论】:

                • 你写的甚至跟代码都不接近,缺少双点,缺少行分隔符,当我需要一分钟的加速时,我又损失了 2 分钟。请写完整有用的代码,或者根本不写。 {code} 对齐 := alClient; FormStyle := fsStayOnTop; {代码}
                猜你喜欢
                • 2010-12-31
                • 2017-03-20
                • 1970-01-01
                • 1970-01-01
                • 2016-08-02
                • 2020-09-22
                • 2010-11-20
                • 1970-01-01
                • 2015-06-02
                相关资源
                最近更新 更多