简体   繁体   English

在C#中使用WM_Close的问题

[英]Problem using WM_Close in c#

I am trying to use the below code to close the window. 我正在尝试使用以下代码关闭窗口。

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 ??? 在哪里提供窗口名称以关闭该窗口? And there is also some problem in the parameters i pass. 我传递的参数中也存在一些问题。


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

After modification of code, but still no luck. 经过修改的代码,但仍然没有运气。 since i am new to windows messaging kinda stuff. 因为我是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;
    }

If I understand correctly what you are trying to do you should first get the window handle of the window you want to close using FindWindow . 如果我正确理解了您要执行的操作,则应该首先使用FindWindow获取要关闭的窗口的窗口句柄。 Your code would look something like this: 您的代码如下所示:

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

Define FindWindow as: FindWindow定义为:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Why are you making nasty hacks, while you can just call Process.CloseMainWindow() ? 为什么只调用Process.CloseMainWindow()为什么要进行讨厌的黑客攻击?

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.closemainwindow.aspx

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

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