當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]?在mfc中使用工具欄里的RichEdit 控件時(shí),應(yīng)該在程序初始話時(shí)加入AfxInitRichEdit,或者?AfxInitRichEdit2?否則的話 程序會(huì)起不來(lái).也沒(méi)有任何錯(cuò)誤信息.這倆函數(shù)

?在mfc中使用工具欄里的RichEdit 控件時(shí),應(yīng)該在程序初始話時(shí)加入AfxInitRichEdit,或者?AfxInitRichEdit2?
否則的話 程序會(huì)起不來(lái).也沒(méi)有任何錯(cuò)誤信息.
這倆函數(shù) 是加載 Riched20.dll(Riched32.dll?)的.

1.設(shè)置edit只讀屬性

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

2.判斷edit中光標(biāo)狀態(tài)并得到選中內(nèi)容(richedit同樣適用)

??????? int nStart, nEnd;
??????? CString strTemp;

??????? m_edit1.GetSel(nStart, nEnd);
??????? if(nStart == nEnd)
??????? {
??????????? strTemp.Format(_T("光標(biāo)在%d"), nStart);
??????????? AfxMessageBox(strTemp);
??????? }
??????? else
??????? {
??????????? //得到edit選中的內(nèi)容?????
??????????? m_edit1.GetWindowText(strTemp);
??????????? strTemp = strTemp.Mid(nStart) - strTemp.Mid(nEnd);
??????????? AfxMessageBox(strTemp);?
??????? }
??? 注:GetSel后,如果nStart和nEnd,表明光標(biāo)處于某個(gè)位置(直觀來(lái)看就是光標(biāo)在閃動(dòng));
???????????? 如果nStart和nEnd不相等,表明用戶在edit中選中了一段內(nèi)容。

3.在edit最后添加字符串

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

4.隨輸入自動(dòng)滾動(dòng)到最后一行(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派生一個(gè)類,添加WM_CHAR消息映射。下面一個(gè)例子實(shí)現(xiàn)了限定輸入16進(jìn)制字符的功能。

?? 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自動(dòng)為寬字符(WideChar),所以它可以解決中文亂碼以及一些漢字問(wèn)題

??? 方法一:(msdn上的做法,適用于用VC.NET及以后版本創(chuàng)建的工程)
??????????? 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.
??? 方法二:以對(duì)話框?yàn)槔?br />?????? (1)??? 增加一全局變量 HMODULE hMod;
?????? (2)??? 在CxxxApp::InitInstance()中添加一句hMod = LoadLibrary(_T("riched20.dll"));
??? ??? ??? 在CxxxApp::ExitInstance()中添加一句FreeLibrary(hMod);
??? ?? (3)??? 在對(duì)話框上放一個(gè)richedit,文本方式打開(kāi).rc文件修改該richedit控件的類名"RICHEDIT" to "RichEdit20a".
??? ?? (4)??? 在對(duì)話框頭文件添加 CRichEditCtrl m_richedit;
??? ??? ??? 在OnInitDialog中添加 m_richedit.SubclassDlgItem(IDC_RICHEDIT1, this);

8.改變r(jià)ichedit指定區(qū)域的顏色及字體

??????? 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("隸書"));//設(shè)置字體
?????
??????? m_richedit1.SetSel(1, 5); //設(shè)置處理區(qū)域
??????? m_richedit1.SetSelectionCharFormat(cf);

9.設(shè)置行間距(只適用于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動(dòng)畫

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

12.richedit嵌入ole對(duì)象

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

13.使richedit選中內(nèi)容只讀

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無(wú)響應(yīng)的問(wèn)題

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.當(dāng)edit控件的父窗口屬性是帶標(biāo)題欄WS_CAPTION和子窗口WS_CHILD時(shí),不能設(shè)置焦點(diǎn)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中回車時(shí),會(huì)退出對(duì)話框?

選中Edit的風(fēng)格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. 動(dòng)態(tài)創(chuàng)建的edit沒(méi)有邊框的問(wèn)題

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

22. 一個(gè)能顯示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();

字體設(shè)置代碼

最后添加字體變換函數(shù):?
CHARFORMAT cf;?
LOGFONT lf;?
memset(&cf, 0, sizeof(CHARFORMAT));?
memset(&lf, 0, sizeof(LOGFONT));?

//判斷是否選擇了內(nèi)容?
BOOL bSelect = (GetSelectionType() != SEL_EMPTY) ? TRUE : FALSE;?
if (bSelect)?
{?
???????????? GetSelectionCharFormat(cf);?
}?
else?
{?
???????????? GetDefaultCharFormat(cf);?
}?

//得到相關(guān)字體屬性?
BOOL bIsBold = cf.dwEffects & CFE_BOLD;?
BOOL bIsItalic = cf.dwEffects & CFE_ITALIC;?
BOOL bIsUnderline = cf.dwEffects & CFE_UNDERLINE;?
BOOL bIsStrickout = cf.dwEffects & CFE_STRIKEOUT;?

//設(shè)置屬性?
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);???? //為選定的內(nèi)容設(shè)定所選字體?
???????????? else?
???????????????????????? SetWordCharFormat(cf);???????? //為將要輸入的內(nèi)容設(shè)定字體?
}


在RichEdit中實(shí)現(xiàn)超鏈接


在RichEdit中實(shí)現(xiàn)超鏈接 責(zé)任編輯:admin   在CBuilder上制作 更新日期:2005-8-6 ? 首先在Form上放置一個(gè)RichEdit。

在窗體的構(gòu)造函數(shù)中添加以下代碼:
__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);?? //自動(dòng)檢測(cè)URL

???? RichEdit1->Text = "歡迎訪問(wèn)C++ Buildern"
?????????????????????? "網(wǎng)址: 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);
}
本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉