【问题标题】:How Can I Check if a Window is an MDI Window?如何检查窗口是否为 MDI 窗口?
【发布时间】:2011-10-30 16:35:19
【问题描述】:

我想我可以使用一些 user32.dll 调用来验证窗口是否是 MDI 窗口,例如使用 DefMDIChildProc 并查看它是否失败,但我想知道这是否有任何限制,或者是否有更好的方法去做这个?检查父母是否足够?

为简单起见,我最终希望的是一种 IsMDI(IntPtr ptr) 调用...

想法?有什么建议吗?

【问题讨论】:

    标签: c# windows interop winforms-interop user32


    【解决方案1】:

    如果控件位于您自己的 .NET 应用程序中,Form class 具有用于处理 MDI 窗口的属性:

    Form.IsMdiChild

    Form.IsMdiContainer

    Form.MdiParent

    Form.MdiChildren

    【讨论】:

    • 我正在尝试找出任何正在运行的 Window 进程上的任何窗口是否是 MDI 窗口,我没有使用我自己的对象。
    • @AlishahNovin,我希望这不是您的反对意见 - 惩罚未满足您未指定要求的人是不公平的。在您最初的问题中根本不清楚这些窗口不是您的应用程序的一部分。
    【解决方案2】:

    我已经弄明白了(在 pinvoke.net 的帮助下)——您可以根据扩展的 Windows 样式找到答案:

            public static bool IsMDI(IntPtr hwnd)
            {
                WINDOWINFO info = new WINDOWINFO();
                info.cbSize = (uint)Marshal.SizeOf(info);
                GetWindowInfo(hwnd, ref info);
                //0x00000040L is the style for WS_EX_MDICHILD
                return (info.dwExStyle & 0x00000040L)==1;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct WINDOWINFO
            {
                public uint cbSize;
                public RECT rcWindow;
                public RECT rcClient;
                public uint dwStyle;
                public uint dwExStyle;
                public uint dwWindowStatus;
                public uint cxWindowBorders;
                public uint cyWindowBorders;
                public ushort atomWindowType;
                public ushort wCreatorVersion;
    
                public WINDOWINFO(Boolean? filler)
                    : this()   // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
                {
                    cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
                }
    
            }
    
            [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多