procedure SetCaretPos(Handle: HWND; const Value: TPoint);
var
CharIdx: Integer;
begin
CharIdx := SendMessage(Handle, EM_LINEINDEX, Value.y, 0) + Value.x;
SendMessage(Handle, EM_SETSEL, CharIdx, CharIdx);
end;
textbox как предотвратить сброс caret'a при изменениии текста.
Есть textbox текст которого меняется, при каждом изменении текста caret'a возвращается на начало textbox'a, что очень неудобно если в textbox много текста на много "страниц". Как это предотвратить?
Ручками или какими-то сторонними действиями?
Попробуй использовать метод ScrollToCaret.
Вообще для серьёзной работы с текстом
советую использовать RichTextBox.
Код:
Если тебя такой вариант устраивает, то можно переделать на C# всё это дело. Сначала описываем PInvoke-вызов:
Код:
[SIZE=2][COLOR=#0000ff][FONT=Courier New]using [/FONT][/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#008080]HWND[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]UInt32[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=#008080]LPARAM[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]Int32[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=#008080]WPARAM[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]Int32[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=#008080]LRESULT[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]Int32[/COLOR][/SIZE][SIZE=2];[/SIZE][/FONT]
[SIZE=2][SIZE=2][FONT=Courier New][[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#008080]DllImport[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"user32.dll"[/COLOR][/SIZE][SIZE=2], CallingConvention = [/SIZE][SIZE=2][COLOR=#008080]CallingConvention[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New].StdCall)][/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]extern [/COLOR][/SIZE][SIZE=2][COLOR=#008080]LRESULT[/COLOR][/SIZE][SIZE=2] SendMessage([/SIZE][SIZE=2][COLOR=#008080]HWND[/COLOR][/SIZE][SIZE=2] handle, [/SIZE][SIZE=2][COLOR=#008080]UInt32[/COLOR][/SIZE][SIZE=2] msg, [/SIZE][SIZE=2][COLOR=#008080]WPARAM[/COLOR][/SIZE][SIZE=2] wParam, [/SIZE][SIZE=2][COLOR=#008080]LPARAM[/COLOR][/SIZE][SIZE=2] lParam);[/SIZE][/FONT]
[/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=#008080]LPARAM[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]Int32[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=#008080]WPARAM[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]Int32[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=#008080]LRESULT[/COLOR][/SIZE][SIZE=2] = System.[/SIZE][SIZE=2][COLOR=#008080]Int32[/COLOR][/SIZE][SIZE=2];[/SIZE][/FONT]
[SIZE=2][SIZE=2][FONT=Courier New][[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#008080]DllImport[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]"user32.dll"[/COLOR][/SIZE][SIZE=2], CallingConvention = [/SIZE][SIZE=2][COLOR=#008080]CallingConvention[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New].StdCall)][/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]extern [/COLOR][/SIZE][SIZE=2][COLOR=#008080]LRESULT[/COLOR][/SIZE][SIZE=2] SendMessage([/SIZE][SIZE=2][COLOR=#008080]HWND[/COLOR][/SIZE][SIZE=2] handle, [/SIZE][SIZE=2][COLOR=#008080]UInt32[/COLOR][/SIZE][SIZE=2] msg, [/SIZE][SIZE=2][COLOR=#008080]WPARAM[/COLOR][/SIZE][SIZE=2] wParam, [/SIZE][SIZE=2][COLOR=#008080]LPARAM[/COLOR][/SIZE][SIZE=2] lParam);[/SIZE][/FONT]
[/SIZE]
Далее функцию установки позиции каретки и оборачиваем её:
Код:
[SIZE=2][FONT=Courier New][[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#008080]SecurityPermission[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#008080]SecurityAction[/COLOR][/SIZE][SIZE=2].Demand, UnmanagedCode = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New])][/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]internal [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void [/COLOR][/SIZE][SIZE=2]SetCaretPos([/SIZE][SIZE=2][COLOR=#008080]HWND[/COLOR][/SIZE][SIZE=2] handle, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] x, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] y)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] charIdx = SendMessage(handle, 0x00BB, y, 0) + x;[/FONT][/SIZE]
[SIZE=2][FONT=Courier New] SendMessage(handle, 0x00B1, charIdx, charIdx);[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void [/COLOR][/SIZE][SIZE=2]SetCaretPos([/SIZE][SIZE=2][COLOR=#008080]TextBoxBase[/COLOR][/SIZE][SIZE=2] control, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] caretX, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] caretY)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
[SIZE=2][FONT=Courier New] SetCaretPos(control.Handle, caretX, caretY);[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]internal [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void [/COLOR][/SIZE][SIZE=2]SetCaretPos([/SIZE][SIZE=2][COLOR=#008080]HWND[/COLOR][/SIZE][SIZE=2] handle, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] x, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] y)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] charIdx = SendMessage(handle, 0x00BB, y, 0) + x;[/FONT][/SIZE]
[SIZE=2][FONT=Courier New] SendMessage(handle, 0x00B1, charIdx, charIdx);[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
[FONT=Courier New][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void [/COLOR][/SIZE][SIZE=2]SetCaretPos([/SIZE][SIZE=2][COLOR=#008080]TextBoxBase[/COLOR][/SIZE][SIZE=2] control, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] caretX, [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][SIZE=2][FONT=Courier New] caretY)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
[SIZE=2][FONT=Courier New] SetCaretPos(control.Handle, caretX, caretY);[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]}[/FONT][/SIZE]