C# 테크닉 - 프로세스 검색
# 윈도우에서 현재 실행되고 있는 프로그램을 찾아보고 핸들을 이용해서 상태를 변경할 수 있다.
> FindWindow : 윈도우 핸들을 찾는다.
> ShowWindowAsync : 윈도우를 활성화 시킨다.
> SetForegroundWindow : 윈도우를 최상위로 이동 시킨다.
# 소스 코드
using System;
using System.Runtime.InteropServices;
class FindProcess
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
void ActiveProcess(String proc_name)
{
IntPtr hWnd = FindWindow(null, proc_name);
if (hWnd.Equals(IntPtr.Zero) == false)
{
ShowWindowAsync(hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
}
}
}