DialogBox.cs
author sl
Tue, 08 Jul 2014 20:02:12 +0200
changeset 9 7c363a7a975e
permissions -rw-r--r--
Persisting brightness, fixing brightness tracker steps, adding show borders option.
     1 //=============================================================================
     2 // COPYRIGHT: Prosoft-Lanz
     3 //=============================================================================
     4 //
     5 // $Workfile: DialogBox.cs $
     6 //
     7 // PROJECT : CodeProject Components
     8 // VERSION : 1.00
     9 // CREATION : 19.02.2003
    10 // AUTHOR : JCL
    11 //
    12 // DETAILS : DialogBoxes centered into the parent owner.
    13 //           This class implement the following objects:
    14 //
    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
    19 //
    20 //-----------------------------------------------------------------------------
    21 using System;
    22 using System.Drawing;
    23 using System.Windows.Forms;
    24 using System.Runtime.InteropServices;
    25 using System.Diagnostics;
    26 
    27 using CodeProject.Win32API;
    28 using CodeProject.Win32API.Hook;
    29 
    30 namespace CodeProject.Dialog
    31 {
    32 	///////////////////////////////////////////////////////////////////////
    33 	#region DlgBox
    34 
    35 	/// <summary>
    36 	/// Class to display a CommonDialog or modal Form centered on the owner.
    37 	/// </summary>
    38 	/// <example>
    39 	/// This example display the default print dialog box in the center of the parent.
    40 	/// <code>
    41 	/// PrintDialog printDlg = new PrintDialog();
    42 	/// if (DlgBox.ShowDialog(printDlg, parent) == DialogResult.OK)
    43 	///   printDocument.Print();
    44 	/// </code>
    45 	/// </example>
    46 	public sealed class DlgBox
    47 	{
    48 		private DlgBox() {}	// To remove the constructor from the documentation!
    49 
    50 		///////////////////////////////////////////////////////////////////////
    51 		// CommonDialog
    52 
    53 		/// <summary>
    54 		/// Show a command dialog box at the center of the active window.
    55 		/// </summary>
    56 		public static DialogResult ShowDialog(CommonDialog dlg)
    57 		{
    58 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
    59 			DialogResult dlgResult = dlg.ShowDialog();
    60 			centerWindow.Dispose();
    61 			return dlgResult;
    62 		}
    63 
    64 		/// <summary>
    65 		/// Show a command dialog box at the center of the owner window.
    66 		/// </summary>
    67 		public static DialogResult ShowDialog(CommonDialog dlg, IWin32Window owner)
    68 		{
    69 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
    70 			CenterWindow centerWindow = new CenterWindow(handle);
    71 			DialogResult dlgResult = dlg.ShowDialog();
    72 			centerWindow.Dispose();
    73 			return dlgResult;
    74 		}
    75 
    76 		///////////////////////////////////////////////////////////////////////
    77 		// Form
    78 
    79 		/// <summary>
    80 		/// Show a form dialog box at the center of the active window.
    81 		/// </summary>
    82 		public static DialogResult ShowDialog(Form form)
    83 		{
    84 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
    85 			DialogResult dlgResult = form.ShowDialog();
    86 			centerWindow.Dispose();
    87 			return dlgResult;
    88 		}
    89 
    90 		/// <summary>
    91 		/// Show a form dialog box at the center of the owner window.
    92 		/// </summary>
    93 		public static DialogResult ShowDialog(Form form, IWin32Window owner)
    94 		{
    95 			IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
    96 			CenterWindow centerWindow = new CenterWindow(handle);
    97 			DialogResult dlgResult = form.ShowDialog();
    98 			centerWindow.Dispose();
    99 			return dlgResult;
   100 		}
   101 	}
   102 
   103 	#endregion
   104 
   105 	///////////////////////////////////////////////////////////////////////
   106 	#region MsgBox
   107 
   108 	/// <summary>
   109 	/// Class to display a MessageBox centered on the owner.
   110 	/// </summary>
   111 	/// <remarks>
   112 	/// Same methods as the standard MessageBox.
   113 	/// </remarks>
   114 	/// <example>
   115 	/// This example display a "Hello" message box centered on the owner.
   116 	/// <code>
   117 	/// MsgBox.Show("Hello");
   118 	/// </code>
   119 	/// </example>
   120 	public sealed class MsgBox
   121 	{
   122 		private MsgBox() {}	// To remove the constructor from the documentation!
   123 
   124 		///////////////////////////////////////////////////////////////////////
   125 		// text
   126 
   127 		/// <summary>
   128 		/// See MSDN MessageBox() method.
   129 		/// </summary>
   130 		public static DialogResult Show(string text)
   131 		{
   132 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   133 			string caption = Application.ProductName;
   134 			DialogResult dlgResult = MessageBox.Show(text, caption);
   135 			centerWindow.Dispose();
   136 			return dlgResult;
   137 		}
   138 
   139 		/// <summary>
   140 		/// See MSDN MessageBox() method.
   141 		/// </summary>
   142 		public static DialogResult Show(IWin32Window owner, string text)
   143 		{
   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();
   149 			return dlgResult;
   150 		}
   151 
   152 		///////////////////////////////////////////////////////////////////////
   153 		// text, caption
   154 
   155 		/// <summary>
   156 		/// See MSDN MessageBox() method.
   157 		/// </summary>
   158 		public static DialogResult Show(string text, string caption)
   159 		{
   160 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   161 			DialogResult dlgResult = MessageBox.Show(text, caption);
   162 			centerWindow.Dispose();
   163 			return dlgResult;
   164 		}
   165 
   166 		/// <summary>
   167 		/// See MSDN MessageBox() method.
   168 		/// </summary>
   169 		public static DialogResult Show(IWin32Window owner, string text, string caption)
   170 		{
   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();
   175 			return dlgResult;
   176 		}
   177 
   178 		///////////////////////////////////////////////////////////////////////
   179 		// text, caption, buttons
   180 
   181 		/// <summary>
   182 		/// See MSDN MessageBox() method.
   183 		/// </summary>
   184 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
   185 		{
   186 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   187 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons);
   188 			centerWindow.Dispose();
   189 			return dlgResult;
   190 		}
   191 
   192 		/// <summary>
   193 		/// See MSDN MessageBox() method.
   194 		/// </summary>
   195 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
   196 		{
   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();
   201 			return dlgResult;
   202 		}
   203 
   204 		///////////////////////////////////////////////////////////////////////
   205 		// text, caption, buttons, defaultButton
   206 
   207 		/// <summary>
   208 		/// See MSDN MessageBox() method.
   209 		/// </summary>
   210 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
   211 		{
   212 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   213 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon);
   214 			centerWindow.Dispose();
   215 			return dlgResult;
   216 		}
   217 
   218 		/// <summary>
   219 		/// See MSDN MessageBox() method.
   220 		/// </summary>
   221 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
   222 		{
   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();
   227 			return dlgResult;
   228 		}
   229 
   230 		///////////////////////////////////////////////////////////////////////
   231 		// text, caption, buttons, defaultButton, icon
   232 
   233 		/// <summary>
   234 		/// See MSDN MessageBox() method.
   235 		/// </summary>
   236 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
   237 		{
   238 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   239 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton);
   240 			centerWindow.Dispose();
   241 			return dlgResult;
   242 		}
   243 
   244 		/// <summary>
   245 		/// See MSDN MessageBox() method.
   246 		/// </summary>
   247 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
   248 		{
   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();
   253 			return dlgResult;
   254 		}
   255 
   256 		///////////////////////////////////////////////////////////////////////
   257 		// text, caption, buttons, defaultButton, icon, options
   258 
   259 		/// <summary>
   260 		/// See MSDN MessageBox() method.
   261 		/// </summary>
   262 		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
   263 		{
   264 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   265 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
   266 			centerWindow.Dispose();
   267 			return dlgResult;
   268 		}
   269 
   270 		/// <summary>
   271 		/// See MSDN MessageBox() method.
   272 		/// </summary>
   273 		public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
   274 		{
   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();
   279 			return dlgResult;
   280 		}
   281 	}
   282 
   283 	#endregion
   284 
   285 	///////////////////////////////////////////////////////////////////////
   286 	#region AppBox
   287 
   288 	/// <summary>
   289 	/// Class to display a MessageBox centered on the owner.
   290 	/// The MessageBox caption is always Application.ProductName.
   291 	/// </summary>
   292 	/// <remarks>
   293 	/// Same methods as the standard MessageBox without caption.
   294 	/// </remarks>
   295 	/// <example>
   296 	/// This example display an application message box centered on the owner.
   297 	/// <code>
   298 	/// AppBox.Show("Hello");
   299 	/// </code>
   300 	/// </example>
   301 	public sealed class AppBox
   302 	{
   303 		private AppBox() {}	// To remove the constructor from the documentation!
   304 
   305 		///////////////////////////////////////////////////////////////////////
   306 		// text
   307 
   308 		/// <summary>
   309 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   310 		/// </summary>
   311 		public static DialogResult Show(string text)
   312 		{
   313 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   314 			string caption = Application.ProductName;
   315 			DialogResult dlgResult = MessageBox.Show(text, caption);
   316 			centerWindow.Dispose();
   317 			return dlgResult;
   318 		}
   319 
   320 		/// <summary>
   321 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   322 		/// </summary>
   323 		public static DialogResult Show(IWin32Window owner, string text)
   324 		{
   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();
   330 			return dlgResult;
   331 		}
   332 
   333 		///////////////////////////////////////////////////////////////////////
   334 		// text, buttons
   335 
   336 		/// <summary>
   337 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   338 		/// </summary>
   339 		public static DialogResult Show(string text, MessageBoxButtons buttons)
   340 		{
   341 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   342 			string caption = Application.ProductName;
   343 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons);
   344 			centerWindow.Dispose();
   345 			return dlgResult;
   346 		}
   347 
   348 		/// <summary>
   349 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   350 		/// </summary>
   351 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons)
   352 		{
   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();
   358 			return dlgResult;
   359 		}
   360 
   361 		///////////////////////////////////////////////////////////////////////
   362 		// text, buttons, defaultButton
   363 
   364 		/// <summary>
   365 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   366 		/// </summary>
   367 		public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon)
   368 		{
   369 			CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
   370 			string caption = Application.ProductName;
   371 			DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon);
   372 			centerWindow.Dispose();
   373 			return dlgResult;
   374 		}
   375 
   376 		/// <summary>
   377 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   378 		/// </summary>
   379 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon)
   380 		{
   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();
   386 			return dlgResult;
   387 		}
   388 
   389 		///////////////////////////////////////////////////////////////////////
   390 		// text, buttons, defaultButton, icon
   391 
   392 		/// <summary>
   393 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   394 		/// </summary>
   395 		public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
   396 		{
   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();
   401 			return dlgResult;
   402 		}
   403 
   404 		/// <summary>
   405 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   406 		/// </summary>
   407 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
   408 		{
   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();
   414 			return dlgResult;
   415 		}
   416 
   417 		///////////////////////////////////////////////////////////////////////
   418 		// text, buttons, defaultButton, icon, options
   419 
   420 		/// <summary>
   421 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   422 		/// </summary>
   423 		public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
   424 		{
   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();
   429 			return dlgResult;
   430 		}
   431 
   432 		/// <summary>
   433 		/// See MSDN MessageBox() method. Caption is Application.ProductName.
   434 		/// </summary>
   435 		public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
   436 		{
   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();
   442 			return dlgResult;
   443 		}
   444 	}
   445 
   446 	#endregion
   447 
   448 	///////////////////////////////////////////////////////////////////////
   449 	#region ErrBox
   450 
   451 	/// <summary>
   452 	/// Class to display application error MessageBox centered on the owner.
   453 	/// The caption of the MessageBox is Application.ProductName.
   454 	/// </summary>
   455 	/// <example>
   456 	/// This example display an error message box centered on the owner.
   457 	/// <code>
   458 	/// ErrBox.Show(ex);
   459 	/// </code>
   460 	/// </example>
   461 	public sealed class ErrBox
   462 	{
   463 		private ErrBox() {}	// To remove the constructor from the documentation!
   464 
   465 		/// <summary>
   466 		/// Show an error MessageBox with an icon error and an OK button.
   467 		/// </summary>
   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)
   472 		{
   473 			string caption = Application.ProductName;
   474 			return MsgBox.Show(owner, err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
   475 		}
   476 
   477 		/// <summary>
   478 		/// Show an error MessageBox with an icon error and an OK button.
   479 		/// </summary>
   480 		/// <param name="err">The error message.</param>
   481 		/// <returns>Dialog result of the MessageBox.</returns>
   482 		public static DialogResult Show(string err)
   483 		{
   484 			string caption = Application.ProductName;
   485 			return MsgBox.Show(err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
   486 		}
   487 
   488 		/// <summary>
   489 		/// Show an error MessageBox with exception message, an icon error and an OK button.
   490 		/// </summary>
   491 		/// <param name="ex">Exception to be displayed.</param>
   492 		/// <returns>Dialog result of the MessageBox.</returns>
   493 		public static DialogResult Show(Exception ex)
   494 		{
   495 			string err = ex.Message;
   496 			while (ex.InnerException != null)
   497 			{
   498 				ex = ex.InnerException;
   499 				err += Environment.NewLine;
   500 				err += ex.Message;
   501 			}
   502 			string caption = Application.ProductName;
   503 			return MsgBox.Show(err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
   504 		}
   505 
   506 		/// <summary>
   507 		/// Show a specialized error MessageBox centered into the parent owner.
   508 		/// </summary>
   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)
   513 		{
   514 			if (debugMode)
   515 				return Show(ex);
   516 			else
   517 				return Show(ex.Message);
   518 		}
   519 	}
   520 
   521 	#endregion
   522 
   523 	///////////////////////////////////////////////////////////////////////
   524 	#region CenterWindow class
   525 
   526 	internal sealed class CenterWindow
   527 	{
   528 		public IntPtr hOwner = IntPtr.Zero;
   529 		private Rectangle rect;
   530 
   531 		public CbtHook cbtHook = null;
   532 		public WndProcRetHook wndProcRetHook = null;
   533 
   534 		public CenterWindow(IntPtr hOwner)
   535 		{
   536 			this.hOwner = hOwner;
   537 			this.cbtHook = new CbtHook();
   538 			cbtHook.WindowActivate += new CbtHook.CbtEventHandler(WndActivate);
   539 			cbtHook.Install();
   540 		}
   541 
   542 		public void Dispose()
   543 		{
   544 			if (wndProcRetHook != null)
   545 			{
   546 				wndProcRetHook.Uninstall();
   547 				wndProcRetHook = null;
   548 			}
   549 			if (cbtHook != null)
   550 			{
   551 				cbtHook.Uninstall();
   552 				cbtHook = null;
   553 			}
   554 		}
   555 
   556 		public void WndActivate(object sender, CbtEventArgs e)
   557 		{
   558 			IntPtr hMsgBox = e.wParam;
   559 
   560 			// try to find a howner for this message box
   561 			if (hOwner == IntPtr.Zero)
   562 				hOwner = USER32.GetActiveWindow();
   563 
   564 			// get the MessageBox window rect
   565 			RECT rectDlg = new RECT();
   566 			USER32.GetWindowRect(hMsgBox, ref rectDlg);
   567 
   568 			// get the owner window rect
   569 			RECT rectForm = new RECT();
   570 			USER32.GetWindowRect(hOwner, ref rectForm);
   571 
   572 			// get the biggest screen area
   573 			Rectangle rectScreen = API.TrueScreenRect;
   574 
   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;
   580 
   581 			// center on parent
   582 			int dx = ((rectDlg.left + rectDlg.right) - (rectForm.left + rectForm.right)) / 2;
   583 			int dy = ((rectDlg.top + rectDlg.bottom) - (rectForm.top + rectForm.bottom)) / 2;
   584 
   585 			rect = new Rectangle(
   586 				rectDlg.left - dx,
   587 				rectDlg.top - dy,
   588 				rectDlg.right - rectDlg.left,
   589 				rectDlg.bottom - rectDlg.top);
   590 
   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);
   596 
   597 			if (e.IsDialog)
   598 			{
   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();
   603 			}
   604 			else
   605 				USER32.MoveWindow(hMsgBox, rect.Left, rect.Top, rect.Width, rect.Height, 1);
   606 
   607 			// uninstall this hook
   608 			WindowsHook wndHook = (WindowsHook)sender;
   609 			Debug.Assert(cbtHook == wndHook);
   610 			cbtHook.Uninstall();
   611 			cbtHook = null;
   612 		}
   613 
   614 		public void WndProcRet(object sender, WndProcRetEventArgs e)
   615 		{
   616 			if (e.cw.message == WndMessage.WM_INITDIALOG ||
   617 				e.cw.message == WndMessage.WM_UNKNOWINIT)
   618 			{
   619 				USER32.MoveWindow(e.cw.hwnd, rect.Left, rect.Top, rect.Width, rect.Height, 1);
   620 				
   621 				// uninstall this hook
   622 				WindowsHook wndHook = (WindowsHook)sender;
   623 				Debug.Assert(wndProcRetHook == wndHook);
   624 				wndProcRetHook.Uninstall();
   625 				wndProcRetHook = null;
   626 			}
   627 		}
   628 	}
   629 	#endregion
   630 }