VC中在对话框上使用Rich Edit控件前一定要用AfxInitRichEdit()初始化RichEd...

1.设置edit只读属性

方法一:
                m_edit1.SetReadOnly(TRUE);
    方法二:
                ::SendMessage(m_edit1.m_hWnd, EM_SETREADONLY, TRUE, 0);


2.判断edit中光标状态并得到选中内容(richedit同样适用)

int nStart, nEnd;
        CString strTemp;

m_edit1.GetSel(nStart, nEnd);
        if(nStart == nEnd)
        {
            strTemp.Format(_T("光标在%d"), nStart);
            AfxMessageBox(strTemp);
        }
        else
        {
            //得到edit选中的内容    
            m_edit1.GetWindowText(strTemp);
            strTemp = strTemp.Mid(nStart) - strTemp.Mid(nEnd);
            AfxMessageBox(strTemp);
        }
    注:GetSel后,如果nStart和nEnd,表明光标处于某个位置(直观来看就是光标在闪动);
             如果nStart和nEnd不相等,表明用户在edit中选中了一段内容。


3.在edit最后添加字符串

CString str;
        m_edit1.SetSel(-1, -1);
        m_edit1.ReplaceSel(str);


4.随输入自动滚动到最后一行(richedit同样适用)

方法一:(摘自msdn)
        // The pointer to my edit.
        extern CEdit* pmyEdit;
        int nFirstVisible = pmyEdit->GetFirstVisibleLine();

// Scroll the edit control so that the first visible line
        // is the first line of text.
        if (nFirstVisible > 0)
        {
            pmyEdit->LineScroll(-nFirstVisible, 0);
        }
    方法二:
        m_richedit.PostMessage(WM_VSCROLL, SB_BOTTOM, 0);


5.如何限制edit输入指定字符

可以从CEdit派生一个类,添加WM_CHAR消息映射。下面一个例子实现了限定输入16进制字符的功能。

void CMyHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
   {
        if ( (nChar >= '0' && nChar <= '9') ||
             (nChar >= 'a' && nChar <= 'f') ||
         (nChar >= 'A' && nChar <= 'F') ||
              nChar == VK_BACK ||
              nChar == VK_DELETE)    //msdn的virtual key
       {
            CEdit::OnChar(nChar, nRepCnt, nFlags);
        }   
   }


6.如何使用richedit

添加AfxInitRichEdit();
       CxxxApp::InitInstance()
        {
             AfxInitRichEdit();
          .............
       }

AfxInitRichEdit()功能:装载 RichEdit 1.0 Control (RICHED32.DLL).


7.如何使用richedit2.0 or richedit3.0

使用原因:由于RichEdit2.0A自动为宽字符(WideChar),所以它可以解决中文乱码以及一些汉字问题

方法一:(msdn上的做法,适用于用VC.NET及以后版本创建的工程)
            To update rich edit controls in existing Visual C++ applications to version 2.0,
            open the .RC file as text, change the class name of each rich edit control from   "RICHEDIT" to "RichEdit20a".
            Then replace the call to AfxInitRichEdit with AfxInitRichEdit2.
    方法二:以对话框为例:
       (1)    增加一全局变量 HMODULE hMod;
       (2)    在CxxxApp::InitInstance()中添加一句hMod = LoadLibrary(_T("riched20.dll"));
            在CxxxApp::ExitInstance()中添加一句FreeLibrary(hMod);
       (3)    在对话框上放一个richedit,文本方式打开.rc文件修改该richedit控件的类名"RICHEDIT" to "RichEdit20a".
       (4)    在对话框头文件添加 CRichEditCtrl m_richedit;
            在OnInitDialog中添加 m_richedit.SubclassDlgItem(IDC_RICHEDIT1, this);


8.改变richedit指定区域的颜色及字体

CHARFORMAT cf;
        ZeroMemory(&cf, sizeof(CHARFORMAT));
        cf.cbSize = sizeof(CHARFORMAT);
        cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
                            CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
        cf.dwEffects = 0;
        cf.yHeight = 12*12;//文字高度
        cf.crTextColor = RGB(200, 100, 255); //文字颜色
        strcpy(cf.szFaceName ,_T("隶书"));//设置字体
    
        m_richedit1.SetSel(1, 5); //设置处理区域
        m_richedit1.SetSelectionCharFormat(cf);


9.设置行间距(只适用于richedit2.0)

PARAFORMAT2 pf;
        pf2.cbSize = sizeof(PARAFORMAT2);
        pf2.dwMask = PFM_LINESPACING | PFM_SPACEAFTER;
        pf2.dyLineSpacing = 200;
        pf2.bLineSpacingRule = 4;
        m_richedit.SetParaFormat(pf2);


10.richedit插入位图

Q220844:How to insert a bitmap into an RTF document using the RichEdit control in Visual C++ 6.0
http://support.microsoft.com/default.aspx?scid=kb;en-us;220844
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2417/
http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/


11.richedit插入gif动画

http://www.codeproject.com/richedit/AnimatedEmoticon.asp


12.richedit嵌入ole对象

http://support.microsoft.com/kb/141549/en-us


13.使richedit选中内容只读

http://www.codeguru.com/cpp/controls/richedit/article.php/c2401/


14.打印richedit

http://www.protext.com/MFC/RichEdit3.htm


15.richeidt用于聊天消息窗口

http://www.vckbase.com/document/viewdoc/?id=1087
http://www.codeproject.com/richedit/chatrichedit.asp
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2395/


16.解决richedit的EN_SETFOCUS和EN_KILLFOCUS无响应的问题

http://support.microsoft.com/kb/181664/en-us


17.richedit拼写检查

http://www.codeproject.com/com/AutoSpellCheck.asp


18.改变edit背景色

Q117778:How to change the background color of an MFC edit control
http://support.microsoft.com/kb/117778/en-us


19.当edit控件的父窗口属性是带标题栏WS_CAPTION和子窗口WS_CHILD时,不能设置焦点SetFocus

Q230587:PRB: Can't Set Focus to an Edit Control When its Parent Is an Inactive Captioned Child Window
http://support.microsoft.com/kb/230587/en-us


20. 在Edit中回车时,会退出对话框

选中Edit的风格Want Return。

MSDN的解释如下:
ES_WANTRETURN    Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. Without this style, pressing the ENTER key has the same effect as pressing the dialog box's default pushbutton. This style has no effect on a single-line edit control.


21. 动态创建的edit没有边框的问题

m_edit.Create(....);
    m_edit.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME);


22. 一个能显示RTF,ole(包括gif, wmv,excel ,ppt)的例子

http://www.codeproject.com/richedit/COleRichEditCtrl.asp

转自

http://blog.csdn.net/lixiaosan/archive/2006/04/06/652795.aspx

Environment: VC6 SP4, 2000.

Follow these 10 easy steps to build the OutLookRichEdit control:

  1. Insert a rich edit control into the dialog.
  2. Call AfxInitRichEdit() in the InitInstance of the App class or in InitDialog.
  3. If it does not exist, copy OutLookRichEdit.cpp and OutLookRichEdit.h to the project directory.
  4. Click the menu choice Project-Add to Project-Files and select the above-copied files to add the wrapper class to your project.
  5. Import the hand cursor into the resource and rename it "IDC_LINK".
  6. Use Classwizard to add a member variable of the rich edit control (CRichEditCtrl).
  7. Include the OutLookRichEdit.h file in the dialog's header file and change the declaration of rich edit member variable, as in
    CRichEditCtrl    m_ctrlText1;

    to

    COutLookRichEdit m_ctrlText1;
  8. In InitDialog(), add the following code.
    m_ctrlText1.SetRawHyperText(_T("Click <%$here$#100#%>
    to see the about box."));

    At this level, if you build the project and run it, you can see the rich edit control with linked text, but nothing would happen if you clicked on the link.

    To Show a dialog while the link is clicked, you have to add some more code in the dialog class. Before that, have a closer look at the preceding code and hypertext syntax. The link text is enclosed between the "$" symbols and the corresponding dialog's resource value 100 (About Box), enclosed in "#" symbols.

    You can find the #define values of dialogs in the resource.h file.

  9. Use ClassWizard to map OnNotify of the dialog and write the corresponding implementation code in .cpp file, like:
    BOOL CDEMODlg::OnNotify(WPARAM wParam,
    LPARAM lParam,
    LRESULT* pResult)
    {
    NMHDR* pNmHdr = (NMHDR*) lParam;
    if(IDC_RICHEDIT1 == pNmHdr->idFrom){
    switch(pNmHdr->code)
    {
    case IDD_ABOUTBOX:
    CAboutDlg oDlg;
    oDlg.DoModal ();
    break;
    }
    }
    return CDialog::OnNotify(wParam, lParam, pResult);
    }
  10. Now, build and run the project. It is recommended that you set the read-only attribute to the rich edit control.

Downloads

Download demo project - 23 Kb
Download source - 6 Kb

在RichEdit中插入Bitmap

COleDataSource src;
STGMEDIUM sm;
sm.tymed=TYMED_GDI;
sm.hBitmap=hbmp;
sm.pUnkForRelease=NULL;
src.CacheData(CF_BITMAP, &sm);
LPDATAOBJECT lpDataObject =
(LPDATAOBJECT)src.GetInterface(&IID_IDataObject);
pRichEditOle->ImportDataObject(lpDataObject, 0, NULL);
lpDataObject->Release();

字体设置代码

最后添加字体变换函数:
CHARFORMAT cf;
LOGFONT lf;
memset(&cf, 0, sizeof(CHARFORMAT));
memset(&lf, 0, sizeof(LOGFONT));

//判断是否选择了内容
BOOL bSelect = (GetSelectionType() != SEL_EMPTY) ? TRUE : FALSE;
if (bSelect)
{
             GetSelectionCharFormat(cf);
}
else
{
             GetDefaultCharFormat(cf);
}

//得到相关字体属性
BOOL bIsBold = cf.dwEffects & CFE_BOLD;
BOOL bIsItalic = cf.dwEffects & CFE_ITALIC;
BOOL bIsUnderline = cf.dwEffects & CFE_UNDERLINE;
BOOL bIsStrickout = cf.dwEffects & CFE_STRIKEOUT;

//设置属性
lf.lfCharSet = cf.bCharSet;
lf.lfHeight = cf.yHeight/15;
lf.lfPitchAndFamily = cf.bPitchAndFamily;
lf.lfItalic = bIsItalic;
lf.lfWeight = (bIsBold ? FW_BOLD : FW_NORMAL);
lf.lfUnderline = bIsUnderline;
lf.lfStrikeOut = bIsStrickout;
sprintf(lf.lfFaceName, cf.szFaceName);

CFontDialog dlg(&lf);
dlg.m_cf.rgbColors = cf.crTextColor;
if (dlg.DoModal() == IDOK)
{
             dlg.GetCharFormat(cf);//获得所选字体的属性
             if (bSelect)
                         SetSelectionCharFormat(cf);     //为选定的内容设定所选字体
             else
                         SetWordCharFormat(cf);         //为将要输入的内容设定字体
}

在RichEdit中实现超链接

在RichEdit中实现超链接
责任编辑:admin   在CBuilder上制作 更新日期:2005-8-6
 
首先在Form上放置一个RichEdit。

在窗体的构造函数中添加以下代码:
__fastcall TMainForm::TMainForm(TComponent* Owner)
         : TForm(Owner)
{
     unsigned mask = SendMessage(RichEdit1->Handle, EM_GETEVENTMASK, 0, 0);
     SendMessage(RichEdit1->Handle, EM_SETEVENTMASK, 0, mask | ENM_LINK);
     SendMessage(RichEdit1->Handle, EM_AUTOURLDETECT, true, 0);   //自动检测URL

RichEdit1->Text = "欢迎访问C++ Builder\n"
                       "网址: http://www.ccrun.com\n"
                       "偶的信箱:\n"
                       "mailto::info@ccrun.com \n"
                       "嘿嘿\n";
}

重载窗体的WndProc

1。在.h中添加:

protected:
       virtual void __fastcall WndProc(Messages::TMessage &Message);

2。在.cpp中添加:
//---------------------------------------------------------------------------
void __fastcall TMainForm::WndProc(Messages::TMessage &Message)
{
     if (Message.Msg == WM_NOTIFY)
     {
         if (((LPNMHDR)Message.LParam)->code == EN_LINK)
         {
             ENLINK* p = (ENLINK *)Message.LParam;
             if (p->msg == WM_LBUTTONDOWN)
             {
                 SendMessage(RichEdit1->Handle, EM_EXSETSEL, 0, (LPARAM)&(p->chrg));
                 ShellExecute(Handle, "open", RichEdit1->SelText.c_str(), 0, 0, SW_SHOWNORMAL);
             }
         }
     }
     TForm::WndProc(Message);
}

专注于最新测试仪器仪表(测试机)研发、生产、销售.

应用领域:线材测试、安规测试(高压测试)、变压器测试(LCR Meter)、PCB低阻测试、电源系统测试。

淘宝小店:http://shop72422917.taobao.com

有意者QQ:1482082920

使用Rich Edit控件相关推荐

  1. MFC EDIT控件的使用记录

    mfc和qt不同,mfc没有单行edit控件,而qt有,下面我就来介绍mfc如何将edit设置为单行控件 CFont * f; f = new CFont; f->CreateFont( 50, ...

  2. c++:MFC EDIT控件内容的四种处理方式使用(CEdit控件类型与CString 值联动)

    EDIT控件的使用 1.UpdateData()函数 2.获取控件内容 ①句柄操作 ②变量操作 1.UpdateData()函数 //UpdateData(FALSE); //将变量的值,输出到与其关 ...

  3. C++ 一个程序获取另一个程序Edit控件的内容

    转载地址:https://www.cnblogs.com/lujin49/p/4796502.html //一个程序获取另一个程序Edit控件的内容 //根据指定程序的标题名获取改程序窗口的句柄 HW ...

  4. java用gui如何写退格_emWin(ucGui)的Edit控件退格处理方法 worldsing

    在enWin(ucGui)中EDIT控件在数值模式(十进制/十六进制/二进制/浮点数)下编辑是,无法使用BackSpace键进行退格,主要涉及到的函数有: EDIT_SetBinMode() EDIT ...

  5. MFC中通过SendMessage修改Edit控件的文本

    通过Windows API可以方便地实现. 3步走: HWND hWnd = ::FindWindow(NULL,"showPicture"); HWND hEdit = ::Ge ...

  6. EDIT控件只读模式及实践问题

    EDIT控件实现只读模式 先说三种使EDIT控件实现只读模式的简单方式,然后由于这三种方式不能满足要求,最后又引出两种稍复杂的方式.这三种简单方式如下: 1.设置EDIT控件窗口风格,使窗口风格中含有 ...

  7. Windows edit控件(编辑框控件)

    在C语言控制台程序(Console Application)中,可以使用 scanf 函数获取用户的输入,而在Windows程序中使用编辑框控件. 创建编辑框控件 编辑框控件的窗口类名是 edit . ...

  8. STM32——EMWIN EDIT 控件(十八)

    EMWIN 文章目录 EMWIN 前言 一. EDIT 控件简介 二.通知代码 三.键盘反应 四. EDIT 控件 API 函数 五.EDIT 控件演示例程 总结 前言 EDIT 控件通常用来作为输入 ...

  9. MFC中的edit控件

    edit control 编辑框 static control 静态文本框控件 Button 按钮控件 第一步,在VS2019中新建一个MFC项目: 第二步,打开资源视图,双击打开窗口,在工具栏中选择 ...

最新文章

  1. 会计学python有用吗-会计转到数据分析值得吗?
  2. cordova 强制竖屏
  3. 7个月,4000+人,500+源码笔记,诚邀你参加源码共读~
  4. C#反射Assembly 具体说明
  5. 腾讯地图 qq.map 设置鼠标样式
  6. Spring中解决事务以及异步注解失效
  7. .NET存储过程入门
  8. windeployqt.exe 发布windows下qt产生的exe程序
  9. 最新版本科、硕士、博士的区别
  10. 阿里云市场联合犀思云开启云V认证 首推“严选”模式企业采购更安心
  11. label怎么换行 vb_vb代码里如何换行啊?
  12. AAA和radius协议学习
  13. 卡巴斯基终于也免费了:功能太鸡肋
  14. java excel 加密_Java 加密/解密Excel
  15. 02.数学建模的步骤
  16. Android8.1系统添加屏幕左侧边缘向右滑退出当前页面功能
  17. catia批量转stp文件格式_CATIA,UG,PROE等等格式批量转成stp,step,igs,iges
  18. mysql 求平均数 (AVG聚合函数)
  19. Java断言(assert)的介绍和使用
  20. 牛客网华为机试题(JavaScript)

热门文章

  1. 抽取JDBC工具类:JDBCUtils
  2. java label 加图片吗_UILabel里面加图片
  3. 一个没有经验的前端工程师,写CSS的时候有什么常见通病?
  4. python文献检索工具与技巧答案_短文本分析----基于python的TF-IDF特征词标签自动化提取...
  5. 学校计算机考察内容是什么意思,2019考研计算机复试四项考察内容分析及注意事项...
  6. python运行游戏是否需要pygame_用Python和Pygame写游戏-从入门到放弃(1)
  7. ajax常见特效,用ajax实现正在载入的特效-Ajax编程
  8. 惠普光影精灵拆机换屏幕_聊聊惠普游戏本大军的“先遣部队”
  9. matlab 并行 计时,用Zen2跑MATLAB R2020a并行计算负载有点奇怪 - 桌面电脑(Computer)版 - 北大未名BBS...
  10. light4java_Light Weight Component Library for Java