Restore a minimized window of another application

前端 未结 6 1171
情深已故
情深已故 2020-11-27 17:00

I\'m adding some code to an app that will launch another app if it isn\'t already running, or if it is, bring it to the front. This requires a small amount of interop/WinAPI

6条回答
  •  半阙折子戏
    2020-11-27 17:46

    ... Apparently you cannot trust the information a Process gives you.

    Process.MainWindowHandle returns the window handle of the first window created by the application, which is USUALLY that app's main top-level window. However, in my case, a call to FindWindow() shows that the handle of the actual window I want to restore is not what MainWindowHandle is pointing to. It appears that the window handle from the Process, in this case, is that of the splash screen shown as the program loads the main form.

    If I call ShowWindow on the handle that FindWindow returned, it works perfectly.

    What's even more unusual is that when the window's open, the call to SetForegroundWindow(), when given the process's MainWindowHandle (which should be invalid as that window has closed), works fine. So obviously that handle has SOME validity, just not when the window's minimized.

    In summary, if you find yourself in my predicament, call FindWindow, passing it the known name of your external app's main window, to get the handle you need.

提交回复
热议问题