richedit2
1 使用richedit 控件首先要加载 richedit 的dll.
LoadLibrary(_T("riched20.dll"));
2 加入对 richedit.h 头文件的引用后,创建窗口
hwndBuffer = CreateWindow(RICHEDIT_CLASS,NULL,WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|ES_SUNKEN
|ES_AUTOVSCROLL|ES_READONLY|ES_SAVESEL,6,30,526,354,hWnd,(HMENU)EDIT_ID,hInstance,NULL);
这里创建了一个只读的窗口,这样只能通过函数输入文字。
3 设置颜色:
//设置背景色
SendMessage( hwndBuffer, EM_SETBKGNDCOLOR, 0, RGB(0,0,0) );
//设置文字颜色
CHARFORMAT2 charFmt;
ZeroMemory(&charFmt,sizeof(charFmt));
charFmt.cbSize = sizeof(charFmt);
charFmt.dwMask = CFM_COLOR; //只修改文字颜色标志
charFmt.crTextColor = RGB(0,179,0);//设置字体颜色
SendMessage(hwndBuffer,EM_SETCHARFORMAT,0,(LPARAM)&charFmt);
//修改部分文字颜色
SendMessage(hwndBuffer,EM_SETSEL,0,10); //选中0-10的文字
charFmt.crTextColor = it->clr;
//修改文字颜色
SendMessage(hwndBuffer,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&charFmt);
注意对于richedit2.0 汉字仅作为一个字符。对于unicode 工程无所谓。但对于多字节来说一个汉字为2个字符。所以你通过strlen之类计算的字符串长度会比richedit 认为的要长如:
"大家好"。计算为6长度。但对于richedit来说为3个.发送EM_SEL消息,设置选中消息时必须以richedit为准.
SendMessage(hwndBuffer,EM_SETSEL,0,3); 如果指定为6将超出范围
4 滚动滚动条:
SendMessage(hwndBuffer,WM_VSCROLL, SB_BOTTOM, 0); //滚动到文档的最后面
5 游标
int temp1,temp2; //获取起始和结束位置
SendMessage(hwndBuffer,EM_GETSEL,(WPARAM)&temp1,(LPARAM)&temp2);
//在文档末尾输入字符:
SendMessage(hwndBuffer, EM_SETSEL,-1,-1);
SendMessage(hwndBuffer, EM_REPLACESEL, TRUE, (LPARAM) buffer );