Form.ShowInTaskBar / Process.MainWindowHandle
質問
アプリケーションのメインの Form
- Application.Run()
に渡されたもの-がある場合
this.ShowInTaskBar = false;
その後、そのアプリケーションを表す Process
のインスタンスは 0
の MainWindowHandle
を持ちます。つまり、 Process.CloseMainWindow()
は機能しません。
どうすればこれを回避できますか? Process
インスタンスを介して Form
をきれいに閉じる必要があります。
解決
Win32に戻り、ウィンドウタイトルを使用して、別の方法を見つけました。面倒ですが、私の状況では機能します。
この例には、1つのアプリケーションインスタンスのコンテキストメニューがあり、そのアプリケーションのすべてのインスタンスが閉じています。
[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 ..
}
所属していません StackOverflow