嘿嘿,正好我今天看到,贴给你。<br><br>unit SysFocus;<br><br>interface<br>uses<br> Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs;<br>type<br> TSysFocus=class(TComponent)<br> public<br> function GetSysFocus:integer;<br> function SetSysFocus(hwnd:integer):integer;<br> end;<br><br>implementation<br><br>function TSysFoucs.GetSysFocus;//取当前活动窗口<br>var<br> hOtherWin,OtherThreadID,hFocusWin:integer;<br>begin<br> hOtherWin:=GetForegroundWindow;<br> OtherThreadID:=GetWindowThreadProcessID(hOtherWin,nil);<br> if AttachThreadInput(GetcurrentThreadID,OtherThreadID,True) then<br> begin<br> hFocusWin:=GetFocus;<br> result:=GetFocus;<br> if HFocusWin<>0 then<br> try<br> //SendMessage(GetFocus,WM_COPY,0,0);//书上是这么写的<br> finally<br> AttachThreadInput(GetcurrentThreadID,OtherThreadID,False);<br> end;<br> end<br> else result:=GetFocus;<br>end;<br><br>function TSysFocus.SetSysFocus(hwnd:integer):integer;//设置某窗口为活动窗口<br>var<br> hOtherWin,OtherTHreadID,hFocusWin:integer;<br>begin<br> hOtherWin:=GetForegroundWindow;<br> OtherThreadID:=GetWindowThreadProcessID(hOtherWin,nil);<br> if AttachThreadInput(GetcurrentThreadID,OtherThreadID,True) then<br> begin<br> hFocusWin:=GetFocus;<br> SetFocus(hwnd);<br> //SendMessage(hwnd,WM_COPY,0,0);<br> if hFocusWin<>0 then<br> try<br> //SendMessage(GetFocus,WM_COPY,0,0);<br> finally<br> AttachThreadInput(GetCurrentTHreadID,OtherTHreadID,False);<br> end;<br> end<br> else result:=SetFocus(hwnd);<br>end;<br><br>end.<br>