Delphi判断是否有全屏程序

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.

自我改编代码:

 

 

function IsFullScreen: Boolean; forward;
function GetClassNameFX(h: HWND): string; forward;

function GetClassNameFX(h: HWND): string;
var
  ClassName: PChar;
begin
  Result := '';
  GetMem(ClassName, 256);
  try
    GetClassName(h, ClassName, 256);

    Result := string(ClassName);

  finally
    FreeMem(ClassName);
  end;
end;

function IsFullScreen: Boolean;
var
  h, h1, h2, h3: HWND;
  r, r1: TRect;
  rk, rg, r1k, r1g: Integer;
  s: string;
begin

  Result := False;

  h := Windows.GetDesktopWindow();
  h1 := GetForegroundwindow;
  h2 := FindWindow('Progman', nil);
  h3 := FindWindow('WorkerW', nil);

  if (h1 = h2) or (h1 = h3) or (h1 = h) then
    Exit;
  if h3 > 0 then
  begin
    s := GetClassNameFX(h1);
    if (s = 'WorkerW') or (s = 'Progman') then
      Exit;
  end;

  GetWindowRect(h, r);
  rk := r.Right - r.Left;
  rg := r.Bottom - r.Top;

  GetWindowRect(h1, r1);
  r1k := r1.Right - r1.Left;
  r1g := r1.Bottom - r1.Top;

  if (rk = r1k) and (rg = r1g) and (r.BottomRight.X = r1.BottomRight.X) then
    Result := True;
end;

不喜勿喷,这是我自己花了很长时间才想出来的

转载于:https://www.cnblogs.com/yzryc/p/6402148.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值