mfc 标题栏 标题字体 居中显示

在头文件中放下面的函数:

view plaincopy to clipboardprint?
afx_msg void OnNcPaint();  
afx_msg BOOL OnNcActivate(BOOL bActive);  
afx_msg void OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized );

在cpp文件的消息映射中放:

view plaincopy to clipboardprint?
ON_WM_NCPAINT()  
ON_WM_NCACTIVATE()  
ON_WM_ACTIVATE()
在cpp文件中添加上面的这些消息响应函数:

以上步骤可以通过ClassWard完成,标题栏的重绘通过响应ON_WM_NCPAINT消息

BOOL CMyDlg::OnNcActivate (BOOL bActive)  
{  
      OnNcPaint();  
      return CDialog::OnNcActivate (bActive);        
}  

void CMyDlg::OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized )  
{  
      OnNcPaint();  
      CDialog::OnActivate ( nState, pWndOther, bMinimized );  
}  

void CMyDlg::OnNcPaint()  
{  
      CDialog::OnNcPaint();  
      CDC* pDC = GetWindowDC();  
      int x = GetSystemMetrics(SM_CXSIZE)+GetSystemMetrics(SM_CXBORDER)+GetSystemMetrics(SM_CXDLGFRAME);  
      int y = GetSystemMetrics(SM_CYDLGFRAME);  
      CRect CapRct;  
      GetWindowRect(&CapRct);  
      int y1 = GetSystemMetrics(SM_CYICON)-GetSystemMetrics(SM_CYDLGFRAME)-GetSystemMetrics(SM_CYBORDER);  
      int x1 = CapRct.Width ()- GetSystemMetrics(SM_CXSIZE)-GetSystemMetrics(SM_CXBORDER)-GetSystemMetrics(SM_CXDLGFRAME);  
        
      CapRct.left = x;  
      CapRct.top = y;  
      CapRct.right = x1;  
      CapRct.bottom = y1;  

      //pDC->FillSolidRect(&CapRct,RGB(0,0,0));  
      CFont* pCurFont = GetFont();  
      LOGFONT lf;  
      pCurFont->GetLogFont (&lf);  
      lf.lfItalic = TRUE;  
      lf.lfWeight = FW_BOLD;  
      lf.lfHeight = 18;  
      strcpy(lf.lfFaceName ,"Arial");  
        
      CFont capfont ;  
      capfont.CreateFontIndirect (&lf);  
      pCurFont = pDC->SelectObject (&capfont);  
      pDC->SetBkMode (TRANSPARENT);  
      pDC->SetTextColor (GetSysColor(COLOR_CAPTIONTEXT));  
      pDC->DrawText ("My Caption",&CapRct, DT_SINGLELINE | DT_CENTER | DT_VCENTER);  

      ReleaseDC(pDC);  
}

你可能感兴趣的:(VC++/MFC)