예제코드
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
public void CloseWindowByTitleText(string titleTxt)
{
try
{
if (titleTxt.Length > 0)
{
IntPtr hWnd = FindWindow(null, titleTxt);
if (hWnd != IntPtr.Zero)
{
SetForegroundWindow(hWnd); // 윈도우 활성화
SendMessage(hWnd, 0x0010, 0, 0); // 종료 메시지 보냄
// SendKeys.Send("{ENTER}"); // 엔터키를 누름
// SendMessage(hWnd, 0x102, (int)Keys.Enter, 0); // 엔터키 메시지를 보냄
}
}
}
catch (Exception err)
{
Console.WriteLine(err.StackTrace);
}
}