10图形的绘制和常用对话框操作

转自   http://blog.sina.com.cn/s/articlelist_1815328704_0_1.html
1.画图:
   a.创建四个菜单,为其添加消息响应;
   b.在View中添加m_DrawType,保存绘画类型;
   c.增加成员变量,m_PtOrigin,当按下鼠标左键时,保存此点;
   d.在OnLButtonUp中画点,线,矩形,椭圆,别忘记设置成透明画刷


2.为其添加一个设置对话框(线型和线宽)
   a.创建对话框,为其创建一个新类关联它;
   b.为其中的线宽关联成员变量;
   c.在View中增加一个菜单,响应新的对话框;
   d.添加线型选项设置,将其Group属性选中,并为单选按纽关联成员变量。在view中增加一个线型变量m_nLineStyle


3.添加一个颜色对话框
   a.实例化一个CColorDialog
   b.调用DoModal方法
4.添加字体对话框,将选择的字体在View中显示出来。
   a.实例化一个对象;
   b.为View添加一个字体成员变量,得到用户选择的字体。
   c.调用Invadate()发出重绘消息;
   d.再次注意一个对象只能创建一次,故要再次创建,必须将原告的删除!
5.为设置对话框增加示例功能。
   a.当控件内容改变时,发出En_change消息。而Radio按纽则为Clicked。需先UpdateData()。另外还需要ScreenToClient(&rect)
6.改变对话框的背景色和控件颜色。
  每个控件被绘制时都发出WM_CTlColor消息,
7.如何改变OK按纽的字体和背景?
  OK按纽
  a.创建一个新类,CTestBtn,基类为CButton
  b.在类中增加虚函数,DrawItem,添加代码。
  c.将OK按纽关联成员变量。类型为CTestBtn,注意将OK按纽的OwnerDraw特性选中。
  Cancel按纽
  用新类来改变。
  a.加入新文件。
  b.为Cancel关联一个成员变量,类型为CSXBtn;
  c.调用CSXBtn的方法。
  Cancel2按纽
  a.方法同上。


8.在窗口中贴图,4个步骤
1、创建位图
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
2、创建兼容DC
CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);
3、将位图选到兼容DC中
dcCompatible.SelectObject(&bitmap);
4、将兼容DC中的位图贴到当前DC中。在WM_EraseBkgnd()中调用,但不能再调用基类的擦除背景函数。也可以在OnDraw函数中完成,但效率低,图像会闪烁,因为它先擦除背景,慢。
pDC->BitBlt(rect.left,rect.top,rect.Width(),
rect.Height(),&dcCompatible,0,0,SRCCOPY);
 
 
void CGraphicView::OnDraw(CDC* pDC)
{
    CGraphicDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
 
 
    CFont *pOldFont=pDC->SelectObject(&m_font);
    pDC->TextOut(0,0,m_strFontName);
    pDC->SelectObject(pOldFont);
}
 
void CGraphicView::OnDot()
{
    // TODO: Add your command handler code here
    m_nDrawType=1;
}
 
void CGraphicView::OnLine()
{
    // TODO: Add your command handler code here
    m_nDrawType=2;
}
 
void CGraphicView::OnRectangle()
{
    // TODO: Add your command handler code here
    m_nDrawType=3;
}
 
void CGraphicView::OnEllipse()
{
    // TODO: Add your command handler code here
    m_nDrawType=4;
}
 
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_ptOrigin=point;
    CView::OnLButtonDown(nFlags, point);
}
 
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    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);
    switch(m_nDrawType)
    {
    case 1:
       dc.SetPixel(point,m_clr);
       break;
    case 2:
       dc.MoveTo(m_ptOrigin);
       dc.LineTo(point);
       break;
    case 3:
       dc.Rectangle(CRect(m_ptOrigin,point));
       break;
    case 4:
       dc.Ellipse(CRect(m_ptOrigin,point));
       break;
    }
    CView::OnLButtonUp(nFlags, point);
}
 
void CGraphicView::OnSetting()
{
    // TODO: Add your command handler code here
    CSettingDlg dlg;
    dlg.m_nLineWidth=m_nLineWidth;
    dlg.m_nLineStyle=m_nLineStyle;
    dlg.m_clr=m_clr;
    if(IDOK==dlg.DoModal())
    {
       m_nLineWidth=dlg.m_nLineWidth;
       m_nLineStyle=dlg.m_nLineStyle;
    }
}
 
void CGraphicView::OnColor()
{
    // TODO: Add your command handler code here
    CColorDialog dlg;
    dlg.m_cc.Flags|=CC_RGBINIT | CC_FULLOPEN;
    dlg.m_cc.rgbResult=m_clr;
    if(IDOK==dlg.DoModal())
    {
       m_clr=dlg.m_cc.rgbResult;
    }
}
 
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();
    }
}
 
BOOL CGraphicView::OnEraseBkgnd(CDC* pDC)
{
    // TODO: Add your message handler code here and/or call default
    CBitmap bitmap;
    bitmap.LoadBitmap(IDB_BITMAP1);
 
    BITMAP bmp;
    bitmap.GetBitmap(&bmp);
 
    CDC dcCompatible;
    dcCompatible.CreateCompatibleDC(pDC);
 
    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 TRUE;
//  return CView::OnEraseBkgnd(pDC);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值