Getting the active window handle
-
I would like to know how to get the handle of the currently active window. I've tried using Findwindow and it works, but i want the whole view of the active window (including the toolbar and menubar) as i would like to create a program to take a screenshot of the active window (something like ALT+PRINTSCREEN but using BitBlt to do the capturing).
-
I would like to know how to get the handle of the currently active window. I've tried using Findwindow and it works, but i want the whole view of the active window (including the toolbar and menubar) as i would like to create a program to take a screenshot of the active window (something like ALT+PRINTSCREEN but using BitBlt to do the capturing).
I'm not sure what problems you're having, but the typical way is similar to the following pseudo code:
HWND hWnd = FindWindow(NULL, "MyApp");
RECT rc;
GetClientRect(hWnd, &rc);
HDC hDC = GetDC(hWnd);
HDC hCDC = CreateCompatibleDC(hDC);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
BitBlt(hCDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY | CAPTUREBLT);Is this what you're doing (roughly)?
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I'm not sure what problems you're having, but the typical way is similar to the following pseudo code:
HWND hWnd = FindWindow(NULL, "MyApp");
RECT rc;
GetClientRect(hWnd, &rc);
HDC hDC = GetDC(hWnd);
HDC hCDC = CreateCompatibleDC(hDC);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
BitBlt(hCDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY | CAPTUREBLT);Is this what you're doing (roughly)?
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Yeah more or less. Here's the exact code : hWnd = FindWindow(null, "Form1"); RECT rc = new RECT(); GetWindowRect(hWnd, ref rc); this.Text = "LEFT" + rc.left + " RIGHT " + rc.right + " TOP " + rc.top; Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics gr1 = Graphics.FromImage(myImage); IntPtr dc1 = gr1.GetHdc(); IntPtr dc2 = GetDC(hWnd); BitBlt(dc1, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, dc2, 0, 0, 13369376); gr1.ReleaseHdc(dc1); ReleaseDC(GetDC(hWnd)), dc2); GC.Collect(); Graphics gr = Graphics.FromHwnd(pictureBox1.Handle); pictureBox1.Image = myImage;