如何自动滚动一个richedit文本框win32 c / c ++

时间:2014-01-23 05:19:58

标签: c++ c textbox richedit richedit-control

这是我的代码,它将消息附加到richedit文本框:

CHARFORMAT cf;
memset( &cf, 0, sizeof cf );
cf.cbSize = sizeof cf;
cf.dwMask = CFM_COLOR;
if (getuserofmessage(msg) == myname)
cf.crTextColor = RGB(0,0,255);// <----- the color of the text
else if (getuserofmessage(msg) == "admin")
cf.crTextColor = RGB(255,0,0);// <----- the color of the text
else
cf.crTextColor = RGB(55,200,100);// <----- the color of the text

SendMessage( hwnd , EM_SETCHARFORMAT, (LPARAM)SCF_SELECTION, (LPARAM) &cf);

/*SendMessage(hwnd, EM_SETSEL, 0, -1); 
    SendMessage(hwnd, EM_SETSEL, -1, -1); 
     SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)msg.c_str());*/
     CHARRANGE cr;
cr.cpMin = -1;
cr.cpMax = -1;

// hwnd = rich edit hwnd
SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)msg.c_str());

这里是richedit文本框的创建窗口:

hwnd=CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, "",                   
ES_READONLY | WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
x,
y,
w,
h,
parent,
(HMENU)identifier,
GetModuleHandle(NULL),
NULL);

但是当文本框中的消息变得非常适合它时,它不会自动向下滚动,迫使用户不得不不断向下滚动。涉及此问题的所有其他引用都在.NET或c#中。我不知何故在追加后将光标设置在文本框的底部?或类似的东西。任何帮助表示赞赏。感谢。

编辑: 我尝试添加:

DWORD TextSize;
TextSize=GetWindowTextLength(hwnd);
SendMessage(hwnd,EM_SETSEL,TextSize,TextSize);
SendMessage(hwnd,EM_SCROLLCARET,0,0);

在我的附加代码之后,因为这是其他人的解决方案,但对我不起作用

3 个答案:

答案 0 :(得分:2)

SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, 0L);

插入文本后

最适合我。

答案 1 :(得分:1)

在插入文本之前,
int start_lines, end_lines; start_lines = SendMessage(hwnd, EM_GETLINECOUNT,0,0);

插入文字后,
end_lines = SendMessage(hwnd, EM_GETLINECOUNT,0,0); SendMessage(hwnd, EM_LINESCROLL, 0, end_lines - start_lines);

答案 2 :(得分:0)

delphi尝试这个。

SendMessage(RichEdit1.Handle, WM_VSCROLL, SB_BOTTOM, 0);