技巧_MFC_標題欄背景
需要在標題欄上貼圖,更換標題欄背景的時候,不想要標題欄顯示顏色,尤其拉動改變窗體大小的時候,所以需要禁止標題欄重繪的方法。
只需要 處理WM_NCPAINT訊息就好了,訊息處理函式OnNcPaint中去掉CDialog::OnNcPaint(),從此世界安靜了
標題欄更換顏色,標題欄貼圖,窗體其他位置貼圖也可以用這裡的貼圖函式
//*******************************************************************//
void CMyDlg::DrawTitleBar(CDC *pDC)
{
CDC memDC;
CBitmap bmp;
CRect rect, rtTitle;
CPoint point;
GetClientRect(rect);
//獲取標題欄大小
rtTitle.left = GetSystemMetrics(SM_CXFRAME);
rtTitle.top = GetSystemMetrics(SM_CYFRAME);
rtTitle.right = rect.right - rect.left - GetSystemMetrics(SM_CXFRAME);
rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
point.x = rect.right + GetSystemMetrics(SM_CXFRAME);
point.y = 30;
memDC.CreateCompatibleDC(NULL);
bmp.CreateCompatibleBitmap(pDC,point.x,point.y);
CBitmap *pOldBit=memDC.SelectObject(&bmp);
//背景顏色
memDC.FillSolidRect(0,0,point.x,point.y,RGB(125,0,125));
////貼圖
//CBitmap pic;
//pic.LoadBitmap(IDB_BITMAP1);
//ShowBitmap(&memDC,0,0, rtTitle.right, 30, pic);
pDC->BitBlt(0,0,point.x,point.y,&memDC,0,0,SRCCOPY);
pOldBit->DeleteObject();
memDC.DeleteDC();
bmp.DeleteObject();
}
LRESULT CMyDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
//return CDialog::DefWindowProc(message, wParam, lParam);
LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam);
if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)
{
CDC* pWinDC = GetWindowDC();
if (pWinDC)
DrawTitleBar(pWinDC);
ReleaseDC(pWinDC);
}
return lrst;
}
void CMyDlg::OnNcPaint()
{
//CDialog::OnNcPaint();
}
BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CMyDlg::ShowBitmap(CDC *pDC, int x, int y, int nW, int nH, CBitmap &m_bitmap)
{
CDC memDc, mdc;
memDc.CreateCompatibleDC(NULL);
mdc.CreateCompatibleDC(NULL);
CBitmap memBitmap;
memBitmap.CreateCompatibleBitmap(pDC,nW,nH);
CBitmap *OldBmp = memDc.SelectObject(&m_bitmap);
mdc.SelectObject(&m_bitmap);
memDc.BitBlt(x,y,nW,nH,&mdc,0,0,SRCCOPY);
BITMAP bm;
m_bitmap.GetBitmap( &bm );
//貼圖
//pDC->BitBlt(x, y,
// x + bm.bmWidth,y + bm.bmHeight,
// &MemDc,
// 0, 0,
// SRCCOPY);
//拉伸
pDC->StretchBlt(x,y, nW, nH ,&memDc,0,0,
bm.bmWidth,bm.bmHeight,SRCCOPY);
memDc.SelectObject(OldBmp);
memDc.DeleteDC();
}