Delphi Base

WindSon

导航

Delphi使程序的窗口出现在最前面并激活

procedure setAppFront();
//使程序的窗口出现在最前面并激活
var
  pt, OldPt, NewPt: TPoint;
begin
  //判断Application是否最小化,而不是主窗口的Handle, 使用Restore来还原
  if IsIconic(Application.Handle) then
    Application.Restore;
 
  SetWindowPos(Application.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE);
  SetWindowPos(Application.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE);
 
  // 保存鼠标位置,模拟点击窗口,然后再还原鼠标位置
  GetCursorPos(OldPt);
  NewPt:=Point(0, 0);
  SetCursorPos(NewPt.X, NewPt.Y);
  mouse_event(MOUSEEVENTF_LEFTDOWN, NewPt.X, NewPt.Y, 0, 0);
  mouse_event(MOUSEEVENTF_LEFTUP, NewPt.X, NewPt.Y, 0, 0);
  SetCursorPos(OldPt.X, OldPt.Y);
end;

 

posted on 2020-03-23 20:09  windsonvip  阅读(768)  评论(1编辑  收藏  举报