11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12. Screen Capturing & Printing<br />

Picking Up a Window<br />

Often there are many windows contained in the desktop window. Each application may have one<br />

mainframe window and several child windows. To let the user pick up a window with mouse clicking, we<br />

must find a way to detect if there is a window under the current mouse cursor.<br />

We can call function CWnd::WindowFromPoint(…) to retrieve the window under current mouse cursor.<br />

If there is such a window, its handle will be returned by this function. Otherwise the function will return<br />

NULL.<br />

We also need to monitor mouse movement and respond to its activities when the user is selecting a<br />

window. We all know that this can be implemented by handling mouse related messages: WM_LBUTTONDOWN,<br />

WM_MOUSEMOVE, and WM_LBUTTONUP. Also we must be able to receive these messages even if the mouse<br />

cursor is outside our application window. To implement this, we need to set window capture. By doing so,<br />

the user can hold the left button and move it anywhere to pick up window. As long as the left button is held<br />

down, we can receive WM_MOUSEMOVE messages (The capture will be released by the system if the left button<br />

is released).<br />

Another issue is how to indicate the selected window. To make the application easier to use, we need<br />

to put some indication on the window that is being selected. From the previous section we know that the<br />

DC of the desktop window can be obtained and used to draw anything anywhere on the screen. With the<br />

desktop window DC, we can reverse the selected window when the mouse cursor is over it, and resume it<br />

as the mouse cursor leaves the window.<br />

The drawing mode can be set by calling function CDC::SetROP2(…) using R2_NOT flag. After this if we<br />

draw a rectangle (By calling function CDC::Rectangle(…)) using the window’s position and size, the whole<br />

window will be reversed. Calling this function twice using the same parameter will resume the original<br />

window.<br />

One thing we must pay attention to is that the application itself also has a mainframe window, and<br />

therefore will be selected for capturing if the mouse cursor is over it. This is not a desirable feature. To<br />

solve this problem, after function CWnd::WindowFromPoint(…) is called, we must check if the window<br />

returned by this function belongs to the application itself.<br />

Because the windows can overlap one another, when the mouse cursor is over one window, we should<br />

not reverse the overlapped part (Figure 11-2). To solve this problem, we need to use region. We can create<br />

a region that contains only the non-overlapped part of a window, which can be selected by the desktop<br />

window DC. By doing this, the overlapped portion of the window will not be reversed when the function<br />

CDC::Rectangle(…) is called.<br />

Window A<br />

Window B<br />

If mouse cursor is over window A,<br />

we do not want to reverse the part<br />

that is overlapped by window B<br />

Figure 11-2. Reverse a window to indicate that it can be selected for capturing<br />

After the user has selected a specific window and executed Capture | Go! command, we can find out<br />

the size and position of this window, start timer, call function CDC::BitBlt(…) to make a snapshot of the<br />

window and store the data to the memory bitmap.<br />

Dialog Box IDD_DIALOG_SELECT<br />

Sample 12.2\GDI is based on sample 12.1\GDI. The new sample allows the user to select a specified<br />

window for making snapshot. First a dialog box is added to the application, it will be used to let the user<br />

375

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!