문제

응용 프로그램이 메인 일 때 Form - 하나가지나 갔다 Application.Run() - 가지다

this.ShowInTaskBar = false;

그런 다음 인스턴스입니다 Process 그 응용 프로그램은 MainWindowHandle0, 그 의미 Process.CloseMainWindow() 작동하지 않습니다.

이 문제를 어떻게 해결할 수 있습니까? 깨끗하게 닫아야합니다 Form TH를 통해 Process 사례.

도움이 되었습니까?

해결책

Win32 물건으로 돌아가서 창 제목을 사용하여 대안적인 방법을 찾았습니다. 지저분하지만 내 상황에서 작동합니다.

예제에는 해당 응용 프로그램의 모든 인스턴스를 닫는 하나의 응용 프로그램 인스턴스의 컨텍스트 메뉴가 있습니다.

[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsCallback x, int y);
public delegate bool EnumWindowsCallback(int hwnd, int lParam);
[DllImport("user32.dll")]
public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
private void ContextMenu_Quit_All(object sender, EventArgs ea)
{
    EnumWindowsCallback itemHandler = (hwnd, lParam) =>
    {
        StringBuilder sb = new StringBuilder(1024);
        GetWindowText(hwnd, sb, sb.Capacity);

        if ((sb.ToString() == MainWindow.APP_WINDOW_TITLE) &&
            (hwnd != mainWindow.Handle.ToInt32())) // Don't close self yet
        {
            PostMessage(new IntPtr(hwnd), /*WM_CLOSE*/0x0010, 0, 0);
        }

        // Continue enumerating windows. There may be more instances to close.
        return true;
    };

    EnumWindows(itemHandler, 0);
    // Close self ..
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top