Write by :lu0<br>98/2000下,有个公开的函数SetForegroundWindow,用于切换前台窗口.但是事实上,SetForegroundWindow并不能用于和其他进程的窗口协同工作,通常情况下SetForegroundWindow会调用FlashWindowEx来闪烁目标窗口,代表已经切换了窗口,但是这不是我们需要的.网络上有一些顶尖高手使用修改窗口切换的系统规则后,用SetForegroundWindow切换到其他进程的窗口,但是现在,我们有了UNDOCUMENTED的另外一个USER32函数: <br>SwitchToThisWindow(...); <br>来完成这项工作. <br>那么原型是怎么的呢? 下面就来揭晓了...... <br>void WINAPI SwitchToThisWindow ( <br> HWND hWnd, // Handle to the window that should be activated <br> BOOL bRestore // Restore the window if it is minimized <br> ); <br>由于没有原型和库,我们在使用时通常用动态联接法. <br>typedef void (WINAPI *PROCSWITCHTOTHISWINDOW) (HWND, BOOL); <br>PROCSWITCHTOTHISWINDOW SwitchToThisWindow; <br>HMODULE hUser32 = GetModuleHandle("user32"); <br>SwitchToThisWindow = (PROCSWITCHTOTHISWINDOW)GetProcAddress(hUser32, "SwitchToThisWindow"); <br>{ Delphi Code:<br> procedure SwitchToThisWindow(hWnd:HWND;bRestore:Boolean);stdcall;external 'user32.dll';<br>}<br>这样,我们的任务就完成了. <br>*******************************<br>从Win98开始,微软更改了系统代码,一般的SetForegroundWindow只能将<br>状态栏中应用按钮闪烁,并没有将应用调到最前面。请使用下列函数:<br><br>function ForceForegroundWindow(hwnd: THandle): boolean;<br>const<br> SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;<br> SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;<br>var<br> timeout: DWORD;<br>begin<br> if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion<br>> 4)) or<br> ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and<br> ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and<br>(Win32MinorVersion > 0)))) then begin<br> SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout,<br>0);<br> SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,<br>TObject(0), SPIF_SENDCHANGE);<br> Result := SetForegroundWindow(hWnd);<br> SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,<br>TObject(timeout), SPIF_SENDCHANGE);<br> end<br> else<br> Result := SetForegroundWindow(hWnd);<br>end; { ForceForegroundWindow }<br>不过最后SystemParametersInfo恢复参数:<br>SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,Pointer(timeout), SPIF_SENDCHANGE);<br>如果不去掉,在WIN2000下不灵<br>***********************<br>发现一个2000下面的方法,试一下 <br>function AllowSetForegroundWindow( dwProcessId
WORD): BOOL; stdcall;<br><br>implementation<br>function AllowSetForegroundWindow; external 'user32.dll' name 'AllowSetForegroundWindow';<br><br>****************<br>function ForceForegroundWindow(hWnd: THandle): BOOL;<br>var<br> hCurWnd: THandle;<br>begin<br> hCurWnd := GetForegroundWindow;<br> AttachThreadInput(GetWindowThreadProcessId(hCurWnd, nil), GetCurrentThreadId, True);<br> Result := SetForegroundWindow(hWnd);<br> AttachThreadInput(GetWindowThreadProcessId(hCurWnd, nil), GetCurrentThreadId, False);<br>end;<br>*********************<br>procedure ForceForegroundWindow(hwnd: THandle);<br>var<br> hlp: TForm;<br>begin<br> hlp := TForm.Create(nil);<br> try<br> hlp.BorderStyle := bsNone;<br> hlp.SetBounds(0, 0, 1, 1);<br> hlp.FormStyle := fsStayOnTop;<br> hlp.Show;<br> mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);<br> mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);<br> SetForegroundWindow(hwnd);<br> finally<br> hlp.Free;<br> end;<br>end;