The correct usage of SetForegroundWindow

Source: Internet
Author: User

Call the following code earlier (such as in the main function) before SetForegroundWindow:

    1. DWORD dwtimeout =-1;
    2. SystemParametersInfo (spi_getforegroundlocktimeout, 0, (lpvoid) &dwtimeout, 0);
    3. if (dwtimeout >= 100) {
    4. SystemParametersInfo (spi_setforegroundlocktimeout, 0, (lpvoid) 0, Spif_sendchange |  Spif_updateinifile);
    5. }

Yes, the consequence is that if you execute the statement inside the IF, the API call itself is stuck for two or three seconds because the user profile changes the write-back and the Wm_settingchange broadcast.

 

msdn:http://msdn.microsoft.com/en-us/library/ms633539 (v=vs.85). aspx below is Google Translate, some of the statements are not very fluent.

SetForegroundWindowThe function creates the specified window and activates the thread to the foreground window. Keyboard input window, and change different visual cues for the user. The system allocates a slightly higher foreground window than other threads created by it.
Grammar
BOOL SetForegroundWindow (
HWNDHWND);
Parameters
HWND
[in] should be activated and take the window handle to the foreground.
return value
If the window is brought to the foreground, the return value is nonzero.
If the window does not take to the foreground, the return value is zero.
Notes
Windows 98/meof theThe system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
This process is the foreground process.
This process is initiated by the foreground process.
The last input event received during the process.
There is no foreground process.
The foreground process is being debugged.
The foreground is not locked (see Locksetforegroundwindow).
The foreground lock timeout has expired (see SPI_GETFOREGROUNDLOCKTIMEOUTSYSTEMPARAMETERSINFO).
Windows 2000/xpof:No menu is active.
With this change, the application cannot force a window to the foreground, while the user is working on another window. Instead, the foreground and background Windows Activation window (see SetActiveWindow) and the call function inform the user. However, in Microsoft Windows 98 and Windows Millennium Edition (Windows Me), if a nonforeground threadcalledSetForegroundWindow, and the window does not flash on the taskbar through a handle to a window that is not created by the calling thread.SetForegroundWindowThe behavior is the same as it does when you install the application in Windows 95 and Microsoft Windows NT 4.0, when you change the foreground color lock time-out value. This can be set from the following function call or install the application:

SystemParametersInfo (spi_setforegroundlocktimeout, 0, (LPVOID) 0, Spif_sendwininichange | Spif_updateinifile);

This approach allows Windows 98/windows me and Windows 2000/windows XPSetForegroundWindow to behave the same as Windows 95 and Windows NT 4.0, For all applications, respectively. To install the application, this is done by users who are surprised by the behavior of the user is not changed to warn. In Windows 2000 and Windows XP on Windows, the call fails unless the calling thread can change the foreground window, so it must be called from an installation or patch application. For more information, see Foreground and background windows .

A process that can be set up in the foreground window allows another process to invoke the foreground window Allowsetforegroundwindow functionality. Dwprocessid and The specified procedure is lost to the foreground window setting the next time the user generates the ability to enter, unless the input is in the process, or the next process calls Allowsetforegroundwindow, unless the process is specified.
The foreground process can disable the Locksetforegroundwindow function that invokes the SetForegroundWindow call.

Ensure SetForegroundWindow success
Call the following code earlier (such as in the main function) before SetForegroundWindow:

DWORD dwtimeout =-1;  
SystemParametersInfo (spi_getforegroundlocktimeout, 0, (LPVOID) &dwtimeout, 0);
if (dwtimeout >= 100) {
SystemParametersInfo (spi_setforegroundlocktimeout, 0, (LPVOID) 0, Spif_sendchange | Spif_updateinifile);
}





Hforewnd =
Dwcurid =
Dwforeid =





AttachThreadInput (Dwcurid, Dwforeid, FALSE);

First paragraph of code:
The consequence is that if you execute the statement inside the IF, the API call itself is stuck for two or three seconds because of the user profile's change writeback and Wm_settingchange broadcast.
The second piece of code:
To set the current window for a window, there are a few steps to do:
1. Get the window handle FindWindow
2. Toggle keyboard Input Focus AttachThreadInput
3. display window ShowWindow (some windows are minimized/hidden)
4. Change the zorder,setwindowpos of the window to the most, in order not to affect the subsequent window zorder, after the change, and then restore
5. Last SetForegroundWindow

You must see the above code to understand the reason why this function failed.

The correct usage of SetForegroundWindow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.