使窗口永远在最前面

  • A+
所属分类:[开发技巧]

首先准备如下代码:

unit SysFocus;
interface

uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs;

type
TSysFocus=class(TComponent)
public
function GetSysFocus:integer;
function SetSysFocus(hwnd:integer):integer;
end;

implementation

function TSysFocus.GetSysFocus : integer;//取当前活动窗口
var
hOtherWin,OtherThreadID,hFocusWin:integer;
begin
hOtherWin:=GetForegroundWindow;
OtherThreadID:=GetWindowThreadProcessID(hOtherWin,nil);
if AttachThreadInput(GetcurrentThreadID,OtherThreadID,True) then
begin
hFocusWin:=GetFocus;
result:=GetFocus;
if HFocusWin<>0 then
AttachThreadInput(GetcurrentThreadID,OtherThreadID,False)
else result:=GetFocus;
end;

function TSysFocus.SetSysFocus(hwnd:integer):integer;//设置某窗口为活动窗口
var
hOtherWin,OtherTHreadID,hFocusWin:integer;
begin
hOtherWin:=GetForegroundWindow;
OtherThreadID:=GetWindowThreadProcessID(hOtherWin,nil);
if AttachThreadInput(GetcurrentThreadID,OtherThreadID,True) then
begin
hFocusWin:=GetFocus;
SetFocus(hwnd);
if hFocusWin<>0 then
AttachThreadInput(GetCurrentTHreadID,OtherTHreadID,False)
end
else result:=SetFocus(hwnd);
end;

end.

说明:将这个单元加入到工程中,然后调用TSysFocus.SetSysFocus(handle) 就可以了

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: