MFC标题栏自绘

1.mfc对话框

1.1 保留原标题栏

1.如果需要加高标题栏(用于贴图等),就要调用OnNcCalcSize追加部分空间(非客户区间):

[cpp] view plaincopy

  1. void CMy2011082302Dlg::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)   

  2. {  

  3.     lpncsp->rgrc[0].top += 25;//尺度自己把握  

  4.     //lpncsp->rgrc[0].bottom -= 100;//类似增加底边栏,此例无用  

  5.       

  6.     CDialog::OnNcCalcSize(bCalcValidRects, lpncsp);  

  7. }  


2.添加绘制标题栏函数DrawTitleBar():

[cpp] view plaincopy

  1. void CMy2011082302Dlg::DrawTitleBar(CDC *pDC)  

  2. {  

  3.     if (m_hWnd)  

  4.     {  

  5.         CBrush Brush(RGB(0,100,255));  

  6.         CBrush* pOldBrush = pDC->SelectObject(&Brush);  

  7.   

  8.         CRect rtWnd, rtTitle, rtButtons;  

  9.         GetWindowRect(&rtWnd);   

  10.         //取得标题栏的位置  

  11.         rtTitle.left = GetSystemMetrics(SM_CXFRAME);  

  12.         rtTitle.top = GetSystemMetrics(SM_CYFRAME);  

  13.         rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);  

  14.         rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);  

  15.   

  16.         CPoint point;  

  17.         //填充顶部框架  

  18.         point.x = rtWnd.Width();  

  19.         point.y = GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CYFRAME)+20;  

  20.         pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);  

  21.         //填充左侧框架  

  22.         point.x = GetSystemMetrics(SM_CXFRAME) + 1;  

  23.         point.y = rtWnd.Height();  

  24.         pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);  

  25.         //填充底部框架  

  26.         point.x = rtWnd.Width();   

  27.         point.y = GetSystemMetrics(SM_CYFRAME) + 1;  

  28.         pDC->PatBlt(0, rtWnd.Height()-point.y, point.x, point.y, PATCOPY);  

  29.         //填充右侧框架  

  30.         point.x = GetSystemMetrics(SM_CXFRAME) + 1;  

  31.         point.y = rtWnd.Height();  

  32.         pDC->PatBlt(rtWnd.Width()-point.x, 0, point.x, point.y, PATCOPY);  

  33.           

  34.         pDC->SelectObject(pOldBrush);  

  35.   

  36.         //重画图标  

  37.         m_rtIcon.left = rtWnd.Width() - 135;  

  38.         m_rtIcon.top = GetSystemMetrics(SM_CYFRAME);  

  39.         m_rtIcon.right = m_rtIcon.left + 32;  

  40.         m_rtIcon.bottom = m_rtIcon.top + 32;  

  41.         ::DrawIconEx(pDC->m_hDC, m_rtIcon.left, m_rtIcon.top, m_hIcon,   

  42.             m_rtIcon.Width(), m_rtIcon.Height(), 0, NULL, DI_NORMAL);  

  43.         m_rtIcon.OffsetRect(rtWnd.TopLeft());   

  44.   

  45.   

  46.         //准备画XP风格按钮  

  47.         CBitmap* pBitmap = new CBitmap;  

  48.         CBitmap* pOldBitmap;  

  49.         CDC* pDisplayMemDC=new CDC;  

  50.         pDisplayMemDC->CreateCompatibleDC(pDC);  

  51.   

  52.         //重画关闭button  

  53.         rtButtons.left = rtTitle.right - 19;  

  54.         rtButtons.top = rtTitle.top;  

  55.         rtButtons.right = rtButtons.left + 19;  

  56.         rtButtons.bottom = rtButtons.top + 19;  

  57.         pBitmap->LoadBitmap(IDB_EXIT_NORMAL);  

  58.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  59.         pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  60.         pDisplayMemDC->SelectObject(pOldBitmap);  

  61.         m_rtButtExit = rtButtons;  

  62.         m_rtButtExit.OffsetRect(rtWnd.TopLeft());   

  63.         pBitmap->DeleteObject();  

  64.   

  65.         //重画最大化/恢复button  

  66.         rtButtons.right = rtButtons.left - 3;  

  67.         rtButtons.left = rtButtons.right - 19;  

  68.         if (IsZoomed())  

  69.             pBitmap->LoadBitmap(IDB_RESTORE_NORMAL);  

  70.         else  

  71.             pBitmap->LoadBitmap(IDB_MAX_NORMAL);  

  72.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  73.         pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  74.         pDisplayMemDC->SelectObject(pOldBitmap);  

  75.         m_rtButtMax = rtButtons;  

  76.         m_rtButtMax.OffsetRect(rtWnd.TopLeft());  

  77.         pBitmap->DeleteObject();  

  78.   

  79.         //重画最小化button  

  80.         rtButtons.right = rtButtons.left - 3;  

  81.         rtButtons.left = rtButtons.right - 19;  

  82.         pBitmap->LoadBitmap(IDB_MIN_NORMAL);  

  83.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  84.         pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  85.         pDisplayMemDC->SelectObject(pOldBitmap);  

  86.         m_rtButtMin = rtButtons;  

  87.         m_rtButtMin.OffsetRect(rtWnd.TopLeft());  

  88.         pBitmap->DeleteObject();  

  89.       

  90.         //重画帮助button  

  91.         rtButtons.right = rtButtons.left - 3;  

  92.         rtButtons.left = rtButtons.right - 19;  

  93.         pBitmap->LoadBitmap(IDB_HELP_NORMAL);  

  94.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  95.         pDC->BitBlt(rtButtons.left, rtButtons.top, rtButtons.Width(), rtButtons.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  96.         pDisplayMemDC->SelectObject(pOldBitmap);  

  97.         m_rtButtHelp = rtButtons;  

  98.         m_rtButtHelp.OffsetRect(rtWnd.TopLeft());  

  99.         pBitmap->DeleteObject();  

  100.   

  101.         //贴图  

  102.         point.x = rtWnd.Width();  

  103.         point.y = GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CYFRAME)+20;  

  104.         pBitmap->LoadBitmap(IDB_BITMAP1);  

  105.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  106.         pDC->BitBlt(0, 0, point.x, point.y, pDisplayMemDC, 0, 0, SRCCOPY);  

  107.         pDisplayMemDC->SelectObject(pOldBitmap);  

  108.         pBitmap->DeleteObject();  

  109.         //拉伸图片  

  110.         //pDC->StretchBlt(x,y, nW, nH ,&memDc,0,0,  

  111.         //bm.bmWidth,bm.bmHeight,SRCCOPY);  

  112.           

  113.         //重写标题文字  

  114.         int nOldMode = pDC->SetBkMode(TRANSPARENT);  

  115.         COLORREF clOldText=pDC->SetTextColor(RGB(255, 255, 255));  

  116.         pDC->SelectStockObject(SYSTEM_FIXED_FONT);  

  117.         rtTitle.left += 2;  

  118.         rtTitle.top += GetSystemMetrics(SM_CYSIZE);  

  119.         rtTitle.bottom = rtTitle.top + 30;  

  120.         CString m_strTitle;  

  121.         GetWindowText(m_strTitle);  

  122.         pDC->DrawText(m_strTitle, &rtTitle, DT_LEFT);  

  123.         pDC->SetBkMode(nOldMode);  

  124.         pDC->SetTextColor(clOldText);  

  125.   

  126.         ReleaseDC(pDisplayMemDC);  

  127.         delete pDisplayMemDC;  

  128.         delete pBitmap;  

  129.     }  

  130. }  


3.按钮响应,消息发送/拦截等(系统消息函数)

[cpp] view plaincopy

  1. LRESULT CMy2011082302Dlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)   

  2. {  

  3.     LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam);  

  4.       

  5.     if (!::IsWindow(m_hWnd))  

  6.         return lrst;  

  7.       

  8.     if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)  

  9.     {  

  10.         CDC* pWinDC = GetWindowDC();  

  11.         if (pWinDC)  

  12.             DrawTitleBar(pWinDC);  

  13.         ReleaseDC(pWinDC);  

  14.     }  

  15.     return lrst;  

  16. }  

  17.   

  18. void CMy2011082302Dlg::OnNcLButtonDown(UINT nHitTest, CPoint point)   

  19. {  

  20.     //检测各按钮是否按到  

  21.     if (m_rtButtHelp.PtInRect(point))  

  22.         SendMessage(WM_HELP);  

  23.     else if (m_rtButtExit.PtInRect(point))  

  24.         SendMessage(WM_CLOSE);  

  25.     else if (m_rtButtMin.PtInRect(point))  

  26.         SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, MAKELPARAM(point.x, point.y));  

  27.     else if (m_rtButtMax.PtInRect(point))  

  28.     {  

  29.         if (IsZoomed())  

  30.             SendMessage(WM_SYSCOMMAND, SC_RESTORE, MAKELPARAM(point.x, point.y));  

  31.         else  

  32.         {  

  33.             SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, MAKELPARAM(point.x, point.y));  

  34.             Invalidate();  

  35.         }  

  36.     }  

  37.     else if (!IsZoomed())  

  38.         Default();  

  39. }  

  40.   

  41. void CMy2011082302Dlg::OnNcMouseMove(UINT nHitTest, CPoint point)   

  42. {  

  43.     CDC* pDC = GetWindowDC();  

  44.     CDC* pDisplayMemDC=new CDC;  

  45.     pDisplayMemDC->CreateCompatibleDC(pDC);  

  46.     CBitmap* pBitmap = new CBitmap;  

  47.     CBitmap* pOldBitmap;  

  48.     CRect rtWnd, rtButton;  

  49.   

  50.     if (pDC)  

  51.     {  

  52.         GetWindowRect(&rtWnd);  

  53.   

  54.         //关闭button  

  55.         if (m_rtButtExit.PtInRect(point))  

  56.             pBitmap->LoadBitmap(IDB_EXIT_FOCUS);  

  57.         else  

  58.             pBitmap->LoadBitmap(IDB_EXIT_NORMAL);  

  59.         rtButton = m_rtButtExit;  

  60.         rtButton.OffsetRect(-rtWnd.left, -rtWnd.top);  

  61.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  62.         pDC->BitBlt(rtButton.left, rtButton.top, rtButton.Width(), rtButton.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  63.         pDisplayMemDC->SelectObject(pOldBitmap);  

  64.         pBitmap->DeleteObject();  

  65.   

  66.         //最大化/恢复button  

  67.         if (m_rtButtMax.PtInRect(point))  

  68.         {  

  69.             if (IsZoomed())  

  70.                 pBitmap->LoadBitmap(IDB_RESTORE_FOCUS);  

  71.             else  

  72.                 pBitmap->LoadBitmap(IDB_MAX_FOCUS);  

  73.         }  

  74.         else  

  75.         {  

  76.             if (IsZoomed())  

  77.                 pBitmap->LoadBitmap(IDB_RESTORE_NORMAL);  

  78.             else  

  79.                 pBitmap->LoadBitmap(IDB_MAX_NORMAL);  

  80.         }  

  81.         rtButton = m_rtButtMax;  

  82.         rtButton.OffsetRect(-rtWnd.left, -rtWnd.top);  

  83.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  84.         pDC->BitBlt(rtButton.left, rtButton.top, rtButton.Width(), rtButton.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  85.         pDisplayMemDC->SelectObject(pOldBitmap);  

  86.         pBitmap->DeleteObject();  

  87.   

  88.         //最小化button  

  89.         if (m_rtButtMin.PtInRect(point))  

  90.             pBitmap->LoadBitmap(IDB_MIN_FOCUS);  

  91.         else  

  92.             pBitmap->LoadBitmap(IDB_MIN_NORMAL);  

  93.         rtButton = m_rtButtMin;  

  94.         rtButton.OffsetRect(-rtWnd.left, -rtWnd.top);  

  95.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  96.         pDC->BitBlt(rtButton.left, rtButton.top, rtButton.Width(), rtButton.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  97.         pDisplayMemDC->SelectObject(pOldBitmap);  

  98.         pBitmap->DeleteObject();  

  99.   

  100.         //帮助button  

  101.         if (m_rtButtHelp.PtInRect(point))  

  102.             pBitmap->LoadBitmap(IDB_HELP_FOCUS);  

  103.         else  

  104.             pBitmap->LoadBitmap(IDB_HELP_NORMAL);  

  105.         rtButton = m_rtButtHelp;  

  106.         rtButton.OffsetRect(-rtWnd.left, -rtWnd.top);  

  107.         pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);  

  108.         pDC->BitBlt(rtButton.left, rtButton.top, rtButton.Width(), rtButton.Height(), pDisplayMemDC, 0, 0, SRCCOPY);  

  109.         pDisplayMemDC->SelectObject(pOldBitmap);  

  110.         pBitmap->DeleteObject();  

  111.     }  

  112.       

  113.     ReleaseDC(pDisplayMemDC);  

  114.     ReleaseDC(pDC);  

  115.     delete pDisplayMemDC;  

  116.     delete pBitmap;  

  117.   

  118.     CDialog::OnNcMouseMove(nHitTest, point);  

  119. }  

 

 1.2 去除原标题栏

1.去除窗口标题风格

2.添加响应按钮(最大化、最小化等)

3.贴图(可借助众所周知的BitButtonNL类 )处理

4.添加可供窗口拖移消息欺骗:

[cpp] view plaincopy

  1. void CMy2011082302Dlg::OnLButtonDown(UINT nFlags, CPoint point)   

  2. {  

  3. //处理移动  

  4.     if (point.y < 26)  

  5.         PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));  

  6.       

  7.     CDialog::OnLButtonDown(nFlags, point);  

  8. }   

2、mfc单/多文档

  2.1  保留原标题栏

    方法同1.1

问题:1)  禁用cs.style&=~WS_SYSMENU;(为了去除原标题栏按钮) ,但这导致邮件菜单不可用。

            2)  不名操作闪烁(个别特殊操作,基本不会)

            3)  1.1中的第1步如果单/多文档绘制了菜单,会出现菜单夹中问题。如需菜单建议菜单重绘或bcg界面库工程

 

  2.2  去除原标题栏

由于2.1中的问题1和2,再加上去除标题栏所导致的非客户区自绘按钮消息无法响应以及窗口无法拖动等情况,我们必须用OnNcHitTest改变新增加空间(1.1中第1步)为HTCAPTION属性。

   1.去除标题栏 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){} 添加:

[cpp] view plaincopy

  1. ModifyStyle(WS_CAPTION,WS_DLGFRAME,SWP_FRAMECHANGED);  


  2.同1.1中1.2.3步。1步不可缺省哦

 3.   OnNcHitTest返回HTCAPTION:

[cpp] view plaincopy

  1. UINT CMainFrame::OnNcHitTest(CPoint point)   

  2. {  

  3.     RECT rectWindows, rectClient;  

  4.     this->GetWindowRect(&rectWindows);  

  5.     this->GetClientRect(&rectClient);  

  6.       

  7.     if (point.y > rectWindows.top && point.y < rectWindows.top + 25)  

  8.     {  

  9.         return HTCAPTION;//标题栏形式  

  10.     }  

  11.     else  

  12.     {  

  13.         return CFrameWnd::OnNcHitTest(point);  

  14.     }  

  15. }  



你可能感兴趣的:(对话框,标题栏,空间)