How to bring a window foreground using c#?

前端 未结 6 1440
遥遥无期
遥遥无期 2020-12-30 13:37

I am trying to bring a window foreground. I am using this code. But its not working. Could someone please help?

ShowWindowAsync(wnd.hWnd, SW_SHOW);

SetForeg         


        
相关标签:
6条回答
  • 2020-12-30 14:02

    You should use SetForegroundWindow. Also it may be interesting for you C# Force Form Focus

    0 讨论(0)
  • 2020-12-30 14:05

    I'll be brief: Form.BringToFront()

    0 讨论(0)
  • 2020-12-30 14:09

    I've used this method before:

        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
    
        Process[] processes = Process.GetProcessesByName("processname");
        SetForegroundWindow(processes[0].MainWindowHandle);
    

    More information: http://pinvoke.net/default.aspx/user32.SetForegroundWindow

    0 讨论(0)
  • 2020-12-30 14:09

    This code restores and set focus to a window:

        [DllImport("User32.dll")]
        static extern int SetForegroundWindow(IntPtr hWnd);
    
        [DllImport("user32.dll")]
        internal static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
        static Int32 WM_SYSCOMMAND = 0x0112;
        static Int32 SC_RESTORE = 0xF120;
    

    And use it like this:

        var proc = Process.GetProcessesByName("YourProgram").FirstOrDefault();
    
        if (proc != null)
        {
            var pointer = proc.MainWindowHandle;
    
            SetForegroundWindow(pointer);
            SendMessage(pointer, WM_SYSCOMMAND, SC_RESTORE, 0);
        }
    
    0 讨论(0)
  • 2020-12-30 14:12

    In order for SetForegroundWindow to work consistently, you have to meet a few criteria. The first is your process that would run the command must be in the foreground. Only foreground process can make another process foreground. In order to make your process foreground first, you have to bring the main window to the front, if it is not. You minimise it first and then SetForegroundWindow it, to make it foreground. Now find the target process and bring it to the front

    The steps are

    • Minimise the current window
    • SetForegroundWindow it
    • Find the target process
    • SetForegroundWindow it

    I've got an example, though it's a slightly different use case.

    0 讨论(0)
  • 2020-12-30 14:18

    As of Windows 7 these features dont behave quite so well. If there is an application such as Excel in front of the application you want to bring to the front then Windows 7 blocks this and flashes the window. You can set a registry timeout setting ForegroundLockTimeout=0 in HKEY_CURRENT_USER\Control Panel\Desktop but these is known as stealing focus. To set the behaviour of how XP "should" behave and will behave in Windows 7 by default you can create/set the value to 0x00030D40 (200000ms). I'd like to know what is the preferred solution for trusted Windows applications. eg. If I trust application B to take focus when I double click something in Application A, and some other app is obscuring the window of Application B.

    0 讨论(0)
提交回复
热议问题