使用我在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,但它们永远不会被调用...
答案 0 :(得分:1)
如果您正确地将表单作为控制面板窗口的子项,则所有这些问题都会消失。我现在就是这样做的,它适用于所有情况。
在表单中,添加this,在创建时强制窗体的窗口样式为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);