sl@0: //=============================================================================
sl@0: // COPYRIGHT: Prosoft-Lanz
sl@0: //=============================================================================
sl@0: //
sl@0: // $Workfile: DialogBox.cs $
sl@0: //
sl@0: // PROJECT : CodeProject Components
sl@0: // VERSION : 1.00
sl@0: // CREATION : 19.02.2003
sl@0: // AUTHOR : JCL
sl@0: //
sl@0: // DETAILS : DialogBoxes centered into the parent owner.
sl@0: // This class implement the following objects:
sl@0: //
sl@0: // DlgBox.ShowDialog(...) for CommonDialog and Form
sl@0: // MsgBox.Show(...) for standard MessageBox
sl@0: // AppBox.Show(...) for standard MessageBox with ProductName as caption
sl@0: // ErrBox.Show(...) for standard error MessageBox
sl@0: //
sl@0: //-----------------------------------------------------------------------------
sl@0: using System;
sl@0: using System.Drawing;
sl@0: using System.Windows.Forms;
sl@0: using System.Runtime.InteropServices;
sl@0: using System.Diagnostics;
sl@0:
sl@0: using CodeProject.Win32API;
sl@0: using CodeProject.Win32API.Hook;
sl@0:
sl@0: namespace CodeProject.Dialog
sl@0: {
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region DlgBox
sl@0:
sl@0: ///
sl@0: /// Class to display a CommonDialog or modal Form centered on the owner.
sl@0: ///
sl@0: ///
sl@0: /// This example display the default print dialog box in the center of the parent.
sl@0: ///
sl@0: /// PrintDialog printDlg = new PrintDialog();
sl@0: /// if (DlgBox.ShowDialog(printDlg, parent) == DialogResult.OK)
sl@0: /// printDocument.Print();
sl@0: ///
sl@0: ///
sl@0: public sealed class DlgBox
sl@0: {
sl@0: private DlgBox() {} // To remove the constructor from the documentation!
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // CommonDialog
sl@0:
sl@0: ///
sl@0: /// Show a command dialog box at the center of the active window.
sl@0: ///
sl@0: public static DialogResult ShowDialog(CommonDialog dlg)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = dlg.ShowDialog();
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Show a command dialog box at the center of the owner window.
sl@0: ///
sl@0: public static DialogResult ShowDialog(CommonDialog dlg, IWin32Window owner)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = dlg.ShowDialog();
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // Form
sl@0:
sl@0: ///
sl@0: /// Show a form dialog box at the center of the active window.
sl@0: ///
sl@0: public static DialogResult ShowDialog(Form form)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = form.ShowDialog();
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Show a form dialog box at the center of the owner window.
sl@0: ///
sl@0: public static DialogResult ShowDialog(Form form, IWin32Window owner)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = form.ShowDialog();
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0: }
sl@0:
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region MsgBox
sl@0:
sl@0: ///
sl@0: /// Class to display a MessageBox centered on the owner.
sl@0: ///
sl@0: ///
sl@0: /// Same methods as the standard MessageBox.
sl@0: ///
sl@0: ///
sl@0: /// This example display a "Hello" message box centered on the owner.
sl@0: ///
sl@0: /// MsgBox.Show("Hello");
sl@0: ///
sl@0: ///
sl@0: public sealed class MsgBox
sl@0: {
sl@0: private MsgBox() {} // To remove the constructor from the documentation!
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(string text)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, caption
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(string text, string caption)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, string caption)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, caption, buttons
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, caption, buttons, defaultButton
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, caption, buttons, defaultButton, icon
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, caption, buttons, defaultButton, icon, options
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0: }
sl@0:
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region AppBox
sl@0:
sl@0: ///
sl@0: /// Class to display a MessageBox centered on the owner.
sl@0: /// The MessageBox caption is always Application.ProductName.
sl@0: ///
sl@0: ///
sl@0: /// Same methods as the standard MessageBox without caption.
sl@0: ///
sl@0: ///
sl@0: /// This example display an application message box centered on the owner.
sl@0: ///
sl@0: /// AppBox.Show("Hello");
sl@0: ///
sl@0: ///
sl@0: public sealed class AppBox
sl@0: {
sl@0: private AppBox() {} // To remove the constructor from the documentation!
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(string text)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, buttons
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(string text, MessageBoxButtons buttons)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, buttons, defaultButton
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, buttons, defaultButton, icon
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: // text, buttons, defaultButton, icon, options
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
sl@0: {
sl@0: CenterWindow centerWindow = new CenterWindow(IntPtr.Zero);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(text, caption, buttons, icon, defaultButton, options);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0:
sl@0: ///
sl@0: /// See MSDN MessageBox() method. Caption is Application.ProductName.
sl@0: ///
sl@0: public static DialogResult Show(IWin32Window owner, string text, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
sl@0: {
sl@0: IntPtr handle = (owner == null) ? IntPtr.Zero: owner.Handle;
sl@0: CenterWindow centerWindow = new CenterWindow(handle);
sl@0: string caption = Application.ProductName;
sl@0: DialogResult dlgResult = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options);
sl@0: centerWindow.Dispose();
sl@0: return dlgResult;
sl@0: }
sl@0: }
sl@0:
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region ErrBox
sl@0:
sl@0: ///
sl@0: /// Class to display application error MessageBox centered on the owner.
sl@0: /// The caption of the MessageBox is Application.ProductName.
sl@0: ///
sl@0: ///
sl@0: /// This example display an error message box centered on the owner.
sl@0: ///
sl@0: /// ErrBox.Show(ex);
sl@0: ///
sl@0: ///
sl@0: public sealed class ErrBox
sl@0: {
sl@0: private ErrBox() {} // To remove the constructor from the documentation!
sl@0:
sl@0: ///
sl@0: /// Show an error MessageBox with an icon error and an OK button.
sl@0: ///
sl@0: /// The error message.
sl@0: /// The owner of the error MessageBox.
sl@0: /// Dialog result of the MessageBox.
sl@0: public static DialogResult Show(IWin32Window owner, string err)
sl@0: {
sl@0: string caption = Application.ProductName;
sl@0: return MsgBox.Show(owner, err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Show an error MessageBox with an icon error and an OK button.
sl@0: ///
sl@0: /// The error message.
sl@0: /// Dialog result of the MessageBox.
sl@0: public static DialogResult Show(string err)
sl@0: {
sl@0: string caption = Application.ProductName;
sl@0: return MsgBox.Show(err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Show an error MessageBox with exception message, an icon error and an OK button.
sl@0: ///
sl@0: /// Exception to be displayed.
sl@0: /// Dialog result of the MessageBox.
sl@0: public static DialogResult Show(Exception ex)
sl@0: {
sl@0: string err = ex.Message;
sl@0: while (ex.InnerException != null)
sl@0: {
sl@0: ex = ex.InnerException;
sl@0: err += Environment.NewLine;
sl@0: err += ex.Message;
sl@0: }
sl@0: string caption = Application.ProductName;
sl@0: return MsgBox.Show(err, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Show a specialized error MessageBox centered into the parent owner.
sl@0: ///
sl@0: /// Exception to be displayed.
sl@0: /// true to display the full informations else false.
sl@0: /// Dialog result of the MessageBox.
sl@0: public static DialogResult Show(Exception ex, bool debugMode)
sl@0: {
sl@0: if (debugMode)
sl@0: return Show(ex);
sl@0: else
sl@0: return Show(ex.Message);
sl@0: }
sl@0: }
sl@0:
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region CenterWindow class
sl@0:
sl@0: internal sealed class CenterWindow
sl@0: {
sl@0: public IntPtr hOwner = IntPtr.Zero;
sl@0: private Rectangle rect;
sl@0:
sl@0: public CbtHook cbtHook = null;
sl@0: public WndProcRetHook wndProcRetHook = null;
sl@0:
sl@0: public CenterWindow(IntPtr hOwner)
sl@0: {
sl@0: this.hOwner = hOwner;
sl@0: this.cbtHook = new CbtHook();
sl@0: cbtHook.WindowActivate += new CbtHook.CbtEventHandler(WndActivate);
sl@0: cbtHook.Install();
sl@0: }
sl@0:
sl@0: public void Dispose()
sl@0: {
sl@0: if (wndProcRetHook != null)
sl@0: {
sl@0: wndProcRetHook.Uninstall();
sl@0: wndProcRetHook = null;
sl@0: }
sl@0: if (cbtHook != null)
sl@0: {
sl@0: cbtHook.Uninstall();
sl@0: cbtHook = null;
sl@0: }
sl@0: }
sl@0:
sl@0: public void WndActivate(object sender, CbtEventArgs e)
sl@0: {
sl@0: IntPtr hMsgBox = e.wParam;
sl@0:
sl@0: // try to find a howner for this message box
sl@0: if (hOwner == IntPtr.Zero)
sl@0: hOwner = USER32.GetActiveWindow();
sl@0:
sl@0: // get the MessageBox window rect
sl@0: RECT rectDlg = new RECT();
sl@0: USER32.GetWindowRect(hMsgBox, ref rectDlg);
sl@0:
sl@0: // get the owner window rect
sl@0: RECT rectForm = new RECT();
sl@0: USER32.GetWindowRect(hOwner, ref rectForm);
sl@0:
sl@0: // get the biggest screen area
sl@0: Rectangle rectScreen = API.TrueScreenRect;
sl@0:
sl@0: // if no parent window, center on the primary screen
sl@0: if (rectForm.right == rectForm.left)
sl@0: rectForm.right = rectForm.left = Screen.PrimaryScreen.WorkingArea.Width / 2;
sl@0: if (rectForm.bottom == rectForm.top)
sl@0: rectForm.bottom = rectForm.top = Screen.PrimaryScreen.WorkingArea.Height / 2;
sl@0:
sl@0: // center on parent
sl@0: int dx = ((rectDlg.left + rectDlg.right) - (rectForm.left + rectForm.right)) / 2;
sl@0: int dy = ((rectDlg.top + rectDlg.bottom) - (rectForm.top + rectForm.bottom)) / 2;
sl@0:
sl@0: rect = new Rectangle(
sl@0: rectDlg.left - dx,
sl@0: rectDlg.top - dy,
sl@0: rectDlg.right - rectDlg.left,
sl@0: rectDlg.bottom - rectDlg.top);
sl@0:
sl@0: // place in the screen
sl@0: if (rect.Right > rectScreen.Right) rect.Offset(rectScreen.Right - rect.Right, 0);
sl@0: if (rect.Bottom > rectScreen.Bottom) rect.Offset(0, rectScreen.Bottom - rect.Bottom);
sl@0: if (rect.Left < rectScreen.Left) rect.Offset(rectScreen.Left - rect.Left, 0);
sl@0: if (rect.Top < rectScreen.Top) rect.Offset(0, rectScreen.Top - rect.Top);
sl@0:
sl@0: if (e.IsDialog)
sl@0: {
sl@0: // do the job when the WM_INITDIALOG message returns
sl@0: wndProcRetHook = new WndProcRetHook(hMsgBox);
sl@0: wndProcRetHook.WndProcRet += new WndProcRetHook.WndProcEventHandler(WndProcRet);
sl@0: wndProcRetHook.Install();
sl@0: }
sl@0: else
sl@0: USER32.MoveWindow(hMsgBox, rect.Left, rect.Top, rect.Width, rect.Height, 1);
sl@0:
sl@0: // uninstall this hook
sl@0: WindowsHook wndHook = (WindowsHook)sender;
sl@0: Debug.Assert(cbtHook == wndHook);
sl@0: cbtHook.Uninstall();
sl@0: cbtHook = null;
sl@0: }
sl@0:
sl@0: public void WndProcRet(object sender, WndProcRetEventArgs e)
sl@0: {
sl@0: if (e.cw.message == WndMessage.WM_INITDIALOG ||
sl@0: e.cw.message == WndMessage.WM_UNKNOWINIT)
sl@0: {
sl@0: USER32.MoveWindow(e.cw.hwnd, rect.Left, rect.Top, rect.Width, rect.Height, 1);
sl@0:
sl@0: // uninstall this hook
sl@0: WindowsHook wndHook = (WindowsHook)sender;
sl@0: Debug.Assert(wndProcRetHook == wndHook);
sl@0: wndProcRetHook.Uninstall();
sl@0: wndProcRetHook = null;
sl@0: }
sl@0: }
sl@0: }
sl@0: #endregion
sl@0: }