引用 acmilan:
mfc的CEditView实现了查找和替换功能,mfc的完整源代码可在vc/atlmfc/src找到。
只用SetWindowText改变Text还是会自动把光标设置为0 滚动条在最上面,使用你的
SetWindowText和我原来办法就完美了~~~
<code class="lang-c"> [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
public static extern int SetWindowText(int hwnd, string lpString);
private void button1_Click(object sender, EventArgs e)
{
int TextBoxSouSelectionStart = textBox1.SelectionStart;//TextBoxSou光标位置
int TextBoxSouSelectionLength = textBox1.SelectionLength;//TextBoxSou选中长度
int TextBoxSouScrollPos = GetScrollPos((int)textBox1.Handle, 1);//TextBox滚动条位置
SetWindowText((int)textBox1.Handle, textBox1.Text + "\r\n" + (Convert.ToUInt32(textBox1.Lines[textBox1.Lines.Length - 1]) + 1));
textBox1.Focus();
textBox1.SelectionStart = TextBoxSouSelectionStart;
textBox1.SelectionLength = TextBoxSouSelectionLength;
SetScrollPos(textBox1.Handle, 1, TextBoxSouScrollPos, true);
SendMessage(textBox1.Handle, 0x00B6, 0, TextBoxSouScrollPos);
}</code>