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 8. DC, Pen, Brush and Palette<br />

void CGDIView::OnDraw(CDC *pDC)<br />

{<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

CRect rect;<br />

int i;<br />

}<br />

CGDIDoc* pDoc = GetDocument();<br />

ASSERT_VALID(pDoc);<br />

GetClientRect(rect);<br />

rect.bottom+=255;<br />

rect.bottom/=256;<br />

for(i=0; iSelectObject(&pen);<br />

pDC->MoveTo(0, rect.top+rect.Height()/2);<br />

pDC->LineTo(rect.right, rect.top);<br />

pDC->SelectObject(ptrPenOld);<br />

pen.DeleteObject();<br />

rect.OffsetRect(0, rect.Height());<br />

}<br />

Instead of creating brushes, solid pens are created to paint the client window area. The width of these<br />

pens is the same with the height of each row, and their colors change gradually from dark blue to bright<br />

blue. Within each loop, before calling functions to draw a line, we select the pen into DC, after the line is<br />

drawn, we select the pen out of the DC. Because a new pen is created in each loop, function<br />

CPen::DeleteObject() is called at the end of the loop. This will destroy the pen so that variable pen can be<br />

initialized again.<br />

Adjusting Display Settings<br />

The two programs must be run on a palette device with a setting of “256 Color” in order to see the<br />

effects of color approximation. Generally this can be adjusted through changing the settings of the system.<br />

We can do this by first opening application Control Panel (Click “Start” menu, go to “Settings”, select<br />

“Control Panel”). Then we ned to double click icon “Display” and click “Setting” tab from the popped up<br />

property sheet. There are a lot of choices in the combo box that is labeled “Color Palette”. Usually it<br />

contains entries such as “24bit”, “256 color”, “16 Color”, etc. The actual available selections depend on the<br />

capability of the video card. If we select “24bit” true color setting, no dithering effect will be seen in the<br />

system. Here we need to choose “256 color” setting in order to test our samples (Figure 8-4).<br />

Choose 256 Color<br />

setting here<br />

Figure 8-4. Setting display<br />

Results<br />

Figure 8-5 and Figure 8-6 show the results from the two color approximation approaches (Please note<br />

that if the samples are executed on non-palette device, we may not see the approximation effect).<br />

222

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

Saved successfully!

Ooh no, something went wrong!