C# messagebox 居中父窗体
在右边项目上点击右键->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 居中父窗体的更多相关文章
- c# 子窗体居中父窗体
1.设置CenterParent不管用.只好用代码控制. frmRunning_ = new FrmRunning(); frmRunning_.StartPosition = FormStartPo ...
- Extjs 窗体居中,双重窗体弹出时清除父窗体的鼠标事件
这个是监控窗体缩放的事件 缩放中居中主要在 'beforeshow' 和 'destroy'两个事件里面监控 var EditTempWindow; Ext.EventManager.onWindow ...
- javascript关闭弹出窗体时刷新父窗体和居中显示弹出窗
居中显示用到了moveTO()方法: 关闭弹出窗时刷新父窗体用到了window.opener方法: 父窗体代码例如以下: <%@ Page Language="C#" Aut ...
- 【winform】userControl刷新父窗体的datagridview
1.ContextMenuStrip 获取右键控件名称 this.contextMenuScriptScore.SourceControl.Name; //当前控件名 2.radiobutton 分组 ...
- winform c#中子窗体关闭刷新父窗体
父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮,Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2,再te ...
- silverlight 对ChildWindow返回给父窗体值的理解(转载)
这篇文章是我对ChildWindow的理解,举例说明: 有时候在项目中需要弹出子窗体进行一些操作,然后将操作的值返回到父窗体中. 下图是子窗体的界面(比较粗糙....) 下面贴出其代码: 子窗体前台代 ...
- C#父窗体右击事件实现
之前在博问上提问过,没人回答啊,豆太少没人权? 没注册钩子的话根本没办法弹出右键菜单啊,因为在父窗体内有一个容器,所以鼠标在右击时是无法触发窗体的mousedown事件的,即使把KeyPreview设 ...
- silverlight子窗体操作数据库后刷新父窗体
silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...
- WPF 子窗体关闭,刷新父窗体
父窗体代码 private void DGUserEdit() { if(DGUser.SelectedItem!=null) { DataRow dr = (DGUser.SelectedItem ...
随机推荐
- HttpServletResponse 的 sendError( )方法以及常用的HttpServletResponse常量级错误代码
HttpServletResponse 的 sendError( )方法以及常用的HttpServletResponse常量级错误代码 转载:http://hi.baidu.com/yanfei_ ...
- 《Computational Statistics with Matlab》硬译
第1章 从随机变量采样 研究者提出的概率模型对于分析方法来说通常比较复杂,研究者处理复杂概率模型时越来越依赖计算.数值方法,通过使用计算方法,研究者就不用对一些分析技术做一些不现实的假设(如正态性和独 ...
- OpenStack Ceilometer -- 后台数据存储优化之MongoDB的分片存储设置
https://xiaofandh12.github.io/Mongo-Shard 关于MongoDB MongoDB中的概念与关系型数据库之间的对应: Database --> Databas ...
- LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)
题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...
- MSSQL2008 临时总结文档2014
2. 索引可以建立在基表上(基表索引),也可以视图上称为视图索引. 基表索引是建立在表数据中的,故视图索引是建立在查询后的结果集上的. (假如建立了视图索引)当从视图中取数据的时候,优化查询分析器会 ...
- JSON.parse()和JSON.stringify()以及stringify()字符串格式化
1. parse用于从一个字符串中解析出json对象,如var str = '{"name":"huangxiaojian","age":& ...
- Neutron的防火墙原理
确切的说这是fwaas,即防火墙即是服务. 防火墙与安全组区别防火墙一般放在网关上,用来隔离子网之间的访问.因此,防火墙即服务也是在网络节点上(具体说来是在路由器命名空间中)来实现. 安全组的对象是虚 ...
- Android中SQLite介绍
现在的主流移动设备像Android.iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储我们大量的数据,所以我们就需要掌握移动设备上 ...
- 原创:项目管理的理论与实践 讲座的PPT
业余时间做的两个PPT,曾经给公司同事讲过,PPT内容毕竟还是不够全面,如果有不清楚的地方,欢迎提问 项目管理的理论与实践 虚拟案例-超市管理系统
- 《Drools7.0.0.Final规则引擎教程》第4章 4.2 activation-group& dialect& date-effective
activation-group 该属性将若干个规则划分成一个组,统一命名.在执行的时候,具有相同activation-group 属性的规则中只要有一个被执行,其它的规则都不再执行.可以用类似sal ...