Search code examples
c#windowsinteropwinforms-interopuser32

How Can I Check if a Window is an MDI Window?


I imagine there's some user32.dll call that I can use to verify if a window is an MDI window, like using DefMDIChildProc and seeing if it fails, but I wonder if there's any limitations to this, or if there's a better way to do this? Is checking for a Parent sufficient?

For simplicity's sake, what I'm ultimately hoping for is an IsMDI(IntPtr ptr) kind of call...

Thoughts? Suggestions?


Solution

  • I've figured it out (with the help of pinvoke.net) - you can find out based on the Extended Windows Styles:

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