簡體   English   中英

在C#中使用WM_Close的問題

[英]Problem using WM_Close in c#

我正在嘗試使用以下代碼關閉窗口。

但是出現錯誤

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

在哪里提供窗口名稱以關閉該窗口? 我傳遞的參數中也存在一些問題。


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

經過修改的代碼,但仍然沒有運氣。 因為我是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);

FindWindow定義為:

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

為什么只調用Process.CloseMainWindow()為什么要進行討厭的黑客攻擊?

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