Exemple #1
0
 public CenterWindow(IntPtr hOwner)
 {
     this.hOwner             = hOwner;
     this.cbtHook            = new CbtHook();
     cbtHook.WindowActivate += new CbtHook.CbtEventHandler(WndActivate);
     cbtHook.Install();
 }
Exemple #2
0
 public void Dispose()
 {
     if (wndProcRetHook != null)
     {
         wndProcRetHook.Uninstall();
         wndProcRetHook = null;
     }
     if (cbtHook != null)
     {
         cbtHook.Uninstall();
         cbtHook = null;
     }
 }
Exemple #3
0
        public void WndActivate(object sender, CbtEventArgs e)
        {
            IntPtr hMsgBox = e.wParam;

            // try to find a howner for this message box
            if (hOwner == IntPtr.Zero)
            {
                hOwner = Win32.Function.GetActiveWindow();
            }

            // get the MessageBox window rect
            Win32.RECT rectDlg = new Win32.RECT();
            Win32.Function.GetWindowRect(hMsgBox, ref rectDlg);

            // get the owner window rect
            Win32.RECT rectForm = new Win32.RECT();
            Win32.Function.GetWindowRect(hOwner, ref rectForm);

            // get the biggest screen area
            Rectangle rectScreen = TrueScreenRect;

            // if no parent window, center on the primary screen
            if (rectForm.right == rectForm.left)
            {
                rectForm.right = rectForm.left = Screen.PrimaryScreen.WorkingArea.Width / 2;
            }
            if (rectForm.bottom == rectForm.top)
            {
                rectForm.bottom = rectForm.top = Screen.PrimaryScreen.WorkingArea.Height / 2;
            }

            // center on parent
            int dx = ((rectDlg.left + rectDlg.right) - (rectForm.left + rectForm.right)) / 2;
            int dy = ((rectDlg.top + rectDlg.bottom) - (rectForm.top + rectForm.bottom)) / 2;

            rect = new Rectangle(
                rectDlg.left - dx,
                rectDlg.top - dy,
                rectDlg.right - rectDlg.left,
                rectDlg.bottom - rectDlg.top);

            // place in the screen
            if (rect.Right > rectScreen.Right)
            {
                rect.Offset(rectScreen.Right - rect.Right, 0);
            }
            if (rect.Bottom > rectScreen.Bottom)
            {
                rect.Offset(0, rectScreen.Bottom - rect.Bottom);
            }
            if (rect.Left < rectScreen.Left)
            {
                rect.Offset(rectScreen.Left - rect.Left, 0);
            }
            if (rect.Top < rectScreen.Top)
            {
                rect.Offset(0, rectScreen.Top - rect.Top);
            }

            if (e.IsDialog)
            {
                // do the job when the WM_INITDIALOG message returns
                wndProcRetHook             = new WndProcRetHook(hMsgBox);
                wndProcRetHook.WndProcRet += new WndProcRetHook.WndProcEventHandler(WndProcRet);
                wndProcRetHook.Install();
            }
            else
            {
                Win32.Function.MoveWindow(hMsgBox, rect.Left, rect.Top, rect.Width, rect.Height, 1);
            }

            // uninstall this hook
            WindowsHook wndHook = (WindowsHook)sender;

            Debug.Assert(cbtHook == wndHook);
            cbtHook.Uninstall();
            cbtHook = null;
        }