Пример #1
0
        /// <summary>
        /// Takes the snapshot of the window owned by a process
        /// </summary>
        /// <param name="name">The process name</param>
        /// <returns>Bitmap object</returns>
        public static Bitmap ProcessSnapshot(string name)
        {
            IntPtr hwnd = IntPtr.Zero;

            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName(name))
            {
                if (p.MainWindowHandle != IntPtr.Zero)
                {
                    hwnd = p.MainWindowHandle;
                    break;
                }
            }

            if (hwnd == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                USER32.RECT rc;
                USER32.GetWindowRect(hwnd, out rc);
                Rectangle r      = ApiConverter.ToRectangle(rc);
                Bitmap    bitmap = new Bitmap(r.Width, r.Height, PixelFormat.Format24bppRgb);
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    IntPtr hdcBitmap = g.GetHdc();
                    USER32.PrintWindow(hwnd, hdcBitmap, 0);
                }
                return(bitmap);
            }
        }
Пример #2
0
 internal static Rectangle GetWindowRectangle(IntPtr hWnd)
 {
     USER32.Rect rect = new USER32.Rect();
     if (USER32.GetWindowRect(hWnd, ref rect))
     {
         return(rect.ToRectangle());
     }
     else
     {
         return(new Rectangle());
     }
 }
Пример #3
0
 /// <summary>
 /// Takes a snaphot of a window
 /// </summary>
 /// <param name="hwnd">Handle to the window</param>
 /// <returns>Bitmap object</returns>
 public static Bitmap WindowSnapshot(IntPtr hwnd)
 {
     if (hwnd == IntPtr.Zero)
     {
         return(null);
     }
     else
     {
         USER32.RECT rc;
         USER32.GetWindowRect(hwnd, out rc);
         Rectangle r      = ApiConverter.ToRectangle(rc);
         Bitmap    bitmap = new Bitmap(r.Width, r.Height, PixelFormat.Format24bppRgb);
         using (Graphics g = Graphics.FromImage(bitmap))
         {
             IntPtr hdcBitmap = g.GetHdc();
             USER32.PrintWindow(hwnd, hdcBitmap, 0);
         }
         return(bitmap);
     }
 }
Пример #4
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 = USER32.GetActiveWindow();
            }

            // get the MessageBox window rect
            RECT rectDlg = new RECT();

            USER32.GetWindowRect(hMsgBox, ref rectDlg);

            // get the owner window rect
            RECT rectForm = new RECT();

            USER32.GetWindowRect(hOwner, ref rectForm);

            // get the biggest screen area
            Rectangle rectScreen = API.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
            {
                USER32.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;
        }