Delphi中如何判断是否有程序全屏
分类:Components, Delphi, System
阅读 (3,032)
Add comments
12月 312014
方法一:
定时调用GetForegroundWindow获取最前端的窗口句柄,然后判断该窗口宽度高度是否等于屏幕的宽度高度。代码如下,大家参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
procedure TForm1.Timer1Timer(Sender: TObject); var topWindow: THandle; wRect: TRect; begin topWindow := GetForegroundWindow; GetWindowRect(topWindow, wRect); if (wRect.Bottom = Screen.Height) and (wRect.Right = Screen.Width) then begin Memo1.Lines.Add('有全屏程序运行'); //做你爱做的事…… end else begin Memo1.Lines.Add('无全屏程序运行'); //做你爱做的事…… end; end; |
这种方法真是弱啊弱,看看第二种方法吧
方法二:
第二种方法是通过SHAppBarMessage函数给系统发送一个监听消息,用于监听一些关于AppBar的信息,该消息的第二个参数里有一个属性是用于指定回调消息的,程序中定义一个消息码用于监听。当传回的消息的wParam是ABN_FULLSCREENAPP 时判断lParam为真假来判断是进入全屏还是退出全屏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ShellAPI; // 要引用此单元 const WM_APPBAR_MESSAGE = WM_USER + 1; type TForm1 = class(TForm) Timer1: TTimer; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public IsFullScreenAppRun: Boolean; //放个全局变量用于记录 procedure WMAppBarMessage(var Msg: TMessage); message WM_APPBAR_MESSAGE; end; var Form1: TForm1; AppBar_Data: APPBARDATA; implementation {$R *.dfm} procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin SHAppBarMessage(ABM_REMOVE, AppBar_Data); //窗口关闭时移除此消息 end; procedure TForm1.FormCreate(Sender: TObject); begin FillChar(AppBar_Data, SizeOf(AppBar_Data), #0); AppBar_Data.cbSize := SizeOf(AppBar_Data); AppBar_Data.hWnd := Handle; AppBar_Data.uCallbackMessage := WM_APPBAR_MESSAGE; //指定回调消息 SHAppBarMessage(ABM_NEW, AppBar_Data); //建立监听 end; procedure TForm1.WMAppBarMessage(var Msg: TMessage); var retCode: Cardinal ; begin if Msg.Msg = WM_APPBAR_MESSAGE then begin if msg.WParam = ABN_FULLSCREENAPP then begin if msg.LParam = 1 then begin Memo1.Lines.Add('有全屏程序运行'); IsFullScreenAppRun := True; end else if Msg.LParam = 0 then begin Memo1.Lines.Add('无全屏程序运行'); IsFullScreenAppRun := False; end; end; end; end; end. |