C# 在c语言中使用WM_-Close#

C# 在c语言中使用WM_-Close#,c#,C#,我正在使用下面的代码通过在任务栏中搜索窗口名称来关闭窗口。 但有一种情况,我的窗口不会出现在任务栏上。在这种情况下,WM_Close无法关闭窗口。使用WM\u Close void DaemonTerminamtionHook_KeyPressed(object sender, KeyPressedEventArgs e) { DaemonResult = MessageBox.Show("Are you sure, you want to Terminate D

我正在使用下面的代码通过在任务栏中搜索窗口名称来关闭窗口。 但有一种情况,我的窗口不会出现在任务栏上。在这种情况下,
WM_Close
无法关闭窗口。使用
WM\u Close

    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)
        {
            //Free the resources of ShellBasics and terminate Daemon here.
            IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, "DAEMON TAB BAR");
            bool ret = CloseWindow(hWnd);
        }
    }

    //WM_Close
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    static uint WM_CLOSE = 0x10;

    static bool CloseWindow(IntPtr hWnd)
    {
        SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); 
        return true;
    }

现在使用下面的代码…但是在

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

在哪里提供窗口名称以关闭该窗口



编辑:对不起,误读了你的问题


改用

@Preet,谢谢你的支持。你能告诉我如何传递参数:void PostMessageSafe(HandleRef hWnd,uint msg,IntPtr wParam,IntPtr lParam)@Preet,在这里传递窗口名的位置吗?@Preet,请参考顶部的更新代码,它给出了一些错误,因为参数不匹配,请告诉我在哪里提供windows名称以便关闭它。我建议阅读以使用类名设置窗口,以便findwindow可以找到它。或者使用SPY++在dev中查找此值
 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 = PostMessage(IntPtr.Zero, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            bool ret = CloseWindow(hWnd);
        }
    }



    static uint WM_CLOSE = 0x10;
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    static bool CloseWindow(IntPtr hWnd)
    {
        bool returnValue = PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        if (!returnValue)
            throw new Win32Exception(Marshal.GetLastWin32Error());
        return true;
    }