使窗口永远在最前面

时间:2010-01-26 12:53:33  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:ndows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs;typeTSysFocus=class(TComponent)publicfunction GetSysFocus:integer;function SetSysFocus(hwnd:integer):integer;end;implementationfunction TSysFocus.GetSysFocus : integer;//取当前活动窗口varhOtherWin,OtherThreadID,...
  正文:

首先准备如下代码:


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) 就可以了 这样窗口永远会在最前面

使窗口永远在最前面》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/12426.html


关键字:

关于《使窗口永远在最前面》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td