在之前的文章《RichEdit 各个版本介绍》中,写到RichEdit已经到达6.0版本了,而我们经常编程使用的却还是2.0,在vc6.0中甚至还使用1.0版本,更高的版本修复了很多的bug,还提供了许多新特性,我们没有理由不去使用它们。在此介绍,在VS2008SP1中如何加载RichEdit4.1版本,甚至更高版本的也可以类似加载。
原理:
1.启动VS2008,新建一个基于CRichEditView的多文档视图Unicode工程,名称取为:CTest_Richedit;
2.查看源代码,CTest_RicheditView派生自CRichEditView,右键CRichEditView“转到声明”,发现CRichEditView派生自CCtrlView,继续查看CRichEditView的构造函数(在文件X:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\viewrich.cpp中),如下所示:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 |
CRichEditView::CRichEditView() : CCtrlView(RICHEDIT_CLASS, AFX_WS_DEFAULT_VIEW | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL | ES_SAVESEL | ES_SELECTIONBAR) { m_bSyncCharFormat = m_bSyncParaFormat = TRUE; m_lpRichEditOle = NULL; m_nBulletIndent = 720; // 1/2 inch m_nWordWrap = WrapToWindow; m_nPasteType = 0; SetPaperSize(CSize(8*1440+720, 11*1440)); SetMargins(CRect(0,0,0,0)); m_charformat.cbSize = sizeof(CHARFORMAT2); m_paraformat.cbSize = sizeof(PARAFORMAT2); } |
在第一行发现有RICHEDIT_CLASS,顾名思义这是RichEdit类名的意思,查看这个宏所定义的内容,在文件X:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\Richedit.h中开头:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#define MSFTEDIT_CLASS L"RICHEDIT50W" // NOTE: MSFTEDIT.DLL only registers MSFTEDIT_CLASS. If an application wants // to use the following Richedit classes, it needs to load the riched20.dll. // Otherwise, CreateWindow with RICHEDIT_CLASS would fail. // This also applies to any dialog that uses RICHEDIT_CLASS, // RichEdit 2.0 Window Class // On Windows CE, avoid possible conflicts on Win95 #define CERICHEDIT_CLASSA "RichEditCEA" #define CERICHEDIT_CLASSW L"RichEditCEW" #define RICHEDIT_CLASSA "RichEdit20A" #define RICHEDIT_CLASS10A "RICHEDIT" // Richedit 1.0 #ifndef MACPORT #define RICHEDIT_CLASSW L"RichEdit20W" #else //----------------------MACPORT #define RICHEDIT_CLASSW TEXT("RichEdit20W") // MACPORT change #endif // MACPORT #if (_RICHEDIT_VER >= 0x0200 ) #ifdef UNICODE #define RICHEDIT_CLASS RICHEDIT_CLASSW #else #define RICHEDIT_CLASS RICHEDIT_CLASSA #endif // UNICODE #else #define RICHEDIT_CLASS RICHEDIT_CLASS10A #endif // _RICHEDIT_VER >= 0x0200 |
摘取那么长的一段,首先RICHEDIT_CLASS在Unicode中代表着"RichEdit20W",否则代表着"RichEdit20A"。另外在上面的语句:
1 2 |
#define MSFTEDIT_CLASS L"RICHEDIT50W" // NOTE: MSFTEDIT.DLL only registers MSFTEDIT_CLASS. |
说明着MSFTEDIT.DLL(即RichEdit4.1版本)注册的类名是"RICHEDIT50W"。继续下一步,因为工程建立的是Unicode,所以CRichEditView的构造函数给CCtrlView的构造函数传了"RichEdit20W"参数,查看CCtrlView的构造函数(在文件X:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\viewcore.cpp中),如下所示:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 |
CCtrlView::CCtrlView(LPCTSTR lpszClass, DWORD dwStyle) { m_strClass = lpszClass; m_dwDefaultStyle = dwStyle; } BOOL CCtrlView::PreCreateWindow(CREATESTRUCT& cs) { ASSERT(cs.lpszClass == NULL); cs.lpszClass = m_strClass; // initialize common controls VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG)); AfxDeferRegisterClass(AFX_WNDCOMMCTLSNEW_REG); // map default CView style to default style // WS_BORDER is insignificant if ((cs.style | WS_BORDER) == AFX_WS_DEFAULT_VIEW) cs.style = m_dwDefaultStyle & (cs.style | ~WS_BORDER); return CView::PreCreateWindow(cs); } |
可以发现CCtrlView的构造函数里把"RichEdit20W"传给了m_strClass,而在CCtrlView的PreCreateWindow函数里m_strClass做为变量传值给了CREATESTRUCT结构体,CREATESTRUCT结构定义了传递给应用程序的窗口过程的初始化参数。我们只要改变m_strClass类名,就能创建出我们想要的窗口。
实现:
1.在上面的CTest_Richedit工程中,在CTest_RicheditView的构造函数里赋值想要的类名给m_strClass,如下所示:
1 2 3 4 |
CTest_RicheditView::CTest_RicheditView() { m_strClass = MSFTEDIT_CLASS; } |
然后在CTest_RicheditApp里,声明一个HINSTANCE对象,如下所示:
1 2 3 4 5 6 7 8 |
class CTest_RicheditApp : public CWinApp { public: CTest_RicheditApp(); virtual ~CTest_RicheditApp(); //重载析构函数 HINSTANCE m_hinstRE41; //声明实例对象 //······ }; |
然后在实现文件里面,写入如下代码:
1 2 3 4 5 6 7 8 9 |
CTest_RicheditApp::CTest_RicheditApp() { m_hinstRE41 = LoadLibrary(_T("msftedit.dll")); } CTest_RicheditApp::~CTest_RicheditApp() { if(m_hinstRE41) FreeLibrary(m_hinstRE41); } |
完成,运行程序。用Spy++检测,为"RICHEDIT50W"类名,如下图所示:
对话框方式:
1.在对话框上放置一个Rich Edit 2.0控件;
2.以文本方式打开工程.rc文件,找到描述该控件的地方,如下所示:
1 2 3 4 5 6 7 8 |
IDD_ABOUTBOX DIALOGEX 0, 0, 266, 199 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "关于 Test_Richedit" FONT 9, "MS Shell Dlg", 0, 0, 0x1 BEGIN DEFPUSHBUTTON "确定",IDOK,209,178,50,14,WS_GROUP CONTROL "",IDC_RICHEDIT21,"RichEdit20W",ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP,22,32,140,103 END |
3.把"RichEdit20W"改成"RICHEDIT50W",保存文件。切换到VS,提示重新加载资源文件,确定即可。
4.跟前面的步骤一样,加载"msftedit.dll",运行如下所示:
参考文章:
A Very Simple Way to Use Richedit 5.0 in VC6 and other VS versions http://www.codeproject.com/KB/edit/UseRichEdit50.aspx
http://blog.csdn.net/akof1314/article/details/6607265