加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:53:36 所属栏目:百科 来源:网络整理
导读:我有一个C#应用程序,它使用SendMessage pinvoke方法向应用程序外的各个窗口发送“关闭窗口”消息(WM_CLOSE / 16).这很有效,除非有问题的窗口是 Windows资源管理器窗口.我没有异常,但窗口没有关闭. 这是签名: [DllImport("user32.dll",CharSet = CharSet.Aut
我有一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读