QA: How can I create a dialog title like in Control Panel property pages?

Vassili Philippov (vasja@spbteam.com), March 05, 2001.

Question

In standard Pocket PC applications they have a special header in dialogs with blue bold title. How can I insert this header in my dialog (property page)?

Answer

A typical dialog with title Pocket PC logo requirements states that application should always show application name in the navigation bar. It is written:

Required: Application title displayed on NavBar. The topmost application must continuously display its title on the NavBar, regardless of which view the application is in. Other titles, such as the names of the current view, document, card, dialog, etc must be within the client area if it is displayed.

Logo requirements do not describe how you should show your dialog title and there is no API function for that. But many developers want to make a program that looks like a standard one. Users know standard programs and they expect the same type of visualization of dialog titles.

Here is code that shows such a title. You can use it in dialogs and in property pages. Handle WM_PAINT message and add the following code in your OnPaint method.

Source code

inline void PaintHeader(CDC &dc, const CString &strTitle) { int nWidth = dc.GetDeviceCaps(HORZRES); const int nHeaderHeight = 24; // paint title CFont *pCurrentFont = dc.GetCurrentFont(); LOGFONT lf; pCurrentFont->GetLogFont(&lf); lf.lfWeight = FW_BOLD; CFont newFont; newFont.CreateFontIndirect(&lf); CFont *pSave = dc.SelectObject(&newFont); dc.SetTextColor(RGB(0, 0, 156)); dc.DrawText(strTitle, CRect(8, 0, nWidth, nHeaderHeight), DT_VCENTER | DT_SINGLELINE); dc.SelectObject(pSave); // paint line CPen blackPen(PS_SOLID, 1, RGB(0,0,0)); CPen *pOldPen = dc.SelectObject(&blackPen); dc.MoveTo(0, nHeaderHeight); dc.LineTo(nWidth, nHeaderHeight); dc.SelectObject(pOldPen); } void CTestHeaderDlg::OnPaint() { CPaintDC dc(this); PaintHeader(dc, _T("Dialog title here")); }