将Windows窗体设置为弹出子窗口到win32窗口

时间:2013-12-06 00:43:21

标签: winforms winapi

Windows屏幕保护程序API为我提供了/c:xxxx的命令行参数,其中xxxx是窗口的句柄,该窗口应该是我为配置屏幕保护程序所做的任何子窗口的父窗口。 / p>

目前我有这些助手:

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

...

然后在Windows窗体对话框中:

    public ConfigForm(IntPtr parentHWnd)
    {
        InitializeComponent();

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

弹出标志似乎被忽略了,因为Windows窗体是作为子窗体(而不是弹出窗口)绘制的,在父窗口的范围内剪切并且有各种绘图毛刺。检查后,我添加WS_POPUP之前表单的窗口样式为0x06CF0000

如何让Windows窗体对话框成为一个实际的弹出窗口 - 即,提供父句柄的模态,但不剪切到父窗口?

0 个答案:

没有答案