服务器之家

服务器之家 > 正文

VC++中HTControl控件类之CHTRichEdit富文本编辑控件实例

时间:2021-01-27 12:00     来源/作者:C++教程网

本文所述CHTRichEdit控件类继承自CRichEditCtrl,为了避免CRichEditCtrl的界面风格与CRichEditCtr的滚动条不相协调,因此在CHTRichEdit控件中去掉了CRichEditCtrl的默认滚动条,替换成自己绘制的滚动条,这样就不会破坏整体外观了。替换的滚动条并不需要与默认的滚动条同样的宽度,不受操作系统主题的影响,可以支持任意的宽度,而且在不需要滚动条时它会自动消失,使软件布局不会受到影响。

具体实现代码如下:

?
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
#if !defined(__CHTRichEdit_H__)
#define __CHTRichEdit_H__
#include "CHTScrollButton.h"
#include "CHTVertScrollBar.h"
#include "CHTHorzScrollBar.h"
class AFX_CLASS_EXPORT CHTRichEdit : public CRichEditCtrl
{
 DECLARE_DYNAMIC(CHTRichEdit)
public:
 CHTRichEdit();
 virtual ~CHTRichEdit();
protected:
 DECLARE_MESSAGE_MAP()
public:
/****************************************************************************
| 函数功能:创建滚动条的按钮                        |
| 输入参数:                                |
| iWidth:  按钮宽                             |
| iHeight: 按钮高                             |
| hBitmapUpNormal: 向上按钮常态位图                    |
| hBitmapUpHover: 向上按钮鼠标停留位图                   |
| hBitmapUpDown: 向上按钮鼠标按下位图                   |
| hBitmapDownNormal: 向下按钮常态位图                   |
| hBitmapDownHover: 向下按钮鼠标停留位图                  |
| hBitmapDownDown: 向下按钮鼠标按下位图                  |
| hBitmapLeftNormal: 向左按钮常态位图                   |
| hBitmapLeftHover: 向左按钮鼠标停留位图                  |
| hBitmapLeftDown: 向左按钮鼠标按下位图                  |
| hBitmapRightNormal: 向右按钮常态位图                  |
| hBitmapRightHover: 向右按钮鼠标停留位图                 |
| hBitmapRightDown: 向右按钮鼠标按下位图                  |
| 输出参数:无                               |
| 返回值: 无                               |
| 说明:                                  |
****************************************************************************/
void CreateButton(int iWidth, int iHeight, HBITMAP hBitmapUpNormal, HBITMAP hBitmapUpHover, HBITMAP hBitmapUpDown,
 HBITMAP hBitmapDownNormal, HBITMAP hBitmapDownHover, HBITMAP hBitmapDownDown,
 HBITMAP hBitmapLeftNormal, HBITMAP hBitmapLeftHover, HBITMAP hBitmapLeftDown,
 HBITMAP hBitmapRightNormal, HBITMAP hBitmapRightHover, HBITMAP hBitmapRightDown);
/****************************************************************************
| 函数功能:创建滚动条                           |
| 输入参数:                                |
| iWidth:  滚动条宽度                           |
| hBmpVBg: 垂直滚动条背景位图                       |
| hBmpVThumb: 垂直滚动条滑块位图                      |
| hBmpVHoverThumb:垂直滚动条鼠标在滑块上的位图               |
| hBmpVPressThumb: 垂直滚动条鼠标按下滑块的位图               |
| hBmpHBg: 水平滚动条背景位图                       |
| hBmpHThumb: 水平滚动条滑块位图                      |
| hBmpHHoverThumb:水平滚动条鼠标在滑块上的位图               |
| hBmpHPressThumb: 水平滚动条鼠标按下滑块的位图               |
| iBgTopLen:滚动条背景位图顶端和左端不可拉伸部分长度            |
| iBgBottomLen:滚动条背景位图底端和右端不可拉伸部分长度          |
| iThumbTopLen:滑块背景位图顶端和左端不可拉伸部分长度           |
| iThumbBottomLen:滑块背景位图底端和右端不可拉伸部分长度          |
| 输出参数:无                               |
| 返回值: 无                               |
| 说明:                                  |
****************************************************************************/
void CreateScrollBar(int iWidth, HBITMAP hBmpVBg, HBITMAP hBmpVThumb, HBITMAP hBmpVHoverThumb, HBITMAP hBmpVPressThumb,
 HBITMAP hBmpHBg, HBITMAP hBmpHThumb, HBITMAP hBmpHHoverThumb, HBITMAP hBmpHPressThumb,
 int iBgTopLen = 0, int iBgBottomLen = 0, int iThumbTopLen = 0, int iThumbBottomLen = 0);
 //以下是重载的原接口,可以查阅MSDN
 virtual BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
 virtual BOOL CreateEx(DWORD dwExStyle, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
 void MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE);
 void MoveWindow(LPCRECT lpRect, BOOL bRepaint = TRUE);
 BOOL SetWindowPos(HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags);
private:
 afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp);
 afx_msg void OnPaint();
 afx_msg void OnEnChange();
 afx_msg void OnNcPaint();
 afx_msg BOOL OnNcActivate(BOOL bActive);
 afx_msg BOOL OnEraseBkgnd(CDC* pDC);
 afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
 afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
 afx_msg void OnEnHscroll();
 afx_msg void OnEnVscroll();
 afx_msg void OnCaptureChanged(CWnd *pWnd);
 afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
 afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
 afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
 afx_msg void OnTimer(UINT_PTR nIDEvent);
 afx_msg LRESULT OnNcHitTest(CPoint point);
 afx_msg void OnEnRequestresize(NMHDR *pNMHDR, LRESULT *pResult);
 afx_msg void OnSize(UINT nType, int cx, int cy);
 void SetScrollState();
 void UpdateScrollBar();
public:
 CHTScrollButton m_HTButtonUp;
 CHTVertScrollBar m_HTVScrollBar;
 CHTScrollButton m_HTButtonDown;
 CHTScrollButton m_HTButtonLeft;
 CHTHorzScrollBar m_HTHScrollBar;
 CHTScrollButton m_HTButtonRight;
private:
 int m_iBtnHeight;
 int m_iBtnWidth;
 BOOL m_bVScrollBarFlag;
 BOOL m_bHScrollBarFlag;
 int m_iWidth;
 int m_iHeight;
 int m_iX;
 int m_iY;
 BOOL m_bScrollFlag;
 BOOL m_bMoveFlag;
 int m_iScrollBarWidth;
 int m_iScrollBarHeight;
 BOOL m_bFlag;
 int m_iCountV;
 int m_iCountH;
};
#endif //!__CHTRichEdit_H__

代码备有一定的注释,希望本例所述能够对大家有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部