2009-11-07 74 views
3

我有一个使用SendMessage函数PInvoke的方法来发送一个“关闭窗口”消息(WM_CLOSE/16),以不同的Windows应用程序之外C#应用程序。这除了当有问题的窗口是一个Windows资源管理器窗口的伟大工程。我没有得到一个例外,但窗口不关闭。无法使用的PInvoke发送WM_CLOSE到Windows资源管理器窗口

下面是签名:

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

是否有不同的消息,我需要发送到Windows资源管理器窗口?或者完成这个的另一种方式?

回答

12

的替代解决方案是使用PostMessage的赢API调用来代替SendMessage函数,下面是这对我来说工作得很好的例子(我用winXP的SP3):PostMessage的和SendMessage函数的API之间

[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); 

差异调用描述如下:http://msdn.microsoft.com/en-us/magazine/cc301431.aspx

+1

谢谢,这个完美的作品。信息链接也是如此。 – 2009-11-09 03:23:56

+1

感谢。简短明了。而在Silvelight :) – 2013-08-05 14:46:40

+0

神奇的工作! [DestroyWindow](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632682%28v=vs.85%29.aspx)的绝佳替代方案,它只能在创建目标的相同线程上工作窗口在第一个地方。 – 2015-02-02 02:07:06

相关问题