Skip to content

Instantly share code, notes, and snippets.

@ivanelianto
Created December 17, 2019 07:43
Show Gist options
  • Save ivanelianto/f604f62ac1bd617d41d6ae2fafb67801 to your computer and use it in GitHub Desktop.
Save ivanelianto/f604f62ac1bd617d41d6ae2fafb67801 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace NA_Line_Broadcaster
{
public static class WindowAPIHelper
{
public 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
};
public struct WindowPlacement
{
public int Leng;
public int flags;
public int showCmd;
public Point ptMinPosition;
public Point ptMaxPosition;
public Rectangle rcNormalPosition;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
public static void BringWindowToFront(string windowTitle)
{
IntPtr wdwIntPtr = FindWindow(null, windowTitle);
//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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment