FindWindow and SetForegroundWindow alternatives?

后端 未结 4 1766
忘掉有多难
忘掉有多难 2020-12-01 11:14

I am searching for alternatives to the old User32.dll version of switching to a different application with FindWindow() and SetForegroundWind

相关标签:
4条回答
  • 2020-12-01 11:28

    You can use System.Diagnostics.Process Object for a FindWindow equivalent. There currently is no equivalent for SetForegroundWindow. You will want use Pinvoke with SetForgroundWindow.

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    
    0 讨论(0)
  • 2020-12-01 11:33

    You could use SetActiveWindow as an alternative to SetForeGroundWindow. I'd say you should go through all the Windows Manipulation Api Functions and see if there's something you're missing out.

    Also, note that you can obtain the handle of the System.Diagnostics.Process object via the Process.Handle property.

    0 讨论(0)
  • 2020-12-01 11:35

    Answer: No.

    But, to help the next wonderer looking to find a window and activate it from C# here's what you have to do:

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    
    void ActivateApp(string processName)
    {
        Process[] p = Process.GetProcessesByName(processName);
    
        // Activate the first application we find with this name
        if (p.Count() > 0)
            SetForegroundWindow(p[0].MainWindowHandle);
    }
    

    To bring notepad to the front, for example, you would call:

    ActivateApp("notepad");
    

    As a side note - for those of you who are trying to bring a window within your application to the foreground just call the Activate() method.

    0 讨论(0)
  • 2020-12-01 11:45

    An alternative to SetForeGroundWindow is VisualBasic's AppActivate

    Call it like this

    Microsoft.VisualBasic.Interaction.AppActivate("WindowTitle")
    

    Just because it is in the VisualBasic namespace doesn't mean you can't use it in C#.

    Full Documentation here

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