自定义提示框之C#设计笔记(十一)

12 篇文章 0 订阅

一、定义提示框位置结构体
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
};
二、实现MessageBox居中owner窗体显示类
class MessageEx
{
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport(“user32.dll”)]
private static extern IntPtr SetWindowsHookEx(int hookid,
HookProc pfnhook, IntPtr hinst, int threadid);
[DllImport(“user32.dll”)]
private static extern IntPtr CallNextHookEx(IntPtr hhook,
int code, IntPtr wparam, IntPtr lparam);
[DllImport(“kernel32.dll”)]
private static extern IntPtr GetModuleHandle(string modName);
[DllImport(“user32.dll”)]
private static extern bool UnhookWindowsHookEx(IntPtr hhook);
[DllImport(“user32.dll”)]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport(“user32.dll”)]
private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport(“user32.dll”)]
private static extern bool MoveWindow(
IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private const int WH_CBT = 5;
private const int HCBT_ACTIVATE = 5;
private const int GW_OWNER = 4;
private static IntPtr hookHandle = IntPtr.Zero;
private static RECT GetOwnerRect(IntPtr hwnd)
{
RECT ownerRect = new RECT();
IntPtr ownerHwnd = GetWindow(hwnd, GW_OWNER);
GetWindowRect(ownerHwnd, ref ownerRect);
return ownerRect;
}
private static IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
switch (nCode)
{
case HCBT_ACTIVATE:
RECT vRectangle = new RECT();
RECT ownerRect = GetOwnerRect(wParam);
GetWindowRect(wParam, ref vRectangle);
int width = vRectangle.right - vRectangle.left;
int height = vRectangle.bottom - vRectangle.top;
int ownerWidth = ownerRect.right - ownerRect.left;
int ownerHeight = ownerRect.bottom - ownerRect.top;
int left = Math.Max(ownerRect.left + (ownerWidth - width) / 2, 0);
int top = Math.Max(ownerRect.top + (ownerHeight - height) / 2, 0);
MoveWindow(wParam,
left,
top,
width, height, false);
UnhookWindowsHookEx(hookHandle);
break;
}
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}
private static void Lock()
{
hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback),
GetModuleHandle(null), 0);
}
//根据需要重载
public static DialogResult Show(string text)
{
Lock();
return MessageBox.Show(text);
}
public static DialogResult Show(IWin32Window owner, string text)
{
Lock();
return MessageBox.Show(owner, text);
}
public static DialogResult Show(string text, string caption)
{
Lock();
return MessageBox.Show(text, caption);
}
public static DialogResult Show(IWin32Window owner, string text, string caption)
{
Lock();
return MessageBox.Show(owner, text, caption);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
{
Lock();
return MessageBox.Show(text, caption, buttons);
}
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
{
Lock();
return MessageBox.Show(owner, text, caption, buttons);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
Lock();
return MessageBox.Show(text, caption, buttons, icon);
}
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
Lock();
return MessageBox.Show(owner, text, caption, buttons, icon);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
Lock();
return MessageBox.Show(text, caption, buttons, icon, defaultButton);
}
public static DialogResult Show(IWin32Window owner,string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
Lock();
return MessageBox.Show(owner,text, caption, buttons, icon, defaultButton);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值