C#:Win32 Hooks(一)让下一个弹出子窗体自动显示在父窗体中心
/* ----------------------------------------------------------
文件名称:FormHelper.cs
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Splash.Windows.Forms
{
/// <summary>
/// 窗体扩展功能:
/// 1.子窗体自动显示在父窗体中心位置
/// </summary>
public static class FormHelper
{
/// <summary>
/// 子窗体自动显示在父窗体中心位置
/// </summary>
/// <param name="owner">要中心化子窗体的窗体</param>
/// <remarks>扩展方法</remarks>
public static void CenterChild(this IWin32Window owner)
{
CenterChildHelper helper = new CenterChildHelper();
helper.Run(owner);
}
/// <summary>
/// 基于Hook实现子窗体自动显示在父窗体中心位置
/// </summary>
private class CenterChildHelper
{
private const Int32 WH_CBT = 5;
private const Int32 HCBT_ACTIVATE = 5;
private const Int32 GWL_HINSTANCE = -6;
private IntPtr _hhk; // 钩子句柄
private IntPtr _parent; // 父窗体句柄
private GCHandle _gch;
public void Run(IWin32Window owner)
{
NativeMethods.CBTProc CenterChildHookProc = new NativeMethods.CBTProc(CenterChildCallBack);
// 分配新的GCHandle,保护对象不被垃圾回收
_gch = GCHandle.Alloc(CenterChildHookProc);
_parent = owner.Handle; // 父窗体句柄
// 注意:dwThreadId为System.Threading.Thread.CurrentThread.ManagedThreadId不起作用
_hhk = NativeMethods.SetWindowsHookEx(WH_CBT, CenterChildHookProc, IntPtr.Zero, NativeMethods.GetCurrentThreadId());
}
private IntPtr CenterChildCallBack(Int32 nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode == HCBT_ACTIVATE)
{ // 父窗体
NativeMethods.RECT formRect;
NativeMethods.GetWindowRect(_parent, out formRect);
// 子窗体
NativeMethods.RECT messageBoxRect;
NativeMethods.GetWindowRect(wParam, out messageBoxRect);
Int32 width = messageBoxRect.right - messageBoxRect.left; // 消息窗体宽度
Int32 height = messageBoxRect.bottom - messageBoxRect.top; // 消息窗体高度
Int32 xPos = (formRect.left + formRect.right - width) >> 1; // 消息窗易做图于父窗体中心时左上角X坐标
Int32 yPos = (formRect.top + formRect.bottom - height) >> 1; // 消息窗易做图于父窗体中心时左上角Y坐标
// 将消息窗体移到父窗体中心
NativeMethods.MoveWindow(wParam, xPos, yPos, width, height, false);
// 卸载钩子
NativeMethods.UnhookWindowsHookEx(_hhk);
// 释放已分配的GCHandle
_gch.Free();
}
// 允许操作
return IntPtr.Zero;
}
}
private static class NativeMethods
{ 
补充:软件开发 , C# ,