无法使用 pinvoke 将 WM_CLOSE 发送到 Windows 资源管理器窗口

发布于 2024-08-11 00:14:23 字数 401 浏览 3 评论 0原文

我有一个 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 资源管理器窗口发送其他消息吗?或者有替代方法来实现这一点?

I have a C# application that uses the SendMessage pinvoke method to send a "close window" message (WM_CLOSE / 16) to various windows outside the application. This works great, except when the window in question is a Windows Explorer window. I do not get an exception, but the window does not close.

Here's the signature:

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

Is there a different message that I need to send to Windows Explorer windows? Or an alternate way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2024-08-18 00:14:23

另一种解决方案是使用 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

an alternative solution would be to use PostMessage win API call instead of SendMessage, below is an example which worked fine for me (I'm using 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);

differences between PostMessage and SendMessage api call are described here: http://msdn.microsoft.com/en-us/magazine/cc301431.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文