简体   繁体   English

c ++ winapi:将WM_SETTEXT之后的滚动条移动到旧位置

[英]c++ winapi: Move scrollbar after WM_SETTEXT to old position

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. 我捕获到EN_UPDATE消息,并且出现时比从第二个窗口向第一个窗口发送更新的文本。 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. 如果我使用SetScrollPos,则不会设置滚动条,但是文本不会移动到正确的位置。 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. 我希望将SendMessage发送到第一个这样的窗口后,不要在将旧文本替换为新文本时将窗口移到文本的起始位置。 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. 因为例如我在中间滚动了第一个窗口,并且在替换了文本之后,将firstwindow移到了新文本的第一行,但是我想保持原来的位置,因为我只更新第二个窗口中文本中的字母,所以我将此更改发送到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 更新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. 但这会在正确位置添加一个字母,但不会添加一个字母,但仍会添加aaaaaaaaaaaaaaaaaaaa。 Why is it doing? 怎么了

I can use only pure c++ and winapi. 我只能使用纯c ++和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. 您当然可以使用EM_REPLACESEL复制文本的一部分,但是您没有说明如何查找新的和旧的部分,因此我建议替换所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM