n lrst;
}
在DrawTitleBar函数中,我们将采用乾坤大挪移,将Icon画到了右边,将最小、
最大、关闭按钮画到了左边,并画上了颜色渐变的Title Bar,在中间写了“My Own
Cool Title Bar!!!”的标题(见代码片段3)。最后将最小、最大、关闭按钮连上
了各自的消息(见代码片段4)。
代码片段3:
void CMainFrame::DrawTitleBar(CDC* pDC)
{
if (m-hWnd)
{
CRect rtWnd, rtTitle, rtButtons;
GetWindowRect(&rtWnd); //整个Window的相对于屏幕的矩形
//取得整个Title bar的矩形
rtTitle.left=GetSystemMetrics(SM-CXFRAME);
rtTitle.top=GetSystemMetrics(SM-CYFRAME);
rtTitle.right=rtWnd.right-rtWnd.left-GetSystemMetrics(SM-CXFRAME);
rtTitle.bottom=rtTitle.top+GetSystemMetrics(SM-CYSIZE);
//重画颜色渐变的Title Bar;有DC,有矩形,想怎么画就怎么画
DrawGradientBar(pDC, rtTitle); //此函数源码因篇幅略去
//重画icon
HICON hIcon=(HICON)::GetClassLong(m-hWnd, GCL-HICON);
m-rtIcon.left=rtTitle.right-GetSystemMetrics(SM-CYSMICON);
m-rtIcon.top=rtTitle.top+1;
m-rtIcon.right=m-rtIcon.left+GetSystemMetrics(SM-CXSMICON);
m-rtIcon.bottom=m-rtIcon.top+GetSystemMetrics(SM-CYSMICON);
::DrawIconEx(pDC->m-hDC, m-rtIcon.left, m-rtIcon.top,hIcon, GetSystemMetrics
(SM-CXSMICON), GetSystemMetrics(SM-CYSMICON), 0, NULL,
DI-NORMAL);
m-rtIcon.OffsetRect(rtWnd.TopLeft()); //记录Icon屏幕位置
//重画最小button
int nButtHeight=GetSystemMetrics(SM-CYSMSIZE)-3;
rtButtons.left=rtTitle.left;
rtButtons.top=rtTitle.top+(GetSystemMetrics(SM-CYSIZE)-nButtHeight)/2;
rtButtons.right=rtButtons.left+GetSystemMetrics(SM-CXSMSIZE);
rtButtons.bottom=rtButtons.top+nButtHeight;
pDC->DrawFrameControl(&rtButtons, DFC-CAPTION, DFCS-CAPTIONMIN);
m-rtButtMin=rtButtons;
m-rtButtMin.OffsetRect(rtWnd.TopLeft()); //记录最小button屏幕位置
//重画最大或恢复button
rtButtons.left=rtButtons.right;
rtButtons.right=rtButtons.left+GetSystemMetrics(SM-CXSMSIZE);
pDC->DrawFrameControl(&rtButtons, DFC-CAPTION, IsZoomed() ?
DFCS-CAPTIONRESTORE : DFCS-CAPTIONMAX);
m-rtButtMax=rtButtons;
m-rtButtMax.OffsetRect(rtWnd.TopLeft());//记录button屏幕位置
//重画关闭button
rtButtons.left=rtButtons.right;
rtButtons.right=rtButtons.left+GetSystemMetrics(SM-CXSMSIZE);
pDC->DrawFrameControl(&rtButtons, DFC-CAPTION, DFCS-CAPTIONCLOSE);
m-rtButtExit=rtButtons;
m-rtButtExit.OffsetRect(rtWnd.TopLeft())//记录关闭button屏幕位置;
//重画caption
int nOldMode=pDC->SetBkMode(TRANSPARENT);
COLORREF clOldText=pDC->SetTextColor(RGB(0, 0, 0));
pDC->SelectStockObject(ANSI-FIXED-FONT); rtTitle.right-=GetSystemMetrics
(SM-CYSMICON); pDC->DrawText((LPSTR)″My Own Cool Title
Bar!!!″, -1, &rtTitle, DT-CENTER);
pDC->SetBkMode(nOldMode);
pDC->SetTextColor(clOldText);
}
}
代码片段4:
void CMainFrame::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
//处理缺省操作,诸如双击Title Bar等其他动作
Default(