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 9. Font<br />

};<br />

Variable m_nRange indicates the range of the percentage bar, and m_nCurPos indicates the current<br />

position. They are initialized in the constructor:<br />

CPercent::CPercent()<br />

{<br />

m_nRange=100;<br />

m_nCurPos=0;<br />

}<br />

Funtions CPercent::SetPercentage(…) and CPercent::SetPosition(…) allow us to change the value<br />

of m_nCurPos, and CPercent::SetRange(…) allows us to change the total range.<br />

Within function CPercent::OnPaint(), we will draw the percentage bar using the values of m_nRange<br />

and m_nCurPos.<br />

We need to check if m_nRange is zero. If so, we can not draw the percentage bar. If not, we need to first<br />

find out the size of the window (the static control window) within which the percentage bar will be drawn.<br />

This information is stored in a local variable rect. Next, we create the text string and store it in another<br />

local variable szStr, whose format is “XXX%” (XXX represents a number between 0 and 100). To place<br />

the text in the center of the rectangle, we need to know its dimension.<br />

To retrieve the dimension of a text string, we need to call function CDC::GetTextExtent(…) and pass<br />

the actual string to it. The function will return a CSize type value that specifies the dimension of this text.<br />

The percentage bar is divided into two portions. On the left side of the rectangle, the text color is white<br />

and the background color is blue. The following portion of function CPercent::OnPaint() shows how to<br />

create text string, set foreground and background colors, and retrieve the dimension of the text:<br />

void CPercent::OnPaint()<br />

{<br />

CPaintDC dc(this);<br />

CRect rect;<br />

CRect rectHalf;<br />

CString szStr;<br />

CSize sizeExtent;<br />

COLORREF colorTextOld;<br />

COLORREF colorBkOld;<br />

……<br />

if(m_nRange != 0)<br />

{<br />

GetClientRect(rect);<br />

szStr.Format("%d%%", m_nCurPos*100/m_nRange);<br />

colorTextOld=dc.SetTextColor(RGB(255, 255, 255));<br />

colorBkOld=dc.SetBkColor(RGB(0, 0, 255));<br />

sizeExtent=dc.GetTextExtent(szStr);<br />

Next, the dimension of the left side rectange of the percentage bar is stored in local variable rectHalf.<br />

Then function CDC::ExtTextOut(…) is called to draw the left part of the percentage bar (Mode<br />

ETO_CLIPPED is used here, it will restrict the drawing within the rectangle). Because ETO_OPAQUE flag is also<br />

used, the text will be drawn with white color and the rest part of rectangle (specified by rectHalf) will all<br />

be painted blue:<br />

……<br />

sizeExtent=dc.GetTextExtent(szStr);<br />

rectHalf=rect;<br />

rectHalf.right=rect.Width()*m_nCurPos/m_nRange;<br />

dc.ExtTextOut<br />

(<br />

(rect.Width()-sizeExtent.cx)/2,<br />

(rect.Height()-sizeExtent.cy)/2,<br />

ETO_OPAQUE|ETO_CLIPPED,<br />

rectHalf,<br />

szStr,<br />

NULL<br />

247

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

Saved successfully!

Ooh no, something went wrong!