1

使用我在 CodeProject 上找到的代码,我创建了一个屏幕保护程序。下面的表单是当用户选择我的屏幕保护程序时我在控制面板中显示的一个很小的表单。

一切似乎都运行良好;表单在控制面板的正确位置正确调整大小并绘制(它是空白的),随着 CP 移动等。但是当控制面板关闭(或用另一个屏幕保护程序的迷你预览替换我的表单)时,我的应用程序不会死。它只是挂在记忆中。

我的表单没有收到表单关闭/关闭消息或可见性更改等。我在这里没有正确设置父母身份吗?

这是相关的代码。所有导入的 WinAPI 调用都返回预期值,而 GetLastError 总是返回零,所以我认为这不是问题......

    private void miniControlPanelForm_Load(object sender, EventArgs e)
    {

        // note that iphWnd is a class variable, passed to us by windows

        // set our window style to WS_CHILD, so that our window is
        // destroyed when parent window is destroyed.

        // get the current window style, but with WS_CHILD set
        IntPtr ip = new IntPtr();
        int index = (int)NativeMethods.WindowLongFlags.GWL_STYLE | 0x40000000;   
        ip = NativeMethods.GetWindowLongPtr(this.Handle, index);
        int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

        // set that value as our current Style
        object ohRef = new object();
        HandleRef hRef = new HandleRef(ohRef, this.Handle);
        IntPtr ip2 = new IntPtr();
        int index2 = (int)NativeMethods.WindowLongFlags.GWL_STYLE;
        ip2 = NativeMethods.SetWindowLongPtr(hRef, index2, ip);
        error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

        // set the passed preview window as the parent of this window
        IntPtr newOldParent = NativeMethods.SetParent(this.Handle, iphWnd);
        error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

        //set our window's size to the size of our window's new parent
        Rectangle ParentRect = new Rectangle();
        NativeMethods.GetClientRect(iphWnd, ref ParentRect);
        this.Size = ParentRect.Size;

        //set our location at (0, 0)
        this.Location = new Point(0, 0);
    }

我在各种“表单正在关闭”事件处理程序中有 Application.Exit,但它们从未被调用...

4

1 回答 1

1

如果您正确地将表单设置为控制面板窗口的子窗口,所有这些问题都会消失。这是我现在的做法,它适用于所有情况。

在表单中,添加这个,在创建时强制表单的窗口样式为 WS_CHILD:

    /// <summary>
    /// Override CreateParams property so we can add "WS_CHILD" to
    /// the Style each time it is queried during window creation.
    /// </summary>
    protected override CreateParams CreateParams
    {
        get
        {
            // get the base params and modify them
            CreateParams cp = base.CreateParams;
            cp.Style |= NativeMethods.WindowStyles.WS_CHILD;
            return cp;
        }
    }

在接收控制面板的 hWnd 并创建表单的代码中,使用 SetParent 使表单成为控制面板的子项:

    /// <summary>
    /// Show the form in the little control panel preview window.
    /// </summary>
    /// <param name="hWnd">hwnd passed to us at launch by windows</param>
    static void ShowMiniPreview(IntPtr hWnd)
    {
        if (NativeMethods.IsWindow(hWnd))
        {
            miniControlPanelForm preview = new miniControlPanelForm(hWnd);
            IntPtr newParent = NativeMethods.SetParent(preview.Handle, hWnd);

            // Set the size of the form to the size of the parent window (using the passed hWnd).
            System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
            bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);

            // Set our size to new rect and location at (0, 0)
            preview.Size = ParentRect.Size;
            preview.Location = new System.Drawing.Point(0, 0);

            // Show the form
            preview.Show();

            // and run it
            Application.Run(preview);
        }
    }

请注意,“NativeMethods”是我的类,其中包含各种 Win32 方法和声明为 PInvokes 的常量:

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

    public static class WindowStyles
    {
        public static readonly Int32
        WS_CHILD = 0x40000000;
    }

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool GetClientRect(IntPtr hWnd, ref Rectangle rect);
于 2014-08-25T04:37:10.680 回答