mfc richedit2格式化 [英] mfc richedit2 formatting

查看:192
本文介绍了mfc richedit2格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用丰富的编辑控制项在屏幕上输出一些文字:



星期一按1.

您的当天是星期一

星期二按2.



真的找到任何简单的例子如何做到这一点。所有我已经能够排序是设置窗口文本(setWindowText),但一切逃避我。
任何简短的例子?

解决方案

尽管有意见,我将回答你问的问题,在Rich Edit控件中格式化数据。几年前,我不得不这样做,并提出了一些我可以对待一点像一个IOstream(如果我今天做,我可能会做一点不同,但这是生活)。



首先,代码行为像IOstream,但写入富编辑控件:

  // rich_stream.h:
#ifndef RICH_STREAM_H
#define RICH_STREAM_H

class rich_stream {
CRichEditCtrl& ctrl;
public:
rich_stream(CRichEditCtrl& ctrl_):ctrl(ctrl_){}

void add_text(char const * txt){
ctrl.SetSel 1,-1);
ctrl.ReplaceSel(txt);
}

void add_int(int val){
CString temp;
temp.Format(%d,val);
add_text(temp);
}

void set_char_format(CHARFORMAT& fmt){
ctrl.SetSelectionCharFormat(fmt);
}
};

inline rich_stream& operator<<(rich_stream& s,char const * t){
s.add_text(t);
return s;
}

inline rich_stream& operator<<(rich_stream& s,CHARFORMAT& fmt){
s.set_char_format(fmt);
return s;
}

inline CString nl(){
return CString(\\\
\\\
);
}

inline rich_stream& operator<<(rich_stream& s,CString(* f)()){
s.add_text(f
return s;
}

inline rich_stream& operator<<(rich_stream& s,int val){
s.add_int(val);
return s;
}
#endif

然后,

  CHARFORMAT bold; 

memset(& bold,0,sizeof(bold));
bold.cbSize = sizeof(bold);
bold.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE;
bold.dwEffects = CFE_BOLD;
strcpy(bold.szFaceName,Times);
bold.yHeight = 14 * 20;

CHARFORMAT正常;
memset(& normal,0,sizeof(normal));
normal.cbSize = sizeof(normal);
normal.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE;
normal.dwEffects = 0;
strcpy(normal.szFaceName,Times);
normal.yHeight = 14 * 20;

// ...

rich_stream txt(GetRichEditCtrl());

txt<<粗体< 标题1:<正常<< info1<< nl
<<粗体< 标题2:<正常<< info2<< nl
<<粗体< 标题3:<正常<< info3;

如果我今天这样做,我几乎肯定会创建一个小类作为 CHARFORMAT 所以我可以构造格式化对象更干净。我可能也会至少考虑把它实现为一个普通的iostream与一个流缓冲区,插入数据到丰富的编辑控制(但当时我不知道流足够好,知道我应该这样做) / p>

瞥一眼,还有一些其他的东西不完全正确 - add_text uses SetSel(-1,-1); 。这应该真正检索文本的当前长度(例如,与 GetWindowTextLength ,并将选择刚刚结束后。


I am trying to use a rich edit control to output some text on the screen:

Monday Press 1.
Your day is Monday
Tuesday Press 2.

I can't really find any simple examples of how to do this. all i have been able to sort out is setting the window text (setWindowText), but everything else is escaping me. Any short examples?

解决方案

Despite the comments, I'm going to answer the question you asked, about how to format data in a Rich Edit control. A few years ago, I had to do this, and came up with something that I could treat a little like an IOstream (if I were doing it today, I'd probably do it a bit differently, but such is life).

First, code to act like an IOstream, but write to a rich-edit control:

// rich_stream.h:
#ifndef RICH_STREAM_H
#define RICH_STREAM_H

class rich_stream { 
    CRichEditCtrl &ctrl;
public:
    rich_stream(CRichEditCtrl &ctrl_) : ctrl(ctrl_) { }

    void add_text(char const *txt) {
        ctrl.SetSel(-1,-1);
        ctrl.ReplaceSel(txt);
    }

    void add_int(int val) {
        CString temp;
        temp.Format("%d", val);
        add_text(temp);
    }

    void set_char_format(CHARFORMAT &fmt) {
        ctrl.SetSelectionCharFormat(fmt);
    }    
};

inline rich_stream &operator<<(rich_stream &s, char const *t) {
    s.add_text(t);
    return s;
}

inline rich_stream &operator<<(rich_stream &s, CHARFORMAT &fmt) {
    s.set_char_format(fmt);
    return s;
}

inline CString nl() {
    return CString("\n\n");
}

inline rich_stream &operator<<(rich_stream &s, CString (*f)()) {
    s.add_text(f());
    return s;
}

inline rich_stream &operator<<(rich_stream &s, int val) {
    s.add_int(val);
    return s;
}
#endif

Then, I'd use this something like:

CHARFORMAT bold;

memset(&bold, 0, sizeof(bold));
bold.cbSize = sizeof(bold);
bold.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE;
bold.dwEffects = CFE_BOLD;
strcpy(bold.szFaceName, "Times");
bold.yHeight = 14 * 20;

CHARFORMAT normal;
memset(&normal, 0, sizeof(normal));
normal.cbSize = sizeof(normal);
normal.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE;
normal.dwEffects = 0;
strcpy(normal.szFaceName, "Times");
normal.yHeight = 14 * 20;

// ...    

rich_stream txt(GetRichEditCtrl());

txt << bold << "Heading 1: " << normal << info1 << nl
    << bold << "Heading 2: " << normal << info2 << nl
    << bold << "Heading 3: " << normal << info3;

If I were doing this today, I'd almost certainly create a small class as a wrapper for a CHARFORMAT so I could construct the formatting objects a little more cleanly. I'd probably also at least think hard about implementing it as a normal iostream with a stream buffer that inserted data into the rich edit control (but at the time I didn't know streams well enough to know I should do that).

Glancing at it, there are a few other things that aren't really exactly right either -- add_text uses SetSel(-1, -1);. This should really retrieve the current length of the text (e.g., with GetWindowTextLength, and set the selection to just after the end.

这篇关于mfc richedit2格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆