首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

MessageBox.Show()怎么在父窗体居中

2012-09-11 
MessageBox.Show()如何在父窗体居中MessageBox.Show()默认是屏幕居中,有没有办法让此对话框在父窗体居中啊

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

谢谢

[解决办法]
不是有这个成员么?
Show(IWin32Window, String) 

[解决办法]
show之前设置一下location 不就好了吗?

应该是 (父窗体宽-show出来窗体的宽)/2 , (父窗体高-show出来窗体的高)/2

这个点就对了

我是小菜鸟! 说的不好清见谅, 希望能够帮到你!
[解决办法]
http://topic.csdn.net/u/20080519/15/ba651bbe-286a-4a2c-961b-e712a40f999d.html
[解决办法]
你可以自己写个MessageBox嘛,然后showDialog就好了
[解决办法]
自己写个Form吧
[解决办法]
挺复杂的,得截获MessageBox显示。

C# code
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 路过");}
[解决办法]
学习~~
[解决办法]
我的办法相对简单一些
重写MessageBox类
C# code
public class MessageBox    {        public MessageBox()        {                 }                public static DialogResult Show()        {            Form2 frm = new Form2();//Form2是自己新建的form,外观做的和MessageBox.show出来的效果一样。设置窗体的StartPosition为CenterParent.            return frm.ShowDialog();        }    }
[解决办法]
崇拜一下zswang大侠!
[解决办法]
只有自己写个Form了
[解决办法]
C# code
public class MessageBox    {        public MessageBox()        {                 }                public static DialogResult Show()        {            Form2 frm = new Form2();//Form2是自己新建的form,外观做的和MessageBox.show出来的效果一样。设置窗体的StartPosition为CenterParent.            return frm.ShowDialog();        }    } 


[解决办法]

C# code
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();
[解决办法]
重写不错
[解决办法]
只能用API来实现了.
[解决办法]
探讨
挺复杂的,得截获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);

[…

[解决办法]
mark,看大侠重写

热点排行