zoukankan      html  css  js  c++  java
  • MFC(继续画图,孙鑫C++第十讲笔记整理)

    原文转自:http://blog.csdn.net/zh634455283/article/details/7875341

    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);

     

    具体细节:

     

    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 CHuiTuView::OnLButtonDown(UINT nFlags, CPoint point)   
    {  
        // TODO: Add your message handler code here and/or call default  
          
        m_yuandian=point;  
        CView::OnLButtonDown(nFlags, point);  
    }  
    void CHuiTuView::OnLButtonUp(UINT nFlags, CPoint point)   
    {  
        // TODO: Add your message handler code here and/or call default  
          
        CClientDC cdcc(this);  
          
        HBRUSH hbrush=(HBRUSH)GetStockObject(NULL_BRUSH);  
        CBrush *oldbrush=cdcc.SelectObject(CBrush::FromHandle(hbrush));  
      
        CPen cpen(m_xianleixing,m_xiandaxiao,RGB(255,0,0));  
        CPen *oldpen=cdcc.SelectObject(&cpen);  
      
        switch(m_huastyle)  
        {  
        case 0:  
      
            cdcc.SetPixel(point,RGB(255,0,0));  
            break;  
              
        case 1:  
            cdcc.MoveTo(m_yuandian);  
            cdcc.LineTo(point);  
            break;  
              
        case 2:  
            cdcc.Rectangle(&CRect(m_yuandian,point));  
            break;  
              
        case 3:  
            cdcc.Ellipse(&CRect(m_yuandian,point));  
            break;  
              
        default:  
            break;  
        }  
          
        cdcc.SelectObject(oldbrush);  
        cdcc.SelectObject(oldpen);  
        CView::OnLButtonUp(nFlags, point);  
    }  
    void CHuiTuView::OnSetting()   
    {  
        // TODO: Add your command handler code here  
        CHuituDlg cdlg;  
      
        cdlg.m_xiankuai=m_xiandaxiao;  
        cdlg.m_leixing=m_xianleixing;  
        if(IDOK==cdlg.DoModal())  
        {  
            UpdateData();  
            m_xiandaxiao=cdlg.m_xiankuai;  
            m_xianleixing=cdlg.m_leixing;  
        }  
      
    }  
    
    void CHuiTuView::OnDian()   
    {  
        // TODO: Add your command handler code here  
        m_huastyle=0;  
          
    }  
      
    void CHuiTuView::OnLine()   
    {  
        // TODO: Add your command handler code here  
        m_huastyle=1;  
    }  
      
    void CHuiTuView::OnJuxing()   
    {  
        // TODO: Add your command handler code here  
        m_huastyle=2;  
    }  
      
    void CHuiTuView::OnYuan()   
    {  
        // TODO: Add your command handler code here  
        m_huastyle=3;  
    }  


    edit挂链一个UINT整形,类型radio关联一个int

     

    调用系统的调色板

    1菜单添加一个“颜色”选项 ID_COLOR

    2为这个COLOR添加事件

    void CHuiTuView::OnColor()   
    {  
        // TODO: Add your command handler code here  
      
        CColorDialog  ccdlg;  
        ccdlg.m_cc.Flags|= CC_FULLOPEN | CC_RGBINIT;  
          
        ccdlg.m_cc.rgbResult=m_yanse;  
        if(IDOK==ccdlg.DoModal())  
        {  
            m_yanse=ccdlg.m_cc.rgbResult;  
        }  
    }




    3在CXXView中定义成员变量m_yanse,然后画图的时候,用这个去创建就行了

    创建字体对话框也是一样的步骤

    void CHuiTuView::OnZiti()   
    {  
        // TODO: Add your command handler code here  
          
        CFontDialog cfdlg;  
          
        if(IDOK==cfdlg.DoModal())  
        {  
            if(m_ziti.m_hObject)  
            {  
            m_ziti.DeleteObject();  
              
            }  
            m_ziti.CreateFontIndirect(cfdlg.m_cf.lpLogFont);  
            m_zitimingzi=cfdlg.m_cf.lpLogFont->lfFaceName;  
        }  
        Invalidate();  
    }  


    一个选择线条的示例

    void CHuituDlg::OnChangeXiankuan()   
    {  
        // TODO: If this is a RICHEDIT control, the control will not  
        // send this notification unless you override the CDialog::OnInitDialog()  
        // function and call CRichEditCtrl().SetEventMask()  
        // with the ENM_CHANGE flag ORed into the mask.  
          
        // TODO: Add your control notification handler code here  
      
        Invalidate();  
          
    }  
      
    void CHuituDlg::OnRadio1()   
    {  
        // TODO: Add your control notification handler code here  
        Invalidate();  
    }  
      
    void CHuituDlg::OnRadio2()   
    {  
        // TODO: Add your control notification handler code here  
        Invalidate();     
    }  
      
    void CHuituDlg::OnRadio3()   
    {  
        // TODO: Add your control notification handler code here  
        Invalidate();  
    }  
      
    void CHuituDlg::OnPaint()   
    {  
        CPaintDC dc(this); // device context for painting  
          
        // TODO: Add your message handler code here  
          
        UpdateData();  
        CClientDC ccdc(this);  
        CPen cpen(m_leixing,m_xiankuai,RGB(255,0,0));//可以设置一个成员变量接收来自调色板的颜色  
        ccdc.SelectObject(&cpen);  
      
        CRect crect;  
        GetDlgItem(ID_SHILI)->GetWindowRect(&crect);  
          
        ScreenToClient(&crect);  
        TEXTMETRIC tm;  
        ccdc.GetTextMetrics(&tm);  
        ccdc.MoveTo(crect.left+20,crect.top+crect.Height()/2);  
        ccdc.LineTo(crect.right-20,crect.top+crect.Height()/2);  
      
      
      
          
        // Do not call CDialog::OnPaint() for painting messages  
    }  


    当进行数据交互是要UpdateData

    能够立即交互

     

    在对话框中添加WM_CTLCOLOR消息

    HBRUSH CHuituDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
    {  
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
          
        // TODO: Change any attributes of the DC here  
          
        // TODO: Return a different brush if the default is not desired  
      
      
        return  m_myBrush;  
        //return hbr;  
    }  


    m_mBrush.CreateSolidBrush(RGB(0,255,0))
    返回自己的画刷,对话框中的控件绘制的时候 就会触发这个消息

    HBRUSH CHuituDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
    {  
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
          
        // TODO: Change any attributes of the DC here  
          
        // TODO: Return a different brush if the default is not desired  
          
        if(pWnd->GetDlgCtrlID()==ID_LEIXINGKUANGKUANG)  
        {  
            pDC->SetBkColor(RGB(255,0,0));  
            pDC->SetTextColor(RGB(255,255,255));  
            return m_myBrush;  
        }  
      
        if(pWnd->GetDlgCtrlID()==ID_XIANKUAN)  
        {  
            pDC->SetBkColor(RGB(255,0,0));  
            pDC->SetTextColor(RGB(255,255,255));  
            return m_myBrush;  
        }  
      
        if(pWnd->GetDlgCtrlID()==ID_MFC)  
        {  
            pDC->SelectObject(&mfcziti);  
        }  
      
        if(pWnd->GetDlgCtrlID()==IDOK)  
        {  
            pDC->SetTextColor(RGB(255,0,0));  
            pDC->SetBkMode(TRANSPARENT);  
        }  
      
        //return  m_myBrush;  
        return hbr;  
    }  

    现在没有办法改变BUTTon的属性,只能通过它的虚函数DrawItem去改变

    下面新建一个CBUTTOn的派生类,然后去覆写DrawItem函数

    然后关联控制,将OK关联到新建的派生类

     

    如果还想改变按钮的背景颜色或其它的,可以使用组建重用,即利用别人写好的现成的类

    把类和头文件都添加项目目录里面,然后将按钮关联到 组件类,然后可以在OnInitDlg中新添加一些属性。这里引用牛人写的CButtonST类

    BOOL CHuituDlg::OnInitDialog()   
    {  
        CDialog::OnInitDialog();  
          
        // TODO: Add extra initialization here  
        m_paisheng.SetActiveBgColor(RGB(255,0,0));  
        m_paisheng.SetActiveFgColor(RGB(0,255,0));  
        return TRUE;  // return TRUE unless you set the focus to a control  
                      // EXCEPTION: OCX Property Pages should return FALSE  
    }  


    位图:

    在CXXView中 添加WM_ERASEBKGND消息

    BOOL CHuiTuView::OnEraseBkgnd(CDC* pDC)   
    {  
        // TODO: Add your message handler code here and/or call default  
      
        //MessageBeep(0);这句用来测试,擦除是什么时候触发的,在调用ONPaint之前触发的  
      
      
        CBitmap cbitmap;  
        cbitmap.LoadBitmap(IDB_BITMAP1);  
      
      
        CDC memDC;  
        memDC.CreateCompatibleDC(pDC);  
        memDC.SelectObject(&cbitmap);  
      
        CRect crect;  
        GetClientRect(&crect);  
        pDC->BitBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,SRCCOPY);  
      
              
        return TRUE;  
        //return CView::OnEraseBkgnd(pDC);  
    }  

    [cpp] view plaincopy


    1:1复制的

    下面还有个可以拉伸的

    CBitmap有个成员方法

    这样就可以获取位图的宽度和高度了

    BOOL CHuiTuView::OnEraseBkgnd(CDC* pDC)   
    {  
        // TODO: Add your message handler code here and/or call default  
      
        //MessageBeep(0);这句用来测试,擦除是什么时候触发的,在调用ONPaint之前触发的  
      
      
        CBitmap cbitmap;  
        cbitmap.LoadBitmap(IDB_BITMAP1);  
      
        BITMAP bitmap;  
        cbitmap.GetBitmap(&bitmap);  
      
        CDC memDC;  
        memDC.CreateCompatibleDC(pDC);  
        memDC.SelectObject(&cbitmap);  
      
        CRect crect;  
        GetClientRect(&crect);  
        //pDC->BitBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,SRCCOPY);  
        pDC->StretchBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);  
              
              
        return TRUE;  
        //return CView::OnEraseBkgnd(pDC);  
    }  



    上面的代码可以写在OnPaint或OnDraw中
  • 相关阅读:
    【用程序思维学习英语】
    【python3】修饰器简单理解
    【FLASK】发送QQ邮件
    【FLASK】数据库迁移
    【python3】with的用法
    【flask】工厂函数和蓝本的作用
    使用Python中的xltpl模块填充excel表格模板文件
    Python添加excel表格的批注
    在原有表格基础上面进行添加内容修改格式等操作
    Python操作excel表格库的介绍
  • 原文地址:https://www.cnblogs.com/SunkingYang/p/11049263.html
Copyright © 2011-2022 走看看