How to set focus back to form after opening up a process (Notepad)?

前端 未结 5 1998
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 12:01

I open up a notepad from my program using Process.Start() but the new opened notepad covers the screen. But I do want my application to maintain its focus.

5条回答
  •  盖世英雄少女心
    2020-12-06 12:19

    I had the same problem, i eventually wound up with programmatically calling alt-tab:

    [DllImport("user32.dll")]
    static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
    
    private void alttab()
    {
         uint WM_SYSCOMMAND = 0x0112;
         int SC_PREVWINDOW = 0xF050;            
    
         PostMessage(Process.GetCurrentProcess().MainWindowHandle, WM_SYSCOMMAND, SC_PREVWINDOW, 0);
    }
    

    //EDIT: You should use process.MainWindowHandle instead ofcourse

提交回复
热议问题