Get window state of another process

前端 未结 4 1179
猫巷女王i
猫巷女王i 2020-11-30 11:46

How do I get the window state(maximized, minimized) of another process that\'s running?

I\'d tried by using this:

Process[]         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-30 12:04

    You're using proc.StartInfo, which is incorrect. It does not reflect the runtime window style of the target process. It is just startup info you can set and can then be passed on to the process when it starts up.

    The C# signature is:

    [DllImport("user32.dll", SetLastError=true)]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    

    You need to use p/invoke and call GetWindowLong(hWnd, GWL_STYLE), and pass proc.MainWindowHandle as the hWnd parameter.

    You can check if the window is minimized/maximized by doing something like:

    int style = GetWindowLong(proc.MainWindowHandle,  GWL_STYLE);
    if((style & WS_MAXIMIZE) == WS_MAXIMIZE) 
    {
       //It's maximized
    } 
    else if((style & WS_MINIMIZE) == WS_MINIMIZE) 
    {
      //It's minimized
    }
    

    NOTE: The values for the flags (WS_MINIMIZE, etc), can be found in this page: http://www.pinvoke.net/default.aspx/user32.getwindowlong

    Thanks to Kakashi for pointing our the error in testing the result.

提交回复
热议问题