MessageBox.Show()如何在父窗体居中

conan304 2008-07-08 10:44:40
MessageBox.Show()默认是屏幕居中,有没有办法让此对话框在父窗体居中啊?
比如单击Form1上的button1弹出Messag.Show()对话框,此对话框能在Form1居中吗?而不是默认的屏幕居中

谢谢
...全文
2518 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
xcf007 2012-08-23
  • 打赏
  • 举报
回复
用Hook拦截了,顶!
liutao8984908 2010-12-03
  • 打赏
  • 举报
回复
标记~~
conan304 2008-07-11
  • 打赏
  • 举报
回复
清洁工同志,汗,恕我驽钝,没看懂。不过我还去招书来看的。谢谢。
BadTank,谢谢,相对简单,确实是一个好的解决方法。

ps:谢谢楼上几位顶贴还有给我出主义的朋友,谢谢。
hillspring 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zswang 的回复:]
挺复杂的,得截获MessageBox显示。
C# codeusingSystem.Runtime.InteropServices;publicdelegateIntPtr HookProc(intnCode, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]publicstaticexternIntPtr SetWindowsHookEx(inthookid,
HookProc pfnhook, IntPtr hinst,intthreadid);

[DllImport("user32.dll")]publicstaticexternIntPtr CallNextHookEx(IntPtr hhook,intcode, IntPtr wparam, IntPtr lparam);

[…
[/Quote]


顶!!!
baihe_591 2008-07-09
  • 打赏
  • 举报
回复
只能用API来实现了.
醉龍 2008-07-09
  • 打赏
  • 举报
回复
重写不错
我姓区不姓区 2008-07-09
  • 打赏
  • 举报
回复
看错了,是MessageBox.Show(),请楼主忽略15楼回复
zzyhuian06142 2008-07-09
  • 打赏
  • 举报
回复
Mark下
我姓区不姓区 2008-07-09
  • 打赏
  • 举报
回复

From2 fm = new Form2();
Point point = PointToScreen(this.Location);
fm.StartPosition = FormStartPosition.Manual;
fm.Location = new Point(point.X + (this.Size.Width - fm.Size.Width) / 2, point.Y + (this.Size.Height - fm.Size.Height) / 2);
fm.Show();
czk598478 2008-07-09
  • 打赏
  • 举报
回复

public class MessageBox
{
public MessageBox()
{

}

public static DialogResult Show()
{
Form2 frm = new Form2();//Form2是自己新建的form,外观做的和MessageBox.show出来的效果一样。设置窗体的StartPosition为CenterParent.
return frm.ShowDialog();
}
}



确实要崇拜一下以上楼的牛人们

赵一一 2008-07-09
  • 打赏
  • 举报
回复
只有自己写个Form了
CloneCenter 2008-07-09
  • 打赏
  • 举报
回复
崇拜一下zswang大侠!
badtank 2008-07-09
  • 打赏
  • 举报
回复
我的办法相对简单一些
重写MessageBox类

public class MessageBox
{
public MessageBox()
{

}

public static DialogResult Show()
{
Form2 frm = new Form2();//Form2是自己新建的form,外观做的和MessageBox.show出来的效果一样。设置窗体的StartPosition为CenterParent.
return frm.ShowDialog();
}
}
yygyogfny 2008-07-09
  • 打赏
  • 举报
回复
学习~~
王集鹄 2008-07-09
  • 打赏
  • 举报
回复
挺复杂的,得截获MessageBox显示。
using System.Runtime.InteropServices;

public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(int hookid,
HookProc pfnhook, IntPtr hinst, int threadid);

[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhook,
int code, IntPtr wparam, IntPtr lparam);

[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string modName);

[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(IntPtr hhook);

[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);

[DllImport("user32.dll")]
public static extern bool MoveWindow(
IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

public const int WH_CBT = 5;
public const int HCBT_ACTIVATE = 5;
IntPtr hookHandle = IntPtr.Zero;

private IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
switch(nCode)
{
case HCBT_ACTIVATE:
Rectangle vRectangle = new Rectangle();
GetWindowRect(wParam, ref vRectangle);
vRectangle.Width = vRectangle.Width - vRectangle.Left;
vRectangle.Height = vRectangle.Height - vRectangle.Top;
MoveWindow(wParam, // 右下
Screen.GetWorkingArea(this).Width - vRectangle.Width,
Screen.GetWorkingArea(this).Height - vRectangle.Height,
vRectangle.Width, vRectangle.Height, false);
UnhookWindowsHookEx(hookHandle);
break;
}
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}

private void button1_Click(object sender, EventArgs e)
{
hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback),
GetModuleHandle(null), 0);
MessageBox.Show("Zswang 路过");
}
 居中就是计算一下出现的位置,交给楼主自己。[img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswang/reply.jpg" alt="" />
fellowcheng 2008-07-09
  • 打赏
  • 举报
回复
自己写个Form吧
rqx110 2008-07-09
  • 打赏
  • 举报
回复
你可以自己写个MessageBox嘛,然后showDialog就好了
conan304 2008-07-09
  • 打赏
  • 举报
回复
谢谢楼上的几位。但是还是无法解决问题。
MessageBox.Show()无法设置location属性

楼上的,哪个帖子,其实老早之前我就搜索到了。

show.StartPosition = FormStartPosition.CenterParent;
或者设置centralparent属性
一样,无法设置StartPosition属性
bbbbbb888888 2008-07-09
  • 打赏
  • 举报
回复
mark,看大侠重写
mapserver 2008-07-08
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080519/15/ba651bbe-286a-4a2c-961b-e712a40f999d.html
加载更多回复(4)

110,567

社区成员

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

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

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