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 />

Sample 8.11\GDI demonstrates how to check the abilities of a device. It is a standard SDI application<br />

generated from Application Wizard. In the sample, ability checking is implemented in the initialization<br />

stage of the client window.<br />

For this purpose, function CGDIView::OnInitialUpdate() is added to the application through using<br />

Application Wizard. In this function, various device abilities are checked and the result is displayed in a<br />

message box:<br />

void CGDIView::OnInitialUpdate()<br />

{<br />

CClientDC dc(this);<br />

CString szStr;<br />

CString szBuf;<br />

int nRasterCaps;<br />

int nNumOfColors;<br />

int nNumOfReserved;<br />

int nColorRes;<br />

CView::OnInitialUpdate();<br />

}<br />

szStr="My video device's ability:\n";<br />

nRasterCaps=dc.GetDeviceCaps(RASTERCAPS);<br />

if(nRasterCaps & RC_PALETTE)<br />

{<br />

szStr+="\tIt is a palette device\n";<br />

nNumOfColors=dc.GetDeviceCaps(SIZEPALETTE);<br />

szBuf.Format("\tThe device supports %d colors\n", nNumOfColors);<br />

szStr+=szBuf;<br />

szBuf.Empty();<br />

nNumOfReserved=dc.GetDeviceCaps(NUMRESERVED);<br />

szBuf.Format("\tThere are %d static colors\n", nNumOfReserved);<br />

szStr+=szBuf;<br />

nColorRes=dc.GetDeviceCaps(COLORRES);<br />

szBuf.Format("\tColor resolution is %d bits\n", nColorRes);<br />

szStr+=szBuf;<br />

}<br />

else<br />

{<br />

szStr+="\tIt is not a palette device\n";<br />

}<br />

szBuf.Empty();<br />

szBuf.Format<br />

(<br />

"\tHorizontal size is %d mm, %d pixels\n",<br />

dc.GetDeviceCaps(HORZSIZE),<br />

dc.GetDeviceCaps(HORZRES)<br />

);<br />

szStr+=szBuf;<br />

szBuf.Empty();<br />

szBuf.Format<br />

(<br />

"\tVertical size is %d mm, %d pixels\n",<br />

dc.GetDeviceCaps(VERTSIZE),<br />

dc.GetDeviceCaps(VERTRES)<br />

);<br />

szStr+=szBuf;<br />

AfxMessageBox(szStr);<br />

Here we check if the device is a palette device or not. If it is a palette device, we further find out the<br />

maximum number of colors it supports, the number of static colors reserved, and the actual color resolution<br />

for each pixel. Also, the device’s horizontal and vertical sizes are retrieved.<br />

This function is especially useful in finding out if the device is a palette device or not. With this<br />

information, we can decide if the logical palette should be used in the application.<br />

The following lists the capabilities of certain device:<br />

It is a palette device<br />

234

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

Saved successfully!

Ooh no, something went wrong!