As we know, it is relatively easy to draw in the client area of the applicaiton.But how to draw in the nonclient area if we want to have custom title bar or menu.I supply one way to implement that object.
First, override the DefWindowProc function to direct some messages to your DrawTitleBar function as shown below.
if (!::IsWindow(m_hWnd))
{
return lrst;
}
if (WM_MOVE == message || WM_NCPAINT == message || WM_NCACTIVATE == message || WM_NOTIFY == message)
{
CDC* pWinDC = GetWindowDC();//to retrieves the device context (DC) for the entire window
if (pWinDC)
{
DrawTitleBar(pWinDC);
}
ReleaseDC(pWinDC);
}
return lrst;
}
Note:The WM_MOVE message is sent after a window has been moved.
The WM_NCPAINT message is sent to a window when its frame must be painted.
The WM_NCACTIVATE message is sent to a window when its nonclient area needs to be changed to indicate an active or inactive state.
The WM_NOTIFY message is sent by a common control to its parent window when an event has occurred or the control requires some information.
Then you can draw the title bar in the DrawTitleBar function. There are some necessary operation to do in this function, such as draw the title bar, draw the window border, draw the exit button, draw the application icon in the title bar and draw the title.It's essential to know the size of those areas if we want to draw them.Thus, the GetSystemMetrics function which can retrieves various system metrics is very useful here. Besides, the draw functions is the same as used in the client area.
CPoint point;
//redraw title bar, the same way to draw the window border
point.x = rtWnd.Width();
point.y = GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CYFRAME);
pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
//...
//redraw the applicaion icon
m_rtIcon.left = GetSystemMetrics(SM_CXFRAME);
m_rtIcon.top = GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYSIZE) / 2 - APP_ICON_HIGHT / 2;
m_rtIcon.right = m_rtIcon.left + APP_ICON_WIDTH;
m_rtIcon.bottom = m_rtIcon.top + APP_ICON_HIGHT;
::DrawIconEx(pDC->m_hDC, m_rtIcon.left, m_rtIcon.top, AfxGetApp()->LoadIcon(IDR_MAINFRAME), m_rtIcon.Width(), m_rtIcon.Height(), 0, NULL, DI_NORMAL);
//redraw the buttons
CBitmap* pBitmap = new CBitmap;
CBitmap* pOldBitmap;
CDC* pDisplayMemDC=new CDC;
pDisplayMemDC->CreateCompatibleDC(pDC);
rtButtons.left = rtTitle.right - EXIT_ICON_WIDTH - 2;//2 is a result of testing
rtButtons.top = rtTitle.top + GetSystemMetrics(SM_CYSIZE) / 2 - EXIT_ICON_HIGHT / 2 - 1;//1 is a result of testing
rtButtons.right = rtButtons.left + EXIT_ICON_WIDTH;
rtButtons.bottom = rtButtons.top + EXIT_ICON_HIGHT;
pBitmap->LoadBitmap(IDB_EXIT_NORMAL);
pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);
pDisplayMemDC->SelectObject(pOldBitmap);
m_rtButtExit = rtButtons;
pBitmap->DeleteObject();
//...
//redraw the title text
int nOldMode = pDC->SetBkMode(TRANSPARENT);
COLORREF clOldText=pDC->SetTextColor(TITLE_TEXT_COLOR);
pDC->SelectStockObject(SYSTEM_FIXED_FONT);
rtTitle.left += APP_ICON_WIDTH + GetSystemMetrics(SM_CXFRAME);
rtTitle.top = m_rtIcon.top;
CString strTitle;
GetWindowText(strTitle);
pDC->DrawText(strTitle, &rtTitle, DT_LEFT);
pDC->SetBkMode(nOldMode);
pDC->SetTextColor(clOldText);
And the OnNcMouseMove which the framework call when the cursor is moved within a nonclient area is also recommended to be implemented.It's intended to redraw the exit button, minimize button and the maximize butoon when the cursor is moved within them.
//the exit button
if (m_rtButtExit.PtInRect(point))
{
pBitmap->LoadBitmap(IDB_EXIT_NORMAL);
rtButton = m_rtButtExit;
pOldBitmap = (CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
pDC->BitBlt(rtButton.left, rtButton.top, rtButton.Width(), rtButton.Height(), pDisplayMemDC, 0, 0, SRCCOPY);
pDisplayMemDC->SelectObject(pOldBitmap);
pBitmap->DeleteObject();
}
To have a custom menu is desirable to derive a class from CMenu.Then there are three steps to complete it.First, attach the custom menu. I finish that in the following way.
if (!pFrame->LoadFrame(IDR_MAINFRAME))
{
return FALSE;
}
m_pMainWnd = pFrame;
pFrame->CustomMenuAttach(pFrame->m_hMenuDefault);
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
The code above is added into the CXXXApp::InitInstance, and CustomMenuAttach is implemented as follow:
And there are three functions to be implemented in the CMainFrame:
//The framework calls this member funcion for the owner of an owner-draw menu when it is created.
void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: Add your message handler code here and/or call default
if(lpMeasureItemStruct->CtlType == ODT_MENU)
{
m_hMenu.MeasureItem(lpMeasureItemStruct);
return ;
}
CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
//The framework calls this member function for the owner of an owner-draw menu when a visual aspect of the menu has changed.
void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if (lpDrawItemStruct->CtlType == ODT_MENU)
{
m_hMenu.DrawItem(lpDrawItemStruct);
}
CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
Well, what the menu looks like depends on how you implement the derived class from CMenu.