简体   繁体   English

在C#中使用WM_Close

[英]Using WM_Close in c#

I am using the below code to close the window, by searching the window name in taskbar. 我正在使用以下代码通过在任务栏中搜索窗口名称来关闭窗口。 But i one case, my window will not appear in the taskbar. 但在一种情况下,我的窗口将不会出现在任务栏中。 In that case, WM_Close could not close the window. 在这种情况下, WM_Close无法关闭窗口。 Whats the other way to do it using WM_Close ??? 使用WM_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;
    }

Now using the below code...But getting error in 现在使用以下代码...但是出现错误

"IntPtr hWnd = PostMessage(IntPtr.Zero, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);" “ IntPtr hWnd = PostMessage(IntPtr.Zero,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);”

where to provide the window name in order to close that ??? 在哪里提供窗口名称以关闭该窗口?


 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;
    }

Edit: Sorry misread your question. 编辑:很抱歉误读了您的问题。

Use FindWindow/FindWindowEx instead. 请改用FindWindow / FindWindowEx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM