当前位置:首页 > 芯闻号 > 充电吧
[导读] 在mfc中使用工具栏里的RichEdit 控件时,应该在程序初始话时加入AfxInitRichEdit,或者 AfxInitRichEdit2 否则的话 程序会起不来.也没有任何错误信息.这俩函数

 在mfc中使用工具栏里的RichEdit 控件时,应该在程序初始话时加入AfxInitRichEdit,或者 AfxInitRichEdit2 
否则的话 程序会起不来.也没有任何错误信息.
这俩函数 是加载 Riched20.dll(Riched32.dll )的.

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)的例子

Environment: VC6 SP4, 2000.

Follow these 10 easy steps to build the OutLookRichEdit control:

Insert a rich edit control into the dialog. Call AfxInitRichEdit() in the InitInstance of the App class or in InitDialog. If it does not exist, copy OutLookRichEdit.cpp and OutLookRichEdit.h to the project directory. Click the menu choice Project-Add to Project-Files and select the above-copied files to add the wrapper class to your project. Import the hand cursor into the resource and rename it "IDC_LINK". Use Classwizard to add a member variable of the rich edit control (CRichEditCtrl). 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;
In InitDialog(), add the following code.
m_ctrlText1.SetRawHyperText(_T("Click 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.

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);
}
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++ Buildern"
                       "网址: http://www.ccrun.comn"
                       "偶的信箱: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);
}
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

9月2日消息,不造车的华为或将催生出更大的独角兽公司,随着阿维塔和赛力斯的入局,华为引望愈发显得引人瞩目。

关键字: 阿维塔 塞力斯 华为

加利福尼亚州圣克拉拉县2024年8月30日 /美通社/ -- 数字化转型技术解决方案公司Trianz今天宣布,该公司与Amazon Web Services (AWS)签订了...

关键字: AWS AN BSP 数字化

伦敦2024年8月29日 /美通社/ -- 英国汽车技术公司SODA.Auto推出其旗舰产品SODA V,这是全球首款涵盖汽车工程师从创意到认证的所有需求的工具,可用于创建软件定义汽车。 SODA V工具的开发耗时1.5...

关键字: 汽车 人工智能 智能驱动 BSP

北京2024年8月28日 /美通社/ -- 越来越多用户希望企业业务能7×24不间断运行,同时企业却面临越来越多业务中断的风险,如企业系统复杂性的增加,频繁的功能更新和发布等。如何确保业务连续性,提升韧性,成...

关键字: 亚马逊 解密 控制平面 BSP

8月30日消息,据媒体报道,腾讯和网易近期正在缩减他们对日本游戏市场的投资。

关键字: 腾讯 编码器 CPU

8月28日消息,今天上午,2024中国国际大数据产业博览会开幕式在贵阳举行,华为董事、质量流程IT总裁陶景文发表了演讲。

关键字: 华为 12nm EDA 半导体

8月28日消息,在2024中国国际大数据产业博览会上,华为常务董事、华为云CEO张平安发表演讲称,数字世界的话语权最终是由生态的繁荣决定的。

关键字: 华为 12nm 手机 卫星通信

要点: 有效应对环境变化,经营业绩稳中有升 落实提质增效举措,毛利润率延续升势 战略布局成效显著,战新业务引领增长 以科技创新为引领,提升企业核心竞争力 坚持高质量发展策略,塑强核心竞争优势...

关键字: 通信 BSP 电信运营商 数字经济

北京2024年8月27日 /美通社/ -- 8月21日,由中央广播电视总台与中国电影电视技术学会联合牵头组建的NVI技术创新联盟在BIRTV2024超高清全产业链发展研讨会上宣布正式成立。 活动现场 NVI技术创新联...

关键字: VI 传输协议 音频 BSP

北京2024年8月27日 /美通社/ -- 在8月23日举办的2024年长三角生态绿色一体化发展示范区联合招商会上,软通动力信息技术(集团)股份有限公司(以下简称"软通动力")与长三角投资(上海)有限...

关键字: BSP 信息技术
关闭
关闭