Windows 7: how to bring a window to the front no matter what other window has focus?

后端 未结 9 1755
走了就别回头了
走了就别回头了 2020-11-28 08:04

I\'m implementing a task-bar replacement, dock-like application-switcher style program. It\'s doing some unique stuff with OpenGL, and with keyboard shortcuts, so the way it

9条回答
  •  [愿得一人]
    2020-11-28 08:41

    I've had some code that's been running for years, going all the way back to Windows 95. When double clicking the applications system tray icon I always used Win32 API functions such as BringWindowToTop and SetForegroundWindow to bring my application windows to the foreground. This all stopped working as intended on Windows 7, where my input window would end up behind other windows and the window icon would flash on the status bar. The 'work around' that I came up with was this; and it seems to work on all versions of Windows.

    //-- show the window as you normally would, and bring window to foreground.
    //   for example;
    ::ShowWindow(hWnd,SW_SHOW); 
    ::BringWindowToTop(hWnd);
    ::SetForegroundWindow(hWnd);
    
    //-- on Windows 7, this workaround brings window to top
    ::SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
    ::SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
    ::SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,0,0,SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
    

提交回复
热议问题