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

not be processed. The active state of the non-client area should always be painted after receiving message<br />

WM_NCPAINT (instead of WM_NCACTIVE).<br />

Since we only want to change the appearance of default caption bar, we will let the rest non-client area<br />

be painted by default implementation. To do this, after receiving WM_NCPAINT and WM_NCACTIVATE<br />

messages, we can first call the default message handlers (Which will cause the non-client area to be painted<br />

by the default method), then paint the caption bar using our own implementation.<br />

The default handlers of the above two messages are functions CWnd::OnNcPaint() and CWnd::<br />

OnNcActivate(…). By default, they paint the caption bar, draw the icon and system buttons on the caption<br />

bar, draw the frame and border.<br />

The other two messages we must handle are WM_SETTEXT and WM_SYSCOMMAND. The first command<br />

corresponds to the situation when the caption text is first set or when it is changed. The second message<br />

corresponds to the situation when the application resumes from the iconic state to its original state. In the<br />

above two cases, after message WM_NCPAINT is sent to the application, the text will be put directly to the<br />

caption bar.<br />

Caption Text Area<br />

Figure 13-4 shows the composition of a caption bar: the outer frame, within which there is an icon<br />

located at the left side, and three system buttons located at the right of the caption bar. The minimize button<br />

and the maximize button are abut together, also, there is a space between them and the close button. By<br />

default, the caption text is aligned to the left.<br />

The position and size of a caption window can be obtained by calling function CWnd::<br />

GetWindowRect(…). We need to exclude the frame, icon and buttons in order to calculate the area where we<br />

can put the caption text.<br />

So the actual caption text area can be calculated as follows:<br />

left position =<br />

(<br />

left position of the caption window +<br />

border width +<br />

system button horizontal size +<br />

frame width<br />

)<br />

Icon<br />

Frame<br />

Caption Text<br />

Minimize Button<br />

Maximize Button<br />

Figure 13-4. Caption bar<br />

Close Button<br />

top position = top position of the caption window + frame height<br />

right position =<br />

(<br />

right position of the caption window -<br />

border width -<br />

frame width -<br />

3×(system button horizontal size)<br />

)<br />

403

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

Saved successfully!

Ooh no, something went wrong!