Screen Capturing via GDI+ and GDI - GDI’s Role in Screen Capturing
(Page 2 of 5 )
On the whole, capturing the screen requires two crucial techniques: one is to obtain the handler of the window in which your picture lies (i.e. making clear what to capture). The other is to persist the captured image. This is the strong point of GDI+.
As for the first technique, we can resort to the SetCapture() function to track the mouse moving opertion. During the movement of the mouse, the SetCapture() function can obtain the handler of the window that the mouse points to. Moreover, we can also use the WindowFromPoint() function to find the corresponding window's handler.
Readers who are familiar with SnagIT know that when capturing some window there is usually a red rectangle around the target window to remind users of the selected window. This functionality is more complex than the former. Next, I’ll show you how to draw a red rectangle using GDI related techniques.
A very fundamental concept in GDI is the device context (DC), which is what is owned by each window. Suppose the DC of a window is retrieved. Users can render at any part of the window. However, in a typical screen capture application, the target window that users select is uncertain. Therefore, it is not that easy for developers to obtain the DC of the window at the location of the mouse cursor. In fact, the key to this lies in the GetDC() function. The signature is given below:
HDC GetDC(HWND hWnd);
The hWnd parameter corresponds to the handler of the window that’s related to the current DC object. Note that when hWnd is null, the GetDC() functionreturns the handler of the device context of the screen. This means that developers can draw anything at any position on the screen.
The purpose of drawing is to remind users of the currently-selected window. So when drawing, we have to make sure we don't destroy the original content. In such a case, we can set the render mode of the window to RS_NOTXORPEN. First, this makes the paint brush color XOR computed with the screen color, and then NOT computed with the screen color. The same pixel will stay unaltered after being computed via RS_NOTXORPEN twice. In this way, we can draw in the RS_NOTXORPEN node twice, because the window will stay unchanged.
Author's Note: all the above requirements are rather difficult to accomplish via GDI+.
Next: Developing a Screen Capture Application >>
More Windows Scripting Articles
More By Xianzhong Zhu