Restore a minimized window of another application

前端 未结 6 1167
情深已故
情深已故 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:52

    Working code using FindWindow method:

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string className, string windowTitle);
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags);
    
    [DllImport("user32.dll")]
    private static extern int SetForegroundWindow(IntPtr hwnd);
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowPlacement(IntPtr hWnd, ref Windowplacement lpwndpl);
    
    private enum ShowWindowEnum
    {
        Hide = 0,
        ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3,
        Maximize = 3, ShowNormalNoActivate = 4, Show = 5,
        Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8,
        Restore = 9, ShowDefault = 10, ForceMinimized = 11
    };
    
    private struct Windowplacement
    {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }
    
    private void BringWindowToFront()
    {
        IntPtr wdwIntPtr = FindWindow(null, "Put_your_window_title_here");
    
        //get the hWnd of the process
        Windowplacement placement = new Windowplacement();
        GetWindowPlacement(wdwIntPtr, ref placement);
    
        // Check if window is minimized
        if (placement.showCmd == 2)
        {
            //the window is hidden so we restore it
            ShowWindow(wdwIntPtr, ShowWindowEnum.Restore);
        }
    
        //set user's focus to the window
        SetForegroundWindow(wdwIntPtr);
    }
    

    You can use it by calling BringWindowToFront().

    I always have one instance of the application running so if you can have several open instances simultaneously you might want to slightly change the logic.

提交回复
热议问题