I have this situation. I have two multiple line edit boxes. First is not editable and second yes. I catch EN_UPDATE message and when appears than I send updated text from second window to first window. So both of window have same text. Also If one is scrolled, than second one is scrolled too (mirrored behaviour).
Problem is that if I update text in first window after send new text from second window than scrollbar is moved at the start. If I use SetScrollPos than scrollbar is set, but text is not moved to correct position. I see the first line of text and I want to see position before update the text. How is this possible?
Update
I want that after SendMessage to first window like this to not to move window to the start position of text when replaced old text with new text. Because I have for example first window scrolled in the middle and after text is replaced that firstwindow is moved to the first line of new text but I want to stay in old position because I am updating only on letter in text in second window and than I send this change to firstWindow. But I resend all text.
SendMessage(firstWindow, WM_SETTEXT, 0, (LPARAM) buffer);
I create multiline text box like this:
firstWindow = CreateWindowEx(
0, TEXT("EDIT"), // predefined class
NULL, // no window title
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER |
ES_READONLY | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
TEXTBOX_START_X, TEXTBOX_START_Y, TEXTBOX_WIDTH, TEXTBOX_HEIGHT,
hWnd, // parent window
(HMENU) ID_TEXTBOX, // edit control ID
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
NULL
);
savedWndProcTablet = (WNDPROC) SetWindowLongPtr(tabletWindowUtils.textboxHwnd, GWL_WNDPROC, (LONG_PTR) &textBoxProc);
Update 2
I try this:
char *buffer = new char[2];
buffer = "a\0";
DWORD l,r;
SendMessage(secondWindow, EM_GETSEL,(WPARAM)&l,(LPARAM)&r);
SendMessage(firstWindow, EM_REPLACESEL, 0, (LPARAM)buffer);
SendMessage(firstWindow, EM_SETSEL,l,r);
So I have some text in first window and where is position in second window I add new letter to this position. But this add one letter to correct position but it add no one letter but still adds aaaaaaaaaaaaaaaa. Why is it doing?
I can use only pure c++ and winapi.
Thanks.
If you want to copy consistently the whole text from second edit box to first, and ensure that scrolling position is correctly mirrored, you can just use:
// get the full text of second window
int len = ::GetWindowTextLength(secondWindow) + 1;
LPTSTR txt = new TCHAR[len];
::GetWindowText(secondWindow, txt, len);
// copies it to first one
::SetWindowText(firstWindow, txt);
// get scroll position of second window
SCROLLINFO si = { sizeof(SCROLLINFO) };
::GetScrollInfo(secondWindow, SB_VERT, &si);
// set scroll bar of first window accordingly
::SetScrollInfo(firstWindow, SB_VERT | SIF_PAGE | SIF_POS | SIF_RANGE, &si, TRUE );
// get first visible line in second window
int line = ::SendMessage(secondWindow, EM_GETFIRSTVISIBLELINE, 0, 0);
// skip to same line in first window
::SendMessage(firstWindow, EM_LINESCROLL, 0, line);
delete[] txt;
You could of course copy only a part of the text using EM_REPLACESEL, but you did not say how to find the new and old part, so I proposed to replace everything.