需要在标题栏上贴图,更换标题栏背景的时候,不想要标题栏显示颜色,尤其拉动改变窗体大小的时候,所以需要禁止标题栏重绘的方法。
只需要 处理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();
}