精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>图形界面和窗体>>[转载]如何将一个窗口激活并带到最前面

主题:[转载]如何将一个窗口激活并带到最前面
发信人: delfan( )
整理人: delfan(2001-03-30 22:55:43), 站内信件
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 TSysFoucs.GetSysFocus;//取当前活动窗口
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
      try
        //SendMessage(GetFocus,WM_COPY,0,0);//书上是这么写的
      finally
        AttachThreadInput(GetcurrentThreadID,OtherThreadID,False);
      end;
  end
  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);
    //SendMessage(hwnd,WM_COPY,0,0);
    if hFocusWin<>0 then
      try
        //SendMessage(GetFocus,WM_COPY,0,0);
      finally
        AttachThreadInput(GetCurrentTHreadID,OtherTHreadID,False);
      end;
  end
  else result:=SetFocus(hwnd);
end;

end.


----
失业中......

[关闭][返回]