在右边项目上点击右键->Add->class,添加MessageBoxEx类:

 using System;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices; public class MessageBoxEx
{
private static IWin32Window _owner;
private static HookProc _hookProc;
private static IntPtr _hHook; public static DialogResult Show(string text)
{
Initialize();
return MessageBox.Show(text);
} public static DialogResult Show(string text, string caption)
{
Initialize();
return MessageBox.Show(text, caption);
} public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
{
Initialize();
return MessageBox.Show(text, caption, buttons);
} public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
Initialize();
return MessageBox.Show(text, caption, buttons, icon);
} public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton)
{
Initialize();
return MessageBox.Show(text, caption, buttons, icon, defButton);
} public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options)
{
Initialize();
return MessageBox.Show(text, caption, buttons, icon, defButton, options);
} public static DialogResult Show(IWin32Window owner, string text)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text);
} public static DialogResult Show(IWin32Window owner, string text, string caption)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption);
} public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons);
} public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons, icon);
} public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons, icon, defButton);
} public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options)
{
_owner = owner;
Initialize();
return MessageBox.Show(owner, text, caption, buttons, icon,
defButton, options);
} public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); public delegate void TimerProc(IntPtr hWnd, uint uMsg, UIntPtr nIDEvent, uint dwTime); public const int WH_CALLWNDPROCRET = ; public enum CbtHookAction : int
{
HCBT_MOVESIZE = ,
HCBT_MINMAX = ,
HCBT_QS = ,
HCBT_CREATEWND = ,
HCBT_DESTROYWND = ,
HCBT_ACTIVATE = ,
HCBT_CLICKSKIPPED = ,
HCBT_KEYSKIPPED = ,
HCBT_SYSCOMMAND = ,
HCBT_SETFOCUS =
} [DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect); [DllImport("user32.dll")]
private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("User32.dll")]
public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc); [DllImport("User32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll")]
public static extern int UnhookWindowsHookEx(IntPtr idHook); [DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength); [DllImport("user32.dll")]
public static extern int EndDialog(IntPtr hDlg, IntPtr nResult); [StructLayout(LayoutKind.Sequential)]
public struct CWPRETSTRUCT
{
public IntPtr lResult;
public IntPtr lParam;
public IntPtr wParam;
public uint message;
public IntPtr hwnd;
} ; static MessageBoxEx()
{
_hookProc = new HookProc(MessageBoxHookProc);
_hHook = IntPtr.Zero;
} private static void Initialize()
{
if (_hHook != IntPtr.Zero)
{
throw new NotSupportedException("multiple calls are not supported");
} if (_owner != null)
{
_hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, _hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
}
} private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < )
{
return CallNextHookEx(_hHook, nCode, wParam, lParam);
} CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));
IntPtr hook = _hHook; if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE)
{
try
{
CenterWindow(msg.hwnd);
}
finally
{
UnhookWindowsHookEx(_hHook);
_hHook = IntPtr.Zero;
}
} return CallNextHookEx(hook, nCode, wParam, lParam);
} private static void CenterWindow(IntPtr hChildWnd)
{
Rectangle recChild = new Rectangle(, , , );
bool success = GetWindowRect(hChildWnd, ref recChild); int width = recChild.Width - recChild.X;
int height = recChild.Height - recChild.Y; Rectangle recParent = new Rectangle(, , , );
success = GetWindowRect(_owner.Handle, ref recParent); Point ptCenter = new Point(, );
ptCenter.X = recParent.X + ((recParent.Width - recParent.X) / );
ptCenter.Y = recParent.Y + ((recParent.Height - recParent.Y) / ); Point ptStart = new Point(, );
ptStart.X = (ptCenter.X - (width / ));
ptStart.Y = (ptCenter.Y - (height / )); ptStart.X = (ptStart.X < ) ? : ptStart.X;
ptStart.Y = (ptStart.Y < ) ? : ptStart.Y; int result = MoveWindow(hChildWnd, ptStart.X, ptStart.Y, width,
height, false);
} }

调用办法

MessageBoxEx.Show(this, "Please fix the validation errors before saving.", "Validation Errors");

C# messagebox 居中父窗体的更多相关文章

  1. c# 子窗体居中父窗体

    1.设置CenterParent不管用.只好用代码控制. frmRunning_ = new FrmRunning(); frmRunning_.StartPosition = FormStartPo ...

  2. Extjs 窗体居中,双重窗体弹出时清除父窗体的鼠标事件

    这个是监控窗体缩放的事件 缩放中居中主要在 'beforeshow' 和 'destroy'两个事件里面监控 var EditTempWindow; Ext.EventManager.onWindow ...

  3. javascript关闭弹出窗体时刷新父窗体和居中显示弹出窗

    居中显示用到了moveTO()方法: 关闭弹出窗时刷新父窗体用到了window.opener方法: 父窗体代码例如以下: <%@ Page Language="C#" Aut ...

  4. 【winform】userControl刷新父窗体的datagridview

    1.ContextMenuStrip 获取右键控件名称 this.contextMenuScriptScore.SourceControl.Name; //当前控件名 2.radiobutton 分组 ...

  5. winform c#中子窗体关闭刷新父窗体

    父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...

  6. silverlight 对ChildWindow返回给父窗体值的理解(转载)

    这篇文章是我对ChildWindow的理解,举例说明: 有时候在项目中需要弹出子窗体进行一些操作,然后将操作的值返回到父窗体中. 下图是子窗体的界面(比较粗糙....) 下面贴出其代码: 子窗体前台代 ...

  7. C#父窗体右击事件实现

    之前在博问上提问过,没人回答啊,豆太少没人权? 没注册钩子的话根本没办法弹出右键菜单啊,因为在父窗体内有一个容器,所以鼠标在右击时是无法触发窗体的mousedown事件的,即使把KeyPreview设 ...

  8. silverlight子窗体操作数据库后刷新父窗体

    silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...

  9. WPF 子窗体关闭,刷新父窗体

    父窗体代码 private void DGUserEdit() { if(DGUser.SelectedItem!=null) { DataRow dr = (DGUser.SelectedItem ...

随机推荐

  1. UVA 1640 The Counting Problem(按位dp)

    题意:给你整数a.b,问你[a,b]间每个数字分解成单个数字后,0.1.2.3.4.5.6.7.8.9,分别有多少个 题解:首先找到[0,b]与[0,a-1]进行区间减法,接着就只是求[0,x] 对于 ...

  2. mysql简单的增删改查

    增加 MYSQL>insert into class (stu,name,age) values (1,'zhangsan',23);(回车) 另外,如果输入的是中文的话,在windows下可能 ...

  3. eclipse中去掉警告提示

    有时候我们要去掉这些不必要的提示 下面我们来设置去掉这些警告提示

  4. CocoaPods学习系列1——安装和常规使用

    CocoaPods是一个Github上的开源项目,目前已经成为iOS开发过程中标准的依赖库管理器,提供了一种对第三方类库简单优雅的集成和管理方案. 其工作原理,是将第三方类库统一管理到一个名为Pods ...

  5. ZC_操作_not敲代码

    1.javah 命令(路径为 项目的bin目录下),例如 : F:\ZC_Code_E\workspace__MyEclipse2013\JNIjw01\bin>javah jniZ.JNIjw ...

  6. elsevier 与 springer 投稿区别

    http://emuch.net/bbs/viewthread.php?tid=5369913

  7. Angularjs注入拦截器实现Loading效果

    angularjs作为一个全ajax的框架,对于请求,如果页面上不做任何操作的话,在结果烦回来之前,页面是没有任何响应的,不像普通的HTTP请求,会有进度条之类. 什么是拦截器? $httpProvi ...

  8. 使用springmvc时报错JSPs only permit GET POST or HEAD

    两个地方需要注意:第一处在web.xml文件中不要忘记配置 <filter> <filter-name>HiddenHttpMethodFilter</filter-na ...

  9. UnsupportedOperationException

    java不支持该功能,多见于, Arrays.asList() ,然后使用remove和add方法.     因为asarraylist的集合是一个转化来的集合,它的大小是固定的.不能进行长度修改. ...

  10. Ajax基础(二)--获取服务器文件

    获取服务器文件相关步骤: 1.创建文件: 2.创建XMLHttpRequest对象: 3.获取文件(注意事项:1)在服务器中运行测试:2)注意编码问题,编码要统一). 3.1 获取xml文件: HTM ...