指定 MessageBox Show 的位置

jackyzhangyb 2009-05-12 01:29:15
想指定它出现的位置和窗体的位置一致,请问如何实现。
...全文
1811 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南野鹤 2012-05-10
  • 打赏
  • 举报
回复
我也试了一下,也不行!
grlsir 2011-12-14
  • 打赏
  • 举报
回复
这个。。。我试了一下好像不行啊。。。
jackyzhangyb 2009-05-13
  • 打赏
  • 举报
回复
牛B啊!
「已注销」 2009-05-12
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace Updater
{
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);
}



}
}

MessageBox好像是不能继承的,这有段代码是实现居中父窗口的,也是改自某位达人的代码(不记得名字了),稍微改改就能实现你自己的目的了
十八道胡同 2009-05-12
  • 打赏
  • 举报
回复
很好的想法,,,
薪水 2009-05-12
  • 打赏
  • 举报
回复
只有这样了。
zhaoweiting0609 2009-05-12
  • 打赏
  • 举报
回复
mark
mathieuxiao 2009-05-12
  • 打赏
  • 举报
回复
和窗体的位置一致是什么意思?
gyouyang 2009-05-12
  • 打赏
  • 举报
回复
写个类继承它,再从新修改位置属性,看可以不
gyouyang 2009-05-12
  • 打赏
  • 举报
回复
不可以,如是要达到你要的效果,那你就自己实现这个类,修改它的位置,看这样可以不

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧