Delphi 窗体置前(设置窗体靠前/激活)的几个方法

Delphi 窗体置前(设置窗体靠前/激活)的几个方法

1、方法一:

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
function BringWindowToTopEx(hWnd: HWND): Boolean;
begin
  if IsIconic(hWnd) then
    ShowWindow(hWnd, SW_RESTORE);
  if GetForegroundWindow <> hWnd then
    SetForegroundWindow(hWnd); //enabled
//BringWindowToTop(hWnd);//not enabled
//ForceForegroundWindow(hWnd);//enabled
{SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);//enabled}
//SwitchToThisWindow(hWnd, True);//enabled
  Result := GetForegroundWindow = hWnd;
end;
 
function BringWindowToTopMost(hWnd: HWND; bTopMost: Boolean): Boolean;
begin
  if IsIconic(hWnd) then
    ShowWindow(hWnd, SW_RESTORE);
  if bTopMost then
    SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
  else
    SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end;
 
function BringWindowToTopXY(hWnd: HWND; X, Y: Integer; hWndInsertAfter: HWND): Boolean;
begin
  Result := BringWindowToTopEx(hWnd);
  Result := SetWindowPos(hWnd, hWndInsertAfter, X, Y, 0, 0, SWP_NOSIZE) and Result;
end;

  

2、方法二:

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
// 窗体置顶
procedure SetXwForegroundWindow(AHandle: Thandle);
var
  hFgWin: Thandle;
  hFgThread: Thandle;
begin
 
  ShowWindow(AHandle, SW_NORMAL);
 
  hFgWin := GetForegroundWindow;
  hFgThread := GetWindowThreadProcessID(hFgWin, nil);
 
  if AttachThreadInput(GetCurrentThreadID, hFgThread, true) then
  begin
    SetForegroundWindow(AHandle);
    AttachThreadInput(GetCurrentThreadID, hFgThread, false);
  end
  else
    SetForegroundWindow(AHandle);
 
end;
 
// 激活窗体
function SetSysFocus(AHandle: Thandle): boolean;
var
  hThreadId: Thandle;
  hFgThreadId: Thandle;
begin
  result := false;
  if not IsWindow(AHandle) then
    exit;
  hThreadId := GetWindowThreadProcessID(AHandle, nil);
  hFgThreadId := GetWindowThreadProcessID(GetForegroundWindow, nil);
  if AttachThreadInput(hThreadId, hFgThreadId, true) then
  begin
    SetFocus(AHandle);
    AttachThreadInput(hThreadId, hFgThreadId, false);
  end
  else
    SetFocus(AHandle);
  result := true;
end;

  

3、方法三:

  窗体函数 WinAPI SwitchToThisWindow

  https://www.cnblogs.com/guorongtao/p/13440024.html

 

 

 

创建时间:2020.08.05  更新时间:

 

posted on   滔Roy  阅读(1821)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
历史上的今天:
2019-08-05 [原创] delphi KeyUp、KeyPress、Keydown区别和用法,如何不按键盘调用事件
2019-08-05 获取Delphi焦点位置的方法,及所在的控件、以及如何通过控件名称访问控件并赋值

导航

点击右上角即可分享
微信分享提示