WEB开发网
开发学院软件开发VC VC中自定义CRichEditCtrl控件 阅读

VC中自定义CRichEditCtrl控件

 2007-10-07 20:23:17 来源:WEB开发网   
核心提示:#define ID_RICH_UNDO 101#define ID_RICH_CUT 102#define ID_RICH_COPY 103#define ID_RICH_PASTE 104#define ID_RICH_CLEAR105#define ID_RICH_SELECTALL 106#define ID_

#define ID_RICH_UNDO           101
#define ID_RICH_CUT             102
#define ID_RICH_COPY            103
#define ID_RICH_PASTE           104
#define ID_RICH_CLEAR          105
#define ID_RICH_SELECTALL      106
#define ID_RICH_SETFONT        107

不过这些值还可以通过VC++编译器中的菜单View->Resource Symbols进行添加。 四. 添加消息相应操作,由于这些ID是我们自己定义的,所以我们只能手动添加:

1.在头文件中添加:

afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnCopy() { Copy(); }
afx_msg void OnCut() { Cut(); }
afx_msg void OnPaste() { Paste(); }
afx_msg void OnSelectall() { SetSel(0, -1); }
afx_msg void OnUndo() { Undo(); }
afx_msg void OnClear() { Clear(); }
afx_msg void OnSelectfont();//改变字体

2.在实现文件的消息映射宏中添加如下:

ON_COMMAND(ID_RICH_COPY, OnCopy)
ON_COMMAND(ID_RICH_CUT, OnCut)
ON_COMMAND(ID_RICH_PASTE, OnPaste)
ON_COMMAND(ID_RICH_SELECTALL, OnSelectall)
ON_COMMAND(ID_RICH_UNDO, OnUndo)
ON_COMMAND(ID_RICH_CLEAR, OnClear)
ON_COMMAND(ID_RICH_SETFONT, OnSelectfont)

3.最后添加字体变换函数:

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);     //为将要输入的内容设定字体
}

然后在我们需要的地方添加头文件和实现文件,将定义的CRichEditCtrl对象改为用CMyRichEdit来定义,就可以了。

还有提醒的就是不要忘记在InitInstance()中调用AfxInitRichEdit()

上一页  1 2 

Tags:VC 定义 CRichEditCtrl

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接