Skip to main content
shorten text
Source Link
Ivan Yurchenko
  • 3.8k
  • 1
  • 25
  • 35

Here is the workingWorking code using the 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);
}

And then youYou can just use it as simple asby calling the BringWindowToFront() method.

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

Here is the working code using the 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);
}

And then you can just use it as simple as calling the BringWindowToFront() method.

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

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.

Removed redundant empty space and added Code tags where it’s needed
Source Link
Ivan Yurchenko
  • 3.8k
  • 1
  • 25
  • 35

Here is the working code using the FindWindowFindWindow 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);
    }

And then you can just use it as simple as calling the BringWindowToFront()BringWindowToFront() method.

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

Here is the working code using the 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);
    }

And then you can just use it as simple as calling the BringWindowToFront() method.

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

Here is the working code using the 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);
}

And then you can just use it as simple as calling the BringWindowToFront() method.

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

Officially, it is called "window" and "title", change to use "window" and “title“ to make it consistent.
Source Link

Here is the working code using the 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_app_caption_here""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);
    }

And then you can just use it as simple as calling the BringWindowToFront() method.

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

Here is the working code using the 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_app_caption_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);
    }

And then you can just use it as simple as calling the BringWindowToFront() method.

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

Here is the working code using the 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);
    }

And then you can just use it as simple as calling the BringWindowToFront() method.

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

Source Link
Ivan Yurchenko
  • 3.8k
  • 1
  • 25
  • 35
Loading