VCä¸å¨å¯¹è¯æ¡ä¸ä½¿ç¨Rich Editæ§ä»¶åä¸å®è¦ç¨AfxInitRichEdit()åå§åRichEditç¯å¢.
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:
- 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
toCRichEditCtrl m_ctrlText1;
COutLookRichEdit m_ctrlText1;
- 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.
- 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++ Builder/n"                       "ç½å: http://www.ccrun.com/n"                       "å¶çä¿¡ç®±:/n"                       "mailto::[email protected] /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); } |
Â
 åæå°å http://hi.baidu.com/zhanglei_186/blog/item/2654340164cfab021c9583fe.html