修改活动窗口标题栏的样式和标题栏字体

转载自 qinhualuo
最终编辑 qinhualuo

部分摘抄自:http://topic.csdn.net/t/20041217/09/3653340.html

处理WM_NCPAINT等几个非客户区消息,这些
消息处理之后都要调用缺省函数DefWindowProc(),因此
重载DefWindowProc虚函数.
对话框属性有 Tool Window 去掉System Menu
LRESULT CTitleDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lt=CDialog::DefWindowProc(message, wParam, lParam);
if(::IsWindow(m_hWnd))
{
if(message==WM_NCPAINT||
message==WM_NCACTIVATE||
message==WM_NOTIFY||
message==WM_SETTEXT)
{
CDC *pDC=GetWindowDC();
if(pDC)
{
CRect rtWnd, rtTitle;
GetWindowRect(&rtWnd);
rtTitle.left=GetSystemMetrics(SM_CXFRAME);
rtTitle.top=GetSystemMetrics(SM_CYFRAME);
rtTitle.right=rtWnd.right-rtWnd.left-
GetSystemMetrics(SM_CYFRAME);
rtTitle.bottom=rtTitle.top +
GetSystemMetrics(SM_CYSMCAPTION)-1;
//现在已经获得DC和RECT,可以随意在标题栏上画
CBrush br;
CPen pen(PS_SOLID,1,RGB(255,255,0));
br.CreateSolidBrush(RGB(255,255,0));
int save=pDC->SaveDC();
pDC->SelectObject(&pen);
pDC->SelectObject(&br);
pDC->Rectangle(rtTitle);//画矩形
//画一个标准最小化按钮
pDC->DrawFrameControl(CRect(4,4,15,14),
DFC_CAPTION,DFCS_CAPTIONMIN);
//照上还可以画最大和关闭标准按钮
pDC->RestoreDC(save);
ReleaseDC(pDC);
}
}
}
return lt;
}
处理鼠标按下消息
........
先判断是哪个按钮,再作相应的处理,例如:
最大化和恢复按钮: if(IsZoomed())
SendMessage(WM_SYSCOMMAND,SC_RESTORE,
MAKELPARAM(point.x,point.y));
else
SendMessage(WM_SYSCOMMAND,SC_MAXIMIZE,
MAKELPARAM(point.x,point.y));
最小化按钮: SendMessage(WM_SYSCOMMAND,SC_MINIMIZE,
MAKELPARAM(point.x,point.y));
关闭按钮: SendMessage(WM_CLOSE);
.........
我用VC打开OICQ.EXE的资源,发现OICQ的标题栏上的按钮也是自绘

2.修改标题栏的字体:
void CXXXDlg::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); //最好定义一个m_strCaption类成员

      ReleaseDC(pDC);
}


你可能感兴趣的:(VC)