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 13. Adding Special Features to Application<br />

bottom position = top position + vertical size of the caption<br />

If we use window DC, the coordinates of the window’s top-left corner are (0, 0), this will simplify our<br />

calculation.<br />

Please note that we must use class CWindowDC to create DC for painting the non-client area rather than<br />

using class CClientDC. Class CClientDC is designed to let us paint only within a window’s client area, so<br />

its origin is located at left-top corner of the client window. Class CWindowDC can let us paint the whole<br />

window, including both client and non-client area.<br />

Sample 13.5\Cap<br />

Sample application 13.5\Cap demonstrates this technique. It is a standard SDI application generated by<br />

Application Wizard, and its caption window is painted yellow no matter what the corresponding system<br />

color is (The default caption bar color can be customized by the user). The modifications made to the<br />

application are all within class CMainFrame, and there are altogether four message handlers added to the<br />

application: CMainFrame::OnNcPaint() for message WM_NCPAINT, CMainFrame::OnNcActivate(…) for<br />

message WM_NCACTIVATE, CMainFrame::OnSetText() for message WM_SETTEXT and CMainFrame::<br />

OnSysCommand(…) for message WM_SYSCOMMAND.<br />

The function implemented for drawing caption text is CMainFrame::DrawCaption(…). This function<br />

has one COLORREF type parameter color, which will be used as the text background. Within this function,<br />

several system metrics are obtained, which will be used to calculate caption text area later:<br />

……<br />

……<br />

nCapButtonX=::GetSystemMetrics(SM_CXSIZE);<br />

nBorderX=::GetSystemMetrics(SM_CXBORDER);<br />

nFrameX=::GetSystemMetrics(SM_CXFRAME);<br />

nFrameY=::GetSystemMetrics(SM_CYFRAME);<br />

szCaption.LoadString(AFX_IDS_APP_TITLE);<br />

szCaption=GetActiveDocument()->GetTitle()+”_”+szCaption;<br />

The caption text is obtained by combining the name of currently opened document with the string<br />

stored in resource AFX_IDS_APP_TITLE. Here resource AFX_IDS_APP_TITLE stores application name, and<br />

function CDocument::GetTitle() returns the name of currently opened document.<br />

Then the area where we can put caption text is calculated and stored in a local variable rectDraw.<br />

Before drawing the text, we need to fill it with the background color:<br />

……<br />

……<br />

GetWindowRect(rect);<br />

rectDraw.left=nCapButtonX+nBorderX+nFrameX;<br />

rectDraw.top=nFrameY;<br />

rectDraw.right=rect.Width()-3*nCapButtonX-nFrameX-nBorderX;<br />

rectDraw.bottom=rectDraw.top+::GetSystemMetrics(SM_CYSIZE);<br />

dc.FillSolidRect(rectDraw, color);<br />

Since the DC is created using class CWindowDC, the coordinates of the window’s origin are (0, 0).<br />

Before drawing the text, we need to set the text background mode to transparent. Also, in the sample, when<br />

the caption text is being drawn, it is centered instead of being aligned left:<br />

……<br />

……<br />

dc.FillSolidRect(rectDraw, color);<br />

nBkMode=dc.SetBkMode(OPAQUE);<br />

dc.DrawText(szCaption, rectDraw, DT_CENTER);<br />

dc.SetBkMode(nBkMode);<br />

Function CMainFrame::DrawCaption() is called in several places. When WM_NCACTIVATE message is<br />

received and the window state is about to become inactive, we paint the caption bar with cyan color. When<br />

WM_NCPAINT message is received, the caption bar is painted with yellow color.<br />

404

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

Saved successfully!

Ooh no, something went wrong!