c#中如何改变messagebox的位置,求大虾解惑!!

 我来答
leiyangbdwk
推荐于2018-04-12 · TA获得超过3295个赞
知道大有可为答主
回答量:4975
采纳率:12%
帮助的人:4400万
展开全部
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
};

class MessageBoxEx
{
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);
}

}
此示例继承了messagebox。
使用方法
MessageBoxEx.Show("此时弹出于父窗体中间位置,而不是屏幕中间位置");
ChuPa1980
2012-05-09 · 超过14用户采纳过TA的回答
知道答主
回答量:86
采纳率:0%
帮助的人:43.4万
展开全部
不行,除非自己写一个弹窗,不用messagebox
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
gao675597253
2012-05-09 · 超过11用户采纳过TA的回答
知道答主
回答量:64
采纳率:0%
帮助的人:36.4万
展开全部
重写一个messagebox
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式