代码之家  ›  专栏  ›  技术社区  ›  James Sulak

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

  •  3
  • James Sulak  · 技术社区  · 16 年前

    我有一个C#应用程序,它使用SendMessage pinvoke方法将“关闭窗口”消息(WM#u 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资源管理器Windows?还是实现这一目标的替代方法?

    1 回复  |  直到 16 年前
        1
  •  12
  •   serge_gubenko    16 年前

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