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