Set Windows Form as a popup child to a win32 window

0

The Windows screensaver API gives me command-line arguments of /c:xxxx, where xxxx is a handle to the window that's supposed to be a parent to whatever child window I make to configure the screensaver.

Currently I have these helpers:

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    const int GWL_STYLE = -16;

    [Flags]
    public enum WindowStyle : ulong
    {
        WS_OVERLAPPED = 0x00000000,
        WS_POPUP = 0x80000000,
        WS_CHILD = 0x40000000,
        WS_MINIMIZE = 0x20000000,
        WS_VISIBLE = 0x10000000,
        WS_DISABLED = 0x08000000,
        WS_CLIPSIBLINGS = 0x04000000,
        WS_CLIPCHILDREN = 0x02000000,
        WS_MAXIMIZE = 0x01000000,
        WS_CAPTION = 0x00C00000, // WS_BORDER | WS_DLGFRAME
        WS_BORDER = 0x00800000,
        WS_DLGFRAME = 0x00400000,
        WS_VSCROLL = 0x00200000,
        WS_HSCROLL = 0x00100000,
        WS_SYSMENU = 0x00080000,
        WS_THICKFRAME = 0x00040000,
        WS_GROUP = 0x00020000,
        WS_TABSTOP = 0x00010000,
        WS_MINIMIZEBOX = 0x00020000,
        WS_MAXIMIZEBOX = 0x00010000,
        WS_TILED = WS_OVERLAPPED,
        WS_ICONIC = WS_MINIMIZE,
        WS_SIZEBOX = WS_THICKFRAME
    };

    [DllImport("user32.dll")]
    static extern UIntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, UIntPtr dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    static extern UIntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);

    public static void SetParent(Control child, IntPtr parent)
    {
        SetParent(child.Handle, parent);
    }

    public static void AddStyle(Control child, WindowStyle addstyle)
    {
        UIntPtr style = GetWindowLongPtr(child.Handle, GWL_STYLE);
        style = new UIntPtr(style.ToUInt64() | (UInt64)addstyle);
        SetWindowLongPtr(child.Handle, GWL_STYLE, style);
    }

    public static Rectangle GetClientRect(IntPtr window)
    {
        Rectangle rect;
        GetClientRect(window, out rect);
        return rect;
    }

...

Then in the Windows Forms dialog:

    public ConfigForm(IntPtr parentHWnd)
    {
        InitializeComponent();

        User32.SetParent(this, parentHWnd);
        User32.AddStyle(this, User32.WindowStyle.WS_POPUP);
    }

The popup flag seems to be ignored, because the Windows form is drawn as a child (not a popup), clipping within the bounds of the parent and having all sorts of drawing glitches. Upon inspection, the window style of the form before I add WS_POPUP is 0x06CF0000.

How do I get the Windows Forms dialog to be an actual popup - that is, modal to the provided parent handle but without clipping to the parent's bounds?

winforms
winapi
asked on Stack Overflow Dec 6, 2013 by Reinderien

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0