正在阅读:Windows Gdi 应用-入门篇 (VC SDK)Windows Gdi 应用-入门篇 (VC SDK)

2005-06-14 10:18 出处: 作者:i_like_cpp 责任编辑:moningfeng
我们以如何绘制一个具有平面风格的状态栏为例:   首先从CStatusBar继承一个类:CStatusBarNew。(如果无法通过类向导做这件事,而你又对MFC的MESSAGEMAP等等东西不熟悉,可以从CStatusBarCtrl继承一个,待生成代码后,把所有的CStatusBarCtrl改为CStatusBar)   在此,只需要重写WM_PAINT和WM_ERASEBKGND这两个消息的响应函数。

BOOL CStatusBarNew::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default CRect rect; GetWindowRect(&rect); ScreenToClient(&rect); CBrush brush(0xf2f2f2); pDC->FillRect(&rect, &brush); return TRUE; }

  上面函数把状态栏背景用0xf2f2f2这种颜色填充。

void CStatusBarNew::OnPaint() { CPaintDC cDC(this); // device context for painting // TODO: Add your message handler code here CRect rcItem; cDC.SetBkMode(TRANSPARENT); cDC.SelectObject (::GetStockObject (NULL_BRUSH));//选入画刷 // 获取字体 CFont* pfont = GetFont(); CFont* def_font; if (pfont) def_font = cDC.SelectObject(pfont);//选入字体 CPen pen; pen.CreatePen(PS_SOLID, 1, RGB(0xBD, 0xBA, 0xBD)); CPen* pOldPen = cDC.SelectObject(&pen);//选入画笔 CBrush br(0x00f2f2f2); for ( int i = 0; i < m_nCount; i++ ) { GetItemRect (i, rcItem); //填充面板背景 cDC.FillRect(rcItem, &br); rcItem.bottom--; if(i == 0) rcItem.left += 2; //对每个面板画圆角矩形 cDC.RoundRect(rcItem, CPoint(5, 5)); //画面板上的文字 UINT nNewStyle = GetPaneStyle(i); //如果style为SBPS_DISABLED,则跳过不画 if ((nNewStyle & SBPS_DISABLED) != 0) continue; CString text = GetPaneText(i); UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP | DT_LEFT; rcItem.left += 3; rcItem.top += 3; cDC.DrawText(text, rcItem, uFormat); } if (pfont) cDC.SelectObject(def_font);//恢复字体 //画右下角小标志(这里画了六个小圆圈) if (GetStyle() & SBARS_SIZEGRIP) { CRect rc; GetClientRect(&rc); rc.left = rcItem.right; rc.right--; rc.bottom--; rc.left = rc.right - rc.Width() / 4; rc.top = rc.bottom - rc.Width(); int w = rc.Width(); rc.top++; rc.left++; cDC.SelectObject(GetStockObject(GRAY_BRUSH)); cDC.Ellipse(&rc); rc.OffsetRect(-w, -w); cDC.Ellipse(&rc); rc.OffsetRect(w, 0); cDC.Ellipse(&rc); rc.OffsetRect(-w, w); cDC.Ellipse(&rc); rc.OffsetRect(-w, 0); cDC.Ellipse(&rc); rc.OffsetRect(2 * w, -2 * w); cDC.Ellipse(&rc); } cDC.SelectObject(pOldPen);//恢复画笔 }

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品