Events can now be instantiated.
Action editor is now a generic object editor.
     1 //=============================================================================
 
     2 // COPYRIGHT: Prosoft-Lanz
 
     3 //=============================================================================
 
     5 // $Workfile: DialogBox.cs $
 
     7 // PROJECT : CodeProject Components
 
     9 // CREATION : 19.02.2003
 
    12 // DETAILS : DialogBoxes centered into the parent owner.
 
    13 //           This class implement the following objects:
 
    15 //   DlgBox.ShowDialog(...)		for CommonDialog and Form
 
    16 //   MsgBox.Show(...)			for standard MessageBox
 
    17 //   AppBox.Show(...)			for standard MessageBox with ProductName as caption
 
    18 //	 ErrBox.Show(...)			for standard error MessageBox
 
    20 //-----------------------------------------------------------------------------
 
    23 using System.Windows.Forms;
 
    24 using System.Runtime.InteropServices;
 
    25 using System.Diagnostics;
 
    27 using CodeProject.Win32API;
 
    28 using CodeProject.Win32API.Hook;
 
    30 namespace CodeProject.Dialog
 
    32 	///////////////////////////////////////////////////////////////////////
 
    36 	/// Class to display a CommonDialog or modal Form centered on the owner.
 
    39 	/// This example display the default print dialog box in the center of the parent.
 
    41 	/// PrintDialog printDlg = new PrintDialog();
 
    42 	/// if (DlgBox.ShowDialog(printDlg, parent) == DialogResult.OK)
 
    43 	///   printDocument.Print();
 
    46 	public sealed class DlgBox
 
    48 		private DlgBox() {}	// To remove the constructor from the documentation!
 
    50 		///////////////////////////////////////////////////////////////////////
 
    54 		/// Show a command dialog box at the center of the active window.
 
    56 		public static DialogResult ShowDialog(CommonDialog dlg)
 
    58 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
    59 			DialogResult dlgResult = dlg.ShowDialog();
 
    60 			centerWindow.Dispose();
 
    65 		/// Show a command dialog box at the center of the owner window.
 
    67 		public static DialogResult ShowDialog(CommonDialog dlg, IWin32Window owner)
 
    69 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
    70 			CenterWindow centerWindow = new CenterWindow(handle);
 
    71 			DialogResult dlgResult = dlg.ShowDialog();
 
    72 			centerWindow.Dispose();
 
    76 		///////////////////////////////////////////////////////////////////////
 
    80 		/// Show a form dialog box at the center of the active window.
 
    82 		public static DialogResult ShowDialog(Form form)
 
    84 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
    85 			DialogResult dlgResult = form.ShowDialog();
 
    86 			centerWindow.Dispose();
 
    91 		/// Show a form dialog box at the center of the owner window.
 
    93 		public static DialogResult ShowDialog(Form form, IWin32Window owner)
 
    95 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
    96 			CenterWindow centerWindow = new CenterWindow(handle);
 
    97 			DialogResult dlgResult = form.ShowDialog();
 
    98 			centerWindow.Dispose();
 
   105 	///////////////////////////////////////////////////////////////////////
 
   109 	/// Class to display a MessageBox centered on the owner.
 
   112 	/// Same methods as the standard MessageBox.
 
   115 	/// This example display a "Hello" message box centered on the owner.
 
   117 	/// MsgBox.Show("Hello");
 
   120 	public sealed class MsgBox
 
   122 		private MsgBox() {}	// To remove the constructor from the documentation!
 
   124 		///////////////////////////////////////////////////////////////////////
 
   128 		/// See MSDN MessageBox() method.
 
   130 		public static DialogResult Show(string text)
 
   132 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   133 			string caption = Application.ProductName;
 
   134 			DialogResult dlgResult = MessageBox.Show(text, caption);
 
   135 			centerWindow.Dispose();
 
   140 		/// See MSDN MessageBox() method.
 
   142 		public static DialogResult Show(IWin32Window owner, string text)
 
   144 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   145 			CenterWindow centerWindow = new CenterWindow(handle);
 
   146 			string caption = Application.ProductName;
 
   147 			DialogResult dlgResult = MessageBox.Show(owner, text, caption);
 
   148 			centerWindow.Dispose();
 
   152 		///////////////////////////////////////////////////////////////////////
 
   156 		/// See MSDN MessageBox() method.
 
   158 		public static DialogResult Show(string text, string caption)
 
   160 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   161 			DialogResult dlgResult = MessageBox.Show(text, caption);
 
   162 			centerWindow.Dispose();
 
   167 		/// See MSDN MessageBox() method.
 
   169 		public static DialogResult Show(IWin32Window owner, string text, string caption)
 
   171 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   172 			CenterWindow centerWindow = new CenterWindow(handle);
 
   173 			DialogResult dlgResult = MessageBox.Show(owner, text, caption);
 
   174 			centerWindow.Dispose();
 
   178 		///////////////////////////////////////////////////////////////////////
 
   179 		// text, caption, buttons
 
   182 		/// See MSDN MessageBox() method.
 
   184 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
 
   186 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   187 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons);
 
   188 			centerWindow.Dispose();
 
   193 		/// See MSDN MessageBox() method.
 
   195 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
 
   197 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   198 			CenterWindow centerWindow = new CenterWindow(handle);
 
   199 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons);
 
   200 			centerWindow.Dispose();
 
   204 		///////////////////////////////////////////////////////////////////////
 
   205 		// text, caption, buttons, defaultButton
 
   208 		/// See MSDN MessageBox() method.
 
   210 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
 
   212 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   213 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon);
 
   214 			centerWindow.Dispose();
 
   219 		/// See MSDN MessageBox() method.
 
   221 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
 
   223 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   224 			CenterWindow centerWindow = new CenterWindow(handle);
 
   225 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon);
 
   226 			centerWindow.Dispose();
 
   230 		///////////////////////////////////////////////////////////////////////
 
   231 		// text, caption, buttons, defaultButton, icon
 
   234 		/// See MSDN MessageBox() method.
 
   236 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
 
   238 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   239 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton);
 
   240 			centerWindow.Dispose();
 
   245 		/// See MSDN MessageBox() method.
 
   247 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
 
   249 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   250 			CenterWindow centerWindow = new CenterWindow(handle);
 
   251 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton);
 
   252 			centerWindow.Dispose();
 
   256 		///////////////////////////////////////////////////////////////////////
 
   257 		// text, caption, buttons, defaultButton, icon, options
 
   260 		/// See MSDN MessageBox() method.
 
   262 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
 
   264 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   265 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
 
   266 			centerWindow.Dispose();
 
   271 		/// See MSDN MessageBox() method.
 
   273 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
 
   275 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   276 			CenterWindow centerWindow = new CenterWindow(handle);
 
   277 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options);
 
   278 			centerWindow.Dispose();
 
   285 	///////////////////////////////////////////////////////////////////////
 
   289 	/// Class to display a MessageBox centered on the owner.
 
   290 	/// The MessageBox caption is always Application.ProductName.
 
   293 	/// Same methods as the standard MessageBox without caption.
 
   296 	/// This example display an application message box centered on the owner.
 
   298 	/// AppBox.Show("Hello");
 
   301 	public sealed class AppBox
 
   303 		private AppBox() {}	// To remove the constructor from the documentation!
 
   305 		///////////////////////////////////////////////////////////////////////
 
   309 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   311 		public static DialogResult Show(string text)
 
   313 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   314 			string caption = Application.ProductName;
 
   315 			DialogResult dlgResult = MessageBox.Show(text, caption);
 
   316 			centerWindow.Dispose();
 
   321 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   323 		public static DialogResult Show(IWin32Window owner, string text)
 
   325 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   326 			CenterWindow centerWindow = new CenterWindow(handle);
 
   327 			string caption = Application.ProductName;
 
   328 			DialogResult dlgResult = MessageBox.Show(owner, text, caption);
 
   329 			centerWindow.Dispose();
 
   333 		///////////////////////////////////////////////////////////////////////
 
   337 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   339 		public static DialogResult Show(string text, MessageBoxButtons buttons)
 
   341 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   342 			string caption = Application.ProductName;
 
   343 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons);
 
   344 			centerWindow.Dispose();
 
   349 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   351 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons)
 
   353 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   354 			CenterWindow centerWindow = new CenterWindow(handle);
 
   355 			string caption = Application.ProductName;
 
   356 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons);
 
   357 			centerWindow.Dispose();
 
   361 		///////////////////////////////////////////////////////////////////////
 
   362 		// text, buttons, defaultButton
 
   365 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   367 		public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon)
 
   369 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   370 			string caption = Application.ProductName;
 
   371 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon);
 
   372 			centerWindow.Dispose();
 
   377 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   379 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon)
 
   381 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   382 			CenterWindow centerWindow = new CenterWindow(handle);
 
   383 			string caption = Application.ProductName;
 
   384 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon);
 
   385 			centerWindow.Dispose();
 
   389 		///////////////////////////////////////////////////////////////////////
 
   390 		// text, buttons, defaultButton, icon
 
   393 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   395 		public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
 
   397 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   398 			string caption = Application.ProductName;
 
   399 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton);
 
   400 			centerWindow.Dispose();
 
   405 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   407 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
 
   409 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   410 			CenterWindow centerWindow = new CenterWindow(handle);
 
   411 			string caption = Application.ProductName;
 
   412 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton);
 
   413 			centerWindow.Dispose();
 
   417 		///////////////////////////////////////////////////////////////////////
 
   418 		// text, buttons, defaultButton, icon, options
 
   421 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   423 		public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
 
   425 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
 
   426 			string caption = Application.ProductName;
 
   427 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
 
   428 			centerWindow.Dispose();
 
   433 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
 
   435 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
 
   437 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
 
   438 			CenterWindow centerWindow = new CenterWindow(handle);
 
   439 			string caption = Application.ProductName;
 
   440 			DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options);
 
   441 			centerWindow.Dispose();
 
   448 	///////////////////////////////////////////////////////////////////////
 
   452 	/// Class to display application error MessageBox centered on the owner.
 
   453 	/// The caption of the MessageBox is Application.ProductName.
 
   456 	/// This example display an error message box centered on the owner.
 
   461 	public sealed class ErrBox
 
   463 		private ErrBox() {}	// To remove the constructor from the documentation!
 
   466 		/// Show an error MessageBox with an icon error and an OK button.
 
   468 		/// <param name="err">The error message.</param>
 
   469 		/// <param name="owner">The owner of the error MessageBox.</param>
 
   470 		/// <returns>Dialog result of the MessageBox.</returns>
 
   471 		public static DialogResult Show(IWin32Window owner, string err)
 
   473 			string caption = Application.ProductName;
 
   474 			return MsgBox.Show(owner, err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
 
   478 		/// Show an error MessageBox with an icon error and an OK button.
 
   480 		/// <param name="err">The error message.</param>
 
   481 		/// <returns>Dialog result of the MessageBox.</returns>
 
   482 		public static DialogResult Show(string err)
 
   484 			string caption = Application.ProductName;
 
   485 			return MsgBox.Show(err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
 
   489 		/// Show an error MessageBox with exception message, an icon error and an OK button.
 
   491 		/// <param name="ex">Exception to be displayed.</param>
 
   492 		/// <returns>Dialog result of the MessageBox.</returns>
 
   493 		public static DialogResult Show(Exception ex)
 
   495 			string err = ex.Message;
 
   496 			while (ex.InnerException != null)
 
   498 				ex = ex.InnerException;
 
   499 				err += Environment.NewLine;
 
   502 			string caption = Application.ProductName;
 
   503 			return MsgBox.Show(err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
 
   507 		/// Show a specialized error MessageBox centered into the parent owner.
 
   509 		/// <param name="ex">Exception to be displayed.</param>
 
   510 		/// <param name="debugMode">true to display the full informations else false.</param>
 
   511 		/// <returns>Dialog result of the MessageBox.</returns>
 
   512 		public static DialogResult Show(Exception ex, bool debugMode)
 
   517 				return Show(ex.Message);
 
   523 	///////////////////////////////////////////////////////////////////////
 
   524 	#region CenterWindow class
 
   526 	internal sealed class CenterWindow
 
   528 		public IntPtr hOwner = IntPtr.Zero;
 
   529 		private Rectangle rect;
 
   531 		public CbtHook cbtHook = null;
 
   532 		public WndProcRetHook wndProcRetHook = null;
 
   534 		public CenterWindow(IntPtr hOwner)
 
   536 			this.hOwner = hOwner;
 
   537 			this.cbtHook = new CbtHook();
 
   538 			cbtHook.WindowActivate += new CbtHook.CbtEventHandler(WndActivate);
 
   542 		public void Dispose()
 
   544 			if (wndProcRetHook != null)
 
   546 				wndProcRetHook.Uninstall();
 
   547 				wndProcRetHook = null;
 
   556 		public void WndActivate(object sender, CbtEventArgs e)
 
   558 			IntPtr hMsgBox = e.wParam;
 
   560 			// try to find a howner for this message box
 
   561 			if (hOwner == IntPtr.Zero)
 
   562 				hOwner = USER32.GetActiveWindow();
 
   564 			// get the MessageBox window rect
 
   565 			RECT rectDlg = new RECT();
 
   566 			USER32.GetWindowRect(hMsgBox, ref rectDlg);
 
   568 			// get the owner window rect
 
   569 			RECT rectForm = new RECT();
 
   570 			USER32.GetWindowRect(hOwner, ref rectForm);
 
   572 			// get the biggest screen area
 
   573 			Rectangle rectScreen = API.TrueScreenRect;
 
   575 			// if no parent window, center on the primary screen
 
   576 			if (rectForm.right == rectForm.left)
 
   577 				rectForm.right = rectForm.left = Screen.PrimaryScreen.WorkingArea.Width / 2;
 
   578 			if (rectForm.bottom == rectForm.top)
 
   579 				rectForm.bottom = rectForm.top = Screen.PrimaryScreen.WorkingArea.Height / 2;
 
   582 			int dx = ((rectDlg.left + rectDlg.right) - (rectForm.left + rectForm.right)) / 2;
 
   583 			int dy = ((rectDlg.top + rectDlg.bottom) - (rectForm.top + rectForm.bottom)) / 2;
 
   585 			rect = new Rectangle(
 
   588 				rectDlg.right - rectDlg.left,
 
   589 				rectDlg.bottom - rectDlg.top);
 
   591 			// place in the screen
 
   592 			if (rect.Right > rectScreen.Right) rect.Offset(rectScreen.Right - rect.Right, 0);
 
   593 			if (rect.Bottom > rectScreen.Bottom) rect.Offset(0, rectScreen.Bottom - rect.Bottom);
 
   594 			if (rect.Left < rectScreen.Left) rect.Offset(rectScreen.Left - rect.Left, 0);
 
   595 			if (rect.Top < rectScreen.Top) rect.Offset(0, rectScreen.Top - rect.Top);
 
   599 				// do the job when the WM_INITDIALOG message returns
 
   600 				wndProcRetHook = new WndProcRetHook(hMsgBox);
 
   601 				wndProcRetHook.WndProcRet += new WndProcRetHook.WndProcEventHandler(WndProcRet);
 
   602 				wndProcRetHook.Install();
 
   605 				USER32.MoveWindow(hMsgBox, rect.Left, rect.Top, rect.Width, rect.Height, 1);
 
   607 			// uninstall this hook
 
   608 			WindowsHook wndHook = (WindowsHook)sender;
 
   609 			Debug.Assert(cbtHook == wndHook);
 
   614 		public void WndProcRet(object sender, WndProcRetEventArgs e)
 
   616 			if (e.cw.message == WndMessage.WM_INITDIALOG ||
 
   617 				e.cw.message == WndMessage.WM_UNKNOWINIT)
 
   619 				USER32.MoveWindow(e.cw.hwnd, rect.Left, rect.Top, rect.Width, rect.Height, 1);
 
   621 				// uninstall this hook
 
   622 				WindowsHook wndHook = (WindowsHook)sender;
 
   623 				Debug.Assert(wndProcRetHook == wndHook);
 
   624 				wndProcRetHook.Uninstall();
 
   625 				wndProcRetHook = null;