Bring another processes Window to foreground when it has ShowInTaskbar = false

前端 未结 3 2061
有刺的猬
有刺的猬 2020-11-29 01:26

We only want one instance of our app running at any one time. So on start up it looks to see if the app is running and if it is, it calls SetForegroundWindow

3条回答
  •  甜味超标
    2020-11-29 01:47

    Well, code is here. Even if the ShowInTaskBar is false, you should be able to bring it to the front.

        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    
        public static void bringToFront(string title) {
            // Get a handle to the Calculator application.
            IntPtr handle = FindWindow(null, title);
    
            // Verify that Calculator is a running process.
            if (handle == IntPtr.Zero) {
                return;
            }
    
            // Make Calculator the foreground application
            SetForegroundWindow(handle);
        }
    

    Note: you should FindWindow using the form's class and not by name as the splash screen forms sometimes do not have titles or even the controlbox. Use Spy++ to dig deeper.

    Use FindWindow on splash. I think this is what you want to do - bring the splash screen in front while loading of the main form.

提交回复
热议问题