微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个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资源管理器窗口吗?或者另一种方法来实现这一目标?

解决方法

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

PostMessage和SendMessage api调用间的差异在这里描述:http://msdn.microsoft.com/en-us/magazine/cc301431.aspx

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐