how to auto scrolldown a richedit textbox win32 c/c++

2k Views Asked by At

here is my code that appends messages to the richedit text box:

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());

and here is the createwindow for the richedit textbox:

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);

But it does not automatically scroll down when the messages in the text box become to much to all fit in it, forcing the user to have to constantly scroll down. All of the other references facing this problem are in .NET or c#. Cant i somehow set the cursor to the bottom of the text box after the append? or something like that. any help is appreciated. thanks.

EDIT: I tried adding :

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

after my append code as this was a solution for someone else but did not work for me

3

There are 3 best solutions below

0
Mitchell On

Before you insert the text in,
int start_lines, end_lines; start_lines = SendMessage(hwnd, EM_GETLINECOUNT,0,0);

After text is inserted,
end_lines = SendMessage(hwnd, EM_GETLINECOUNT,0,0); SendMessage(hwnd, EM_LINESCROLL, 0, end_lines - start_lines);

0
user3406468 On

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

after the Text was inserted works best for me.

0
k.bon On

delphi try this.

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