CTestDlg::CTestDlg(int nID, CWnd* pParent /*=NULL*/) : CDialog(nID, pParent)
{ //{{AFX_DATA_INIT(CTestDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_bPressed=FALSE; //load the bitmap of button_down m_bitmapPressed.LoadBitmap(IDB_BUTTONDOWN); //load the bitmap of button_up m_bitmapUnpressed.LoadBitmap(IDB_BUTTONUP); //load the bitmap of caption m_bitmapCaption.LoadBitmap(IDB_CAPTION); //load the bitmap of background m_bmpBk.LoadBitmap(IDB_BKGROUND);
}
?
(2)分別得到標題欄和按鈕的位置。??
首先得到按鈕的位置:??
void CTestDlg::GetButtonRect(CRect& rect)
{ GetWindowRect(&rect); // for small caption use SM_CYDLGFRAME rect.top += GetSystemMetrics(SM_CYFRAME)+1; // for small caption use SM_CYSMSIZE rect.bottom =rect.top +GetSystemMetrics(SM_CYSIZE)-4; // for small caption use SM_CXDLGFRAME rect.left =rect.right -GetSystemMetrics(SM_CXFRAME); // for small caption use SM_CXSMSIZE GetSystemMetrics(SM_CXSIZE))-1; // for small caption use SM_CXSMSIZE rect.right =rect.left +GetSystemMetrics(SM_CXSIZE)-3;
}
?
然后得到標題欄的位置:?
void CTestDlg::GetCaptionRect(CRect &rect)
{ GetWindowRect(&rect); // for small caption use SM_CYSMSIZE rect.bottom =rect.top+GetSystemMetrics(SM_CYSIZE)+3;
}
?
5.在按鈕和標題欄對應的位置上分別畫出蘋果風格的按鈕和標題欄。??
首先畫出按鈕:??
void CTestDlg::DrawButton()
{ // if window isn't visible or is minimized, skip if (!IsWindowVisible() || IsIconic()) return; // get appropriate bitmap CDC memDC; CDC* pDC = GetWindowDC(); memDC.CreateCompatibleDC(pDC); memDC.SelectObject(m_bPressed ? &m_bitmapPressed : &m_bitmapUnpressed); // get button rect and convert it into non-client area coordinates CRect rect, rectWnd; GetButtonRect(rect); GetWindowRect(rectWnd); rect.OffsetRect(-rectWnd.left, -rectWnd.top); int width,height; BITMAP *pBitMap; pBitMap = new BITMAP; if (m_bPressed) m_bitmapPressed.GetBitmap(pBitMap); else m_bitmapUnpressed.GetBitmap(pBitMap); width=pBitMap->bmWidth; height=pBitMap->bmHeight; // draw it pDC->StretchBlt( rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0,width,height,SRCCOPY ); memDC.DeleteDC(); ReleaseDC(pDC); delete pBitMap;
}
?
然后畫出標題欄:?
void CTestDlg::DrawCaption()
{ if (!IsWindowVisible() || IsIconic()) return; // get appropriate bitmap CDC memDC; CDC* pDC = GetWindowDC(); memDC.CreateCompatibleDC(pDC); memDC.SelectObject(m_bitmapCaption); // get button rect and convert it into non-client area coordinates CRect rect, rectWnd; GetCaptionRect(rect); GetWindowRect(rectWnd); rect.OffsetRect(-rectWnd.left, -rectWnd.top); // draw the caption int width,height; BITMAP *pBitMap; pBitMap = new BITMAP; m_bitmapCaption.GetBitmap(pBitMap); width=pBitMap->bmWidth; height=pBitMap->bmHeight; pDC->StretchBlt( rect.left, rect.top, rect.Width(),rect.Height(), &memDC, 0, 0, width,height, SRCCOPY ); //get the the text of the caption and draw it with 3D style pDC->SetBkColor(RGB(209,209,209)); CString caption; GetWindowText(caption); caption = ""+caption+""; rect.OffsetRect(0,4); //draw the text of the caption with gray color pDC->SetTextColor(RGB(128,128,128)); pDC->DrawText(caption,rect,DT_CENTER|DT_VCENTER); //move the coordinate to left and up rect.OffsetRect(-1,-1); pDC->SetBkMode(TRANSPARENT); //draw the text of the caption with white color pDC->SetTextColor(RGB(255,255,255)); //255,255,255 128,128,128 pDC->DrawText(caption,rect,DT_CENTER|DT_VCENTER); memDC.DeleteDC(); ReleaseDC(pDC); delete pBitMap;
}
void CTestDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{ if (nHitTest == HTCAPTION) { // see if in area we reserved for button CRect rect; GetButtonRect(rect); if (rect.PtInRect(point)) { m_bPressed = !m_bPressed; DrawButton(); CDialog::OnCancel(); } } CDialog::OnNcLButtonDown(nHitTest, point);
}