【问题标题】:Cannot use pinvoke to send WM_CLOSE to a Windows Explorer window无法使用 pinvoke 将 WM_CLOSE 发送到 Windows 资源管理器窗口
【发布时间】:2009-11-09 09:58:43
【问题描述】:

我有一个 C# 应用程序,它使用 SendMessage pinvoke 方法向应用程序外部的各种窗口发送“关闭窗口”消息 (WM_CLOSE / 16)。这很好用,除非所讨论的窗口是 Windows 资源管理器窗口。我没有收到异常,但窗口没有关闭。

这是签名:

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    internal static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

我需要向 Windows 资源管理器窗口发送不同的消息吗?或者另一种方法来实现这一点?

【问题讨论】:

    标签: c# pinvoke


    【解决方案1】:

    另一种解决方案是使用 PostMessage win API 调用而不是 SendMessage,下面是一个对我来说很好的示例(我使用的是 winXP sp3):

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.Dll")]
    public static extern int PostMessage(IntPtr hWnd, UInt32 msg, int wParam, int lParam);
    
    private const UInt32 WM_CLOSE          = 0x0010;
    
    ...
    
        IntPtr hWnd = FindWindow("ExploreWClass", null);
        if (hWnd.ToInt32()!=0) PostMessage(hWnd, WM_CLOSE, 0, 0);
    

    PostMessage 和 SendMessage api 调用的区别如下:http://msdn.microsoft.com/en-us/magazine/cc301431.aspx

    【讨论】:

    • 太棒了! DestroyWindow 的绝佳替代品,它仅适用于最初创建目标窗口的同一线程。
    猜你喜欢
    • 2018-07-28
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多