第十课
图形的绘制,如何使用自定义画笔(颜色,线宽,线型)。如何为程序中添加选项菜单和选项设置对话框,如何使用标准颜色对话框,如何使用字体对话框,在选项对话框中实现预览功能。实现选项对话框和窗口类中的数据交换。如何改变对话框和控件的背景色,如何改变控件的文本颜色,对按钮控件的特殊处理。如何在窗口中显示一幅位图。
- 改变button的样式,应该在CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)这个虚函数去改变一些值,框架去调用这个函数
- // Draw the button text using the text color red.
- COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
- ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
- &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
- ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
- 在窗口中贴图
- 1、创建位图
- CBitmap bitmap;
- bitmap.LoadBitmap(IDB_BITMAP1);
- 2、创建兼容DC
- CDC dcCompatible;
- dcCompatible.CreateCompatibleDC(pDC);
- 3、将位图选到兼容DC中
- dcCompatible.SelectObject(&bitmap);
- 4、将兼容DC中的位图贴到当前DC中。
- pDC->BitBlt(rect.left,rect.top,rect.Width(),
- rect.Height(),&dcCompatible,0,0,SRCCOPY);
- 响应背景的擦除BOOL CGraphicView::OnEraseBkgnd(CDC* pDC) 。WM_OnEraseBkgnd
- CBitmap bitmap;
- bitmap.LoadBitmap(IDB_BITMAP1); 加载位图
- BITMAP bmp;
- bitmap.GetBitmap(&bmp);得到位图信息
- CDC dcCompatible; 创建兼容dc
- dcCompatible.CreateCompatibleDC(pDC);与当前dc兼容
- dcCompatible.SelectObject(&bitmap);选入位图
- CRect rect;
- GetClientRect(&rect);得到客户端大小
- // pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);
- pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,
- 0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
- 屏蔽基类// return CView::OnEraseBkgnd(pDC);
- dc.SetPixel(point,m_clr);画点
- dc.MoveTo(m_ptOrigin);画线
- dc.LineTo(point);
- dc.Rectangle(CRect(m_ptOrigin,point));画矩形
- dc.Ellipse(CRect(m_ptOrigin,point));画椭圆
- CClientDC dc(this);
- CPen pen(m_nLineStyle,m_nLineWidth,m_clr);创建画笔
- dc.SelectObject(&pen);
- CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));画刷
- dc.SelectObject(pBrush);
- CColorDialog 颜色选择对话框
- CColorDialog dlg;
- COLORREF m_clr;
- dlg.m_cc.Flags|=CC_RGBINIT | CC_FULLOPEN;
- dlg.m_cc.rgbResult=m_clr;
- if(IDOK==dlg.DoModal())
- {
- m_clr=dlg.m_cc.rgbResult;
- }
- CFontDialog dlg; 字体对话框
- CFont m_font;
- void CGraphicView::OnFont()
- {
- // TODO: Add your command handler code here
- CFontDialog dlg;
- if(IDOK==dlg.DoModal())
- {
- if(m_font.m_hObject) 释放字体资源
- m_font.DeleteObject();
- m_font.CreateFontIndirect(dlg.m_cf.lpLogFont);
- m_strFontName=dlg.m_cf.lpLogFont->lfFaceName;
- Invalidate();
- }
- }
- Wm_ctlcolor控件颜色设置
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);设置对话框画刷设置背景色
- return hbr;
- HBRUSH CSettingDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- // TODO: Change any attributes of the DC here
- if(pWnd->GetDlgCtrlID()==IDC_LINE_STYLE)
- {
- pDC->SetTextColor(RGB(255,0,0));设置文本颜色
- pDC->SetBkMode(TRANSPARENT);设置背景模式
- return m_brush;
- }
- if(pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH)
- {
- pDC->SetTextColor(RGB(255,0,0));设置文本颜色
- //pDC->SetBkMode(TRANSPARENT);设置背景混合模式
- pDC->SetBkColor(RGB(0,0,255));设置背景色
- return m_brush;
- }
- if(pWnd->GetDlgCtrlID()==IDC_TEXT)
- {
- pDC->SelectObject(&m_font);
- }
- /*if(pWnd->GetDlgCtrlID()==IDOK)
- {
- pDC->SetTextColor(RGB(255,0,0));
- return m_brush;
- }*/
- // TODO: Return a different brush if the default is not desired
- return hbr;
- //return m_brush;
- }
本文出自 “不曾远去” 博客,谢绝转载!