如何让messagebox.show的在父窗口上居中显示

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

如何让MessageBox.Show的在父窗口上居中显示...
如何在.NET Windows Form编程中让MessageBox.Show()弹出的消息框在父窗口上居中显示?方法一:由于在VS2008中MessageBox共有21个重载方法,且没有一个方法可以指定显示的位置。

这21个静态方法,实际上是调用的Windows API,你可以尝试直接调用该API来实现。

方法二(推荐):由于MessageBox不能继承和重写,所以可以自己定义一个MessageBox窗体,注意设置DialogResult属性,设置StartPosition为CenterParent在父窗口居中,当然你可以设置Left和Top属性,使消息窗口中任意位置。

使用ShowDialog方法显示与MessageBox.Show类似的模态窗口。

方法三:如果你一定要用MessageBox.Show方法,可以使用Windows API FindWindow函数通过Title找到MessageBox窗口的句柄,通过API函数MoveWindow设置窗口的位置。

下面为MessageBoxEx.cs的代码,可以直接使用MessageBoxEx.Show()方法实现
MessageBox.Show()的功能,并在父窗口上居中显示:using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication3
{
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
};
//实现MessageBox居中owner窗体显示
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);
}
}
}
转载请保留本文链接:
/707043659/blog/1247672380。

相关文档
最新文档