gpt4 book ai didi

c# - Form.ShowInTaskBar/Process.MainWindowHandle

转载 作者:可可西里 更新时间:2023-11-01 13:27:15 28 4
gpt4 key购买 nike

当应用程序的主要 Form - 传递给 Application.Run() 的那个 - 有

this.ShowInTaskBar = false;

然后,表示该应用程序的 Process 实例具有 0MainWindowHandle,这意味着 Process.CloseMainWindow() 不起作用。

我该如何解决这个问题?我需要通过 Process 实例彻底关闭 Form

最佳答案

我找到了一种替代方法,即回到 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 ..
}

关于c# - Form.ShowInTaskBar/Process.MainWindowHandle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1006459/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com