11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12. Screen Capturing & Printing<br />

(<br />

)<br />

{<br />

pWnd->GetSafeHwnd() != GetSafeHwnd() &&<br />

pWnd->GetSafeHwnd() != AfxGetApp()->m_pMainWnd->GetSafeHwnd()<br />

if(pWnd->GetSafeHwnd() != m_hWnd)<br />

{<br />

if(m_rectSelect.IsRectEmpty() != TRUE)<br />

{<br />

dc.Rectangle(m_rectSelect);<br />

}<br />

pWnd->GetWindowRect(m_rectSelect);<br />

dc.Rectangle(m_rectSelect);<br />

m_hWnd=pWnd->GetSafeHwnd();<br />

}<br />

}<br />

else<br />

{<br />

if(m_rectSelect.IsRectEmpty() != TRUE)<br />

{<br />

dc.Rectangle(m_rectSelect);<br />

m_rectSelect=CRect(0, 0, 0, 0);<br />

}<br />

m_hWnd=NULL;<br />

}<br />

if(bErase == TRUE && m_rectSelect.IsRectEmpty() != TRUE)<br />

{<br />

dc.Rectangle(m_rectSelect);<br />

m_rectSelect=CRect(0, 0, 0, 0);<br />

}<br />

}<br />

}<br />

dc.SetROP2(nRop2Mode);<br />

dc.SelectClipRgn(NULL);<br />

New Command<br />

After the user picks up a window, the dialog box will be closed. So we need to store the handle of<br />

selected window (Contained in CSelDlg::m_hWnd) so that it can be used when making snapshot. In the<br />

sample, variable CGDIDoc::m_hWnd is declared for this purpose. Also, function CGDIDoc::<br />

GetSelectedWnd() is implemented to allow this handle be accessed from the view. In the sample, a new<br />

command Capture | Settings is added to menu IDR_MAINFRAME, it is handled in function CGDIDoc::<br />

OnCaptureSetting(). Within the function, window selection dialog box is implemented to let the user<br />

select a window, if the user clicks OK button, the selected window’s handle will be saved to variable<br />

CGDIDoc::m_hWnd:<br />

void CGDIDoc::OnCaptureSetting()<br />

{<br />

CSelDlg dlg;<br />

}<br />

if(dlg.DoModal() == IDOK)<br />

{<br />

m_hWnd=dlg.GetSelectedWnd();<br />

}<br />

In function CGDIView::Capture(), we need to first obtain handle CGDIDoc::m_hWnd from the<br />

document, if it is not a valid window handle, we still capture the whole desktop screen. Otherwise we use<br />

this handle to find out the rectangular area of the window and make the snapshot.<br />

378

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

Saved successfully!

Ooh no, something went wrong!