我们在使用聊天工具的时候经常会看到消息显示为各种颜色、字体、下划线、超链接识别等效果,使用微软为我们提供的RichEdit控件就可以达到这样的效果,但是RichEdit控件使用起来并不简单,甚至于在使用的时候让我们的窗体中充斥着RichEdit的代码,下面让我们重新封装这个控件,以方面我们以后的使用。
一、效果图:
二、源码:
RichEditCtrlEx.h
一、效果图:
![](http://www.looiy.com/wp-content/uploads/2011/05/richedit.jpg)
二、源码:
RichEditCtrlEx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/
// Name: RICHEDITCTRLEX.H
// Purpose: CRichEditCtrlEx
// Author: yfq
// Modified by:
// Created: 2010/12/24 11:01
// RCS-ID:
// Copyright:
// Licence:
/
#ifndef __RICHEDITCTRLEX_H__
#define __RICHEDITCTRLEX_H__
class
CRichEditCtrlEx :
public
CRichEditCtrl
{
DECLARE_DYNCREATE(CRichEditCtrlEx)
public
:
CRichEditCtrlEx(
void
);
~CRichEditCtrlEx(
void
);
void
Initlize();
/**
* 最后追加一行
*
* @param text -[in] -要追加的文本
*
* @param newline -[in] -是否换行
*
* @param link -[in] -是否带超链接
*
* @param nLinkBegin -[in] -超链接起始位置(索引从0开始)
*
* @param nLinkCount -[in] -超链接字符数
*
* @return -[NONE]
*/
void
Append(
LPCSTR
text,
bool
newline =
false
,
bool
link =
false
,
int
nLinkBegin = 0,
int
nLinkCount = 0);
/**
* 获得文本
*
* @param nBegin -[in] -开始位置
*
* @param nEnd -[in] -结束位置
*
* @param text -[out] -得到的文本
*
* @return -[NONE]
*/
void
GetText(
int
nBegin,
int
nEnd,CString& text);
/**
* 新增文字时是否滚动到底部
*
* @param bottom -[in] -否滚动到底部
*
* @return -[NONE]
*/
void
SetScrollBottom(
bool
bottom=
true
);
void
StopScroll();
/**
* 设置字体
*
* @param nBegin -[in] -开始位置
*
* @param nEnd -[in] -结束位置
*
* @param bold -[in] -粗体
*
* @param italic -[in] -斜体
*
* @param underline -[in] -下划线
*
* @param color -[in] -颜色
*
* @param size -[in] -文字大小
*
* @return -[NONE]
*/
void
SetTextFont(
int
nBegin,
int
nEnd,
bool
bold,
bool
italic,
bool
underline,
COLORREF
color=0,
int
size=0);
/**
* 为选中的文本添加超链接
*
* @return -[NONE]
*/
void
AddLink();
/**
* 取消选中文本添的超链接
*
* @return -[NONE]
*/
void
CancelLink();
/**
* 校验URL是否合法文件路径,依据为rfc2396
* 这个函数只做粗略的校验,不完全符合RFC2369
* 仅支持IPV4
*
*/
BOOL
CheckURL(
const
CString &szUrl, CString &szMsg);
void
OnSize(CWnd* pParent);
void
OnSize(CRect& rect);
void
OnSpecifySize(CWnd* pParent);
bool
IsCreate();
void
Clear();
void
SetEditFocus(
bool
focus);
/**
* 显示指定颜色的边框
*
* @param bShow -[in] -是否显示边框
*
* @param nColor -[in] -边框颜色
*
* @return -[NONE]
*/
void
ShowBorderLine(
BOOL
bShow,
COLORREF
nColor = RGB(53, 104, 187));
/**
* 设置左侧偏移
*
* @param nOffset -[in] -偏移量
*
* @return -[NONE]
*/
void
SetLeftOffset(
int
nOffset);
public
:
afx_msg
void
OnPaint();
afx_msg
void
OnSize(
UINT
nType,
int
cx,
int
cy);
afx_msg
BOOL
OnLink(NMHDR *in_pNotifyHeader,
LRESULT
*out_pResult);
afx_msg
void
OnSetFocus(CWnd* pOldWnd);
DECLARE_MESSAGE_MAP()
private
:
CWnd* m_pParentWnd;
bool
m_bBottom;
BOOL
m_ShowBorder;
COLORREF
m_BorderColor;
bool
m_bCreate;
bool
m_bFocus;
};
#endif //__RICHEDITCTRLEX_H__
三、使用方法 1、在窗体上放一个Rich Edit 2.0 Control,修改属性,例如,你需要多行显示则要把Multiline属性设置为TRUE,如果需要垂直滚动条则需要把Vertical Scroll属性设置为TRUE 2、为新添加的RichEdit控件关联控件变量: 在头文件中声明CRichEditCtrlEx m_RichEdit; 在DoDataExchange中写入DDX_Control(pDX, IDC_RICHEDIT21, m_RichEdit);
3、初始化控件
|