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 11. Sample: Simple Paint<br />

……<br />

);<br />

::SetDIBits<br />

(<br />

dc.GetSafeHdc(),<br />

(HBITMAP)m_bmpSelBackup.GetSafeHandle(),<br />

0,<br />

lpBi->bmiHeader.biHeight,<br />

(LPVOID)<br />

(<br />

(LPSTR)lpBi+<br />

sizeof(BITMAPINFOHEADER)+<br />

pDoc->GetColorTableSize<br />

(<br />

lpBi->bmiHeader.biBitCount<br />

)*sizeof(RGBQUAD)<br />

),<br />

lpBi,<br />

DIB_RGB_COLORS<br />

);<br />

When calling function ::SetDIBits(…), we need to provide the handle of client window (to its first<br />

parameter). This is because the image will finally be put to the client window. Also we need to provide the<br />

handle of target bitmap image (to the second parameter). The third and fourth parameters of this function<br />

specify the first raster line and total number of raster lines in the target bitmap respectively. The fifth<br />

parameter is a pointer to the source DIB bit values (stored as an array of bytes). The sixth parameter is a<br />

pointer to bitmap header, and final parameter specifis how to use the palette. Since we want the DIB bits to<br />

indicate the color table contained in the DIB data, we should choose DIB_RGB_COLORS flag.<br />

After copying the image, we need to enable the tracker, backup the current bitmap, copy the new<br />

bitmap to the area specified by tracker rectangle, and set the current drawing tool to rectangular selection.<br />

Then, we need to update the client window. The following portion of function CGDIView::PasteDIB(…)<br />

shows how these are implemented:<br />

……<br />

……<br />

m_trackerSel.m_rect=CRect<br />

(<br />

0,<br />

0,<br />

nRatio*lpBi->bmiHeader.biWidth,<br />

nRatio*lpBi->bmiHeader.biHeight<br />

);<br />

BackupCurrentBmp();<br />

StretchCopySelection();<br />

Invalidate();<br />

::GlobalUnlock(hData);<br />

((CMainFrame *)(AfxGetApp()->m_pMainWnd))->SendMessage<br />

(<br />

WM_COMMAND, ID_BUTTON_RECTSEL, 0<br />

);<br />

Because the rectangular selection button is not actually pressed by the user, we need to generate a<br />

WM_COMMAND message in the program and send it to the mainframe window. When doing this, WPARAM<br />

parameter of the message is set to the ID of rectangle selection command. This will have the same effect<br />

with clicking the rectangular selection button using the mouse.<br />

With the above implementation, we can execute cut, copy and paste commands now. Please note that<br />

these commands work correctly only if the currently loaded image is 256-color format. For the other<br />

formats, error message may be generated.<br />

11.10 Palette Change & Flickering<br />

Sample 11.10\GDI is based on sample 11.9\GDI.<br />

365

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

Saved successfully!

Ooh no, something went wrong!