zoukankan      html  css  js  c++  java
  • MFC—RichEdit控件有关使用

    注意:MFC中使用RichEdit控件时需要在RichEditUse.cpp文件中的InitInstance()函数中添加初始化函数AfxInitRichEdit2();

    1、RichEdit控件简单应用

    为方便演示本例创建RichContrl基于对话框,生成CRichContrlDlg.h和CRichContrlDlg.cpp两个文件,在.cpp文件中宏定义:

     #define     COLOR_BLUE       RGB(0, 0, 128)
     #define     COLOR_RED        RGB(0xFF, 0, 0)
     #define     COLOR_GREEN      RGB(0x2e, 0x8b, 0x57)
     #define     COLOR_WHITE      RGB(255, 255, 255)
     #define     COLOR_BLACK      RGB(0, 0, 0)
     #define     COLOR_YELLOW     RGB(250, 250, 60)

     从工具箱中拖拽richedit控件、editcontrl控件、按钮到对话框中,ID分别为IDC_RICHEDIT_SHOW、IDC_EDIT_INPUT,然后在CRichContrlDlg.h中申明函数void DrawTextToREdit(CString szMessage, UINT uiREditId, COLORREF crTextCr, COLORREF crBackCr, int nFontSize);返回CRichContrlDlg.cpp实现函数DrawTextToREdit()如下:

     1 void CRichContrlDlg::DrawTextToREdit(CString szMessage, UINT uiREditId, COLORREF crTextCr, COLORREF crBackCr, int nFontSize)
     2   {
     3       CRichEditCtrl* pREdit = (CRichEditCtrl*)this->GetDlgItem(uiREditId);
     4       CFont font;
     5       font.CreateFont(
     6           nFontSize,            //字体的高度
     7           0,                    //字体的宽度
     8           0,                    //字体的倾斜角
     9           0,                    //字体的倾斜角
    10          FW_LIGHT,             //字体的粗细
    11          FALSE,                //字体是否为斜体
    12          FALSE,                  //字体是否为下划线
    13          FALSE,                //字体是否有删除线
    14          ANSI_CHARSET,         //字体使用的字符集
    15          OUT_TT_PRECIS,        //指定如何选择合适的字体
    16          CLIP_DEFAULT_PRECIS,  //用来确定裁剪的精度
    17          PROOF_QUALITY,        //怎么样跟选择的字体相符合
    18          FF_MODERN | FIXED_PITCH,//距标志和属性标志
    19          _T("楷体")            //字体的名称
    20      );
    21      this->GetDlgItem(uiREditId)->SetFont(&font);
    22      pREdit->SetBackgroundColor(FALSE, crBackCr);
    23      CHARFORMAT cf;
    24      ::memset(&cf, 0, sizeof(cf));
    25      cf.dwMask = CFM_COLOR | CFM_BOLD;
    26      cf.dwEffects = CFE_BOLD;
    27      cf.crTextColor = crTextCr;
    28      pREdit->SetDefaultCharFormat(cf);
    29      this->SetDlgItemText(uiREditId, szMessage);
    30  }
    View Code

    给按钮添加事件处理程序,调用DrawTextToREdit()函数:

    void CRichContrlDlg::OnBnClickedButton1()
    {
        // TODO: 在此添加控件通知处理程序代码
        CString input_str;
        this->GetDlgItemText(IDC_EDIT_INPUT, input_str);
        DrawTextToREdit(input_str, IDC_RICHEDIT_SHOW, COLOR_WHITE, COLOR_GREEN, 120);
    }

    效果截图:

    2、RichEdit控件和Check控件结合使用

    新建一个基于对话框的MFC程序,工具箱中拖拽三个复选框控件、richedit控件、按钮控件到对话框中,并在CRichEditUseDlg.h文件中添加控件变量,如下所示:

    public:
        afx_msg void OnTextColor();
        afx_msg void OnBold();
        afx_msg void OnItalic();
        afx_msg void OnUnderline();
    
        CRichEditCtrl m_RichSample; //RichEdit控件变量
        CButton m_ctlBold;          //粗体复选框
        CButton m_ctlItalic;        //斜体复选框
        CButton m_ctlUnderline;     //下划线复选框

     接下来在CRichEditUseDlg.cpp文件中分别给各个控件添加事件处理函数:

     1 //按钮的事件处理函数
     2 void CRichEditUseDlg::OnTextColor()
     3 {
     4     // TODO: 在此添加控件通知处理程序代码
     5     CHARFORMAT cf;
     6     memset(&cf, '', sizeof(CHARFORMAT));
     7     cf.dwMask = CFM_COLOR;
     8     m_RichSample.GetSelectionCharFormat(cf);
     9     CColorDialog cc(cf.crTextColor);   //弹出颜色选择对话框
    10     if (cc.DoModal() == IDCANCEL) 
    11     {
    12         return;
    13     }
    14     cf.dwMask = CFM_COLOR;
    15     cf.dwEffects &= ~CFE_AUTOCOLOR;
    16     cf.crTextColor = cc.GetColor();
    17     m_RichSample.SetSelectionCharFormat(cf);
    18     m_RichSample.SetFocus();
    19 }
    20 
    21 //粗体复选框事件处理函数
    22 void CRichEditUseDlg::OnBold()
    23 {
    24     // TODO: 在此添加控件通知处理程序代码
    25     CHARFORMAT cf;               //结构体
    26     m_RichSample.GetSelectionCharFormat(cf);
    27     cf.dwMask |= CFM_BOLD;
    28     cf.dwEffects ^= CFE_BOLD;
    29     m_RichSample.SetSelectionCharFormat(cf);
    30     if (cf.dwEffects & CFE_BOLD) //当前为粗体
    31     {
    32         m_ctlBold.SetCheck(1);   //设置按钮被选
    33     }
    34     else                         //当前不为粗体
    35     {
    36         m_ctlBold.SetCheck(0);   //设置按钮未被选中
    37     }
    38     m_RichSample.SetFocus();
    39 }
    40 
    41 //斜体复选框事件处理函数
    42 void CRichEditUseDlg::OnItalic()
    43 {
    44     // TODO: 在此添加控件通知处理程序代码
    45     CHARFORMAT cf;
    46     m_RichSample.GetSelectionCharFormat(cf);
    47     cf.dwMask |= CFM_ITALIC;
    48     cf.dwEffects ^= CFE_ITALIC;
    49     m_RichSample.SetSelectionCharFormat(cf);
    50     if (cf.dwEffects & CFM_ITALIC)     //当前为斜体
    51     {
    52         m_ctlItalic.SetCheck(1);       //设置按钮被选中
    53     }
    54     else                               //当前不为斜体
    55     {
    56         m_ctlItalic.SetCheck(0);       //设置按钮未被选中
    57     }
    58     m_RichSample.SetFocus();
    59 }
    60 
    61 //下划线复选框事件处理函数
    62 void CRichEditUseDlg::OnUnderline()
    63 {
    64     // TODO: 在此添加控件通知处理程序代码
    65     CHARFORMAT cf;
    66     m_RichSample.GetSelectionCharFormat(cf);
    67     cf.dwMask |= CFM_UNDERLINE;
    68     cf.dwEffects ^= CFE_UNDERLINE;
    69     m_RichSample.SetSelectionCharFormat(cf);
    70     if (cf.dwEffects & CFM_UNDERLINE)    //当前带有下划线
    71     {
    72         m_ctlUnderline.SetCheck(1);        //设置按钮被选中
    73     }
    74     else                                //当前不带有下划线
    75     {
    76         m_ctlUnderline.SetCheck(0);     //设置按钮未被选中
    77     }
    78     m_RichSample.SetFocus();
    79 }
    View Code

    效果图:

     3、从文件中读取内容并存入数组中

    添加类TestItem,在该类中申明:

    class TestItem
    {
    public:
        TestItem(void);
        ~TestItem(void);
        CString m_szIteamName;
        CString m_szSpec1;
        CString m_szSpec2;
        CString m_szSpec3;
        CString m_szSpec4;
        CString m_szSpec5;
        CString m_szSpec6;
    };

    对应的.cpp文件中初始化变量:

    #include "pch.h"
    #include "TestItem.h"
    TestItem::TestItem(void)
    {
        this->m_szIteamName = _T("");
        this->m_szSpec1 = _T("");
        this->m_szSpec2 = _T("");
        this->m_szSpec3 = _T("");
        this->m_szSpec4 = _T("");
        this->m_szSpec5 = _T("");
        this->m_szSpec6 = _T("");
    }
    TestItem::~TestItem(void) {
    }

    在对话框中实现函数:

     1 void CThreadTestDlg::InitItem() {
     2     //项目名全部暂时清空
     3     for (int i = 0;i < 50;i++) {
     4         this->m_objTestItem[i].m_szIteamName = _T("");
     5     }
     6     CString szCsvPath = _T(".\temp.log");
     7     CStdioFile file;
     8     if (file.Open(szCsvPath, CFile::modeRead | CFile::shareDenyNone)) {
     9         int nItemCount = 0;
    10         CString sz1LineStr;
    11         while (file.ReadString(sz1LineStr))
    12         {
    13             CString szItemTmp = _T("");
    14             // 0
    15             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    16             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    17             this->m_objTestItem[nItemCount].m_szIteamName = szItemTmp;
    18             // 1
    19             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    20             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    21             this->m_objTestItem[nItemCount].m_szSpec1 = szItemTmp;
    22             // 2
    23             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    24             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    25             this->m_objTestItem[nItemCount].m_szSpec2 = szItemTmp;
    26             // 3
    27             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    28             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    29             this->m_objTestItem[nItemCount].m_szSpec3 = szItemTmp;
    30             // 4
    31             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    32             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    33             this->m_objTestItem[nItemCount].m_szSpec4 = szItemTmp;
    34             // 5
    35             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    36             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    37             this->m_objTestItem[nItemCount].m_szSpec5 = szItemTmp;
    38             // 6
    39             szItemTmp = sz1LineStr.SpanExcluding(_T(","));
    40             sz1LineStr.Delete(0, szItemTmp.GetLength() + 1);
    41             this->m_objTestItem[nItemCount].m_szSpec6 = szItemTmp;
    42             nItemCount++;
    43         }
    44     }
    45 }
  • 相关阅读:
    lnmp 优化
    linux-lnmp 搭建报错
    nfs 配置
    全网备份脚本rsync
    .Net面试题二
    软件设计模式
    .Net面试题一
    asp.net运行机制
    NHiberante的优缺点
    什么是架构、框架、模式和平台
  • 原文地址:https://www.cnblogs.com/geziyu/p/14109650.html
Copyright © 2011-2022 走看看