C# 在c语言中使用WM_Close的问题#

C# 在c语言中使用WM_Close的问题#,c#,C#,我正在尝试使用下面的代码关闭窗口 但是在这个过程中会出错 IntPtr hWnd=PostMessage(IntPtr.Zero,WM_CLOSE,IntPtr.Zero,IntPtr.Zero) 在哪里提供窗口名称以关闭该窗口??? 我传递的参数也有一些问题 修改代码后,仍然没有运气。因为我不熟悉windows消息传递之类的东西 void DaemonTerminamtionHook_KeyPressed(object sender, KeyPressedEventArgs e)

我正在尝试使用下面的代码关闭窗口

但是在这个过程中会出错

IntPtr hWnd=PostMessage(IntPtr.Zero,WM_CLOSE,IntPtr.Zero,IntPtr.Zero)

在哪里提供窗口名称以关闭该窗口??? 我传递的参数也有一些问题


修改代码后,仍然没有运气。因为我不熟悉windows消息传递之类的东西

    void DaemonTerminamtionHook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        DaemonResult = MessageBox.Show("Are you sure, you want to Terminate Daemon?", "Terminate Daemon", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

        if (DaemonResult == DialogResult.Yes)
        {
            IntPtr hWnd = FindWindow(null, "DAEMON TAB BAR");
            bool ret = CloseWindow(hWnd);
        }
    }

    static bool CloseWindow(IntPtr hWnd)
    {
        //How to call it here
        return true;
    }

如果我正确理解您试图执行的操作,您应该首先使用
FindWindow
获取要关闭的窗口的窗口句柄。您的代码如下所示:

IntPtr hWnd = FindWindow(null, <WindowName>);
bool ret = CloseWindow(hWnd);

你为什么要进行令人讨厌的黑客攻击,而你只需调用
Process.CloseMainWindow()


参数与方法不匹配。您在CloseWindow中的初始代码似乎正常。您可能需要检查FindWindow的返回值是否为IntPtr.Zero。如果是,则找不到要关闭的窗口。否则就用hWnd作为参数调用您原来的CloseWindow。@Peter van der Heijden,我已经按照您的建议进行了更新。但是不知道如何在closewindow()中调用它。在这件事上,金迪可以帮我。谢谢。@请注意,我的窗口不会出现在windows任务栏中
IntPtr hWnd = FindWindow(null, <WindowName>);
bool ret = CloseWindow(hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);