现在的位置: 首页 > 综合 > 正文

MFC 绘图控制 — 总结

2018年02月07日 ⁄ 综合 ⁄ 共 3616字 ⁄ 字号 评论关闭

一、简单绘图

下面简单实现一个绘图功能:
    1.在视图类CXXXView 的头文件中添加四个宏定义:
       #define    DRAW_DOT             1
       #define    DRAW_LINE             2
       #define    DRAW_RECT           3
       #define    DRAW_ELLIPSE      4

    2.为视图类CXXXView 添加两个成员变量: 
       private:
            UINT     m_drawStyle;
            CPoint   m_ptOrigin;

    3.打开资源编辑器,最主菜单的最后添加一个“绘图”子菜单,为此子菜单再添加四个菜单项:
        点          IDM_DOT
        直线       IDM_LINE
        矩形       IDM_RECTANGLE
        椭圆       IDM_ELLIPSE

    4.在视图类 CXXXView 中截获这四个菜单命令消息,并实现之:
          On_Dot                :     m_drawStyle  = DRAW_DOT;
          On_Line               :     m_drawStyle  = DRAW_LINE;
          On_Rectangle      :     m_drawStyle  = DRAW_RECT;
          On_Ellipse           :     m_drawStyle  = DRAW_ELLIPSE;

    5.为视图类 CXXXView 实现LBUTTONDOWN 同 LBUTTONUP 命令消息,并实现之:
         OnLButtonDown:
               m_ptOrigin  = point;     // point 是OnLButtonDown 函数的一个形参

         OnLButtonUp:
              CClientDC     dc(this);
         switch (m_drawStyle)
         {
             case DRAW_DOT:
                       dc.SetPixel(point, m_color);
                       break;
            case DRAW_LINE:
                      dc.MoveTo(m_ptOrigin);
                      dc.LineTo(point);
                      break;
            case DRAW_RECT:
                      dc.Rectangle(CRect(m_ptOrigin, point));
                      break;
           case DRAW_ELLIPSE:
                     dc.Ellipse(CRect(m_ptOrigin, point));
                     break;
           default:
                     break;
         }

 

二、设置对话框

设置线宽:

    1.为视图类 CXXXView 添加一个成员变量:
       private:
              UINT    m_nLineWidth;

    2.打开资源编辑器,新创建一个对话框资源并添加两个控件:
       静态文本控件, 编辑框控件(ID重写为IDC_LINE_WIDTH)。
       为新建的对话框创建一个类:CSettingDlg,基类为CDialog。
       为对话框中的编辑框控件关联一个变量:
        public:
              UINT  m_nLineWidth;

    3.在视图类的主菜单新添加一个菜单项,ID为新建对话框ID:IDM_SETTING。
       在视图类CXXXView中添加命令消息IDM_SETTING的响应函数OnSetting,如下代码:

       CSetting    dlg;
       dlg.m_nLineWidth  = m_nLineWidth;
       if (IDOK  == dlg.DoModal())
       {
             m_nLineWidth = dlg.m_nLineWidth;
       }

 

 

设置线型:

    1.为视图类 CXXXView 添加一个成员变量:
       private:
             UINT    m_nLineStyle;

       并在其构造函数中将 m_nLineStyle 初始化为-1;

 

    2.为之前创建的对话框再添加两个控件:
       组框控件,三个放在组框中的单选按钮:实线,虚线,点。
       第一个单选钮选中Group,并为此些单选按钮关联一个变量:

       public:
              UINT  m_nLineStyle;

 

    3.在视图类CXXXView中添加命令消息IDM_SETTING的响应函数OnSetting,如下代码:

       CSetting    dlg;
       dlg.m_nLineWidth  = m_nLineWidth;
       dlg.m_nLineStyle   = m_nLineStyle;
       if (IDOK  == dlg.DoModal())
       {
            m_nLineWidth = dlg.m_nLineWidth;
            m_nLineStyle = dlg.m_nLineStyle;
       }

三、颜色对话框

    1.为视图类CXXXView 添加一个成员变量:
        private:
             COLORREF   m_color;

    2.在视图类CXXXView 主菜单中添加一个菜单项:颜色对话框,ID为IDM_COLOR
       并为IDM_COLOR消息命令添加响应函数OnColor

        CColorDialog       dlg;
        dlg.m_cc.Flags     |= CCRGBINIT;
        dlg.m_cc.rgbResult  = m_color;

        if (IDOK == dlg.DoModal())
        {
               m_color = dlg.m_cc.rgbResult;
        }

 

四、字体对话框

    1.为视图类CXXXView 添加两个成员变量:
        private:
             CFont     m_font;
             CString  m_strFontName;

    2.在视图类CXXXView 主菜单中添加一个菜单项:字体对话框,ID为IDM_FONT
      并为IDM_FONT消息命令添加响应函数OnFont:

      CFontDialog    dlg;
      if (IDOK == dlg.DoModal())
      {
         m_font.CreateFontIndirect(dlg.m_cf.lpLogFont);
         m_strFontName = dlg.m_cf.lpLogFont->lfFaceName;
      }

五、示例对话框

    1.在之前的设置对话框上,再添加一个组框,并将其ID设为:IDC_SAMPLE。

    2.重写该对话框类的OnPaint函数,如下代码:
      CPaintDC dc(this); // device context for painting
      // TODO: 在此处添加消息处理程序代码

      UpdateData();
      CPen  pen(m_nLineStyle, m_nLineWidth, m_color);
      dc.SelectObject(&pen);

      CRect  rect;
      GetDlgItem(IDC_SAMPLE)->GetWindowRect(&rect);
      ScreenToClient(&rect);

      dc.MoveTo(rect.left + 40, rect.top + 40);
      dc.LineTo(rect.right - 40, rect.bottom - 40);

 

 

 

 

 

抱歉!评论已关闭.