【问题标题】:How can I move the cursor to the end of the text (Delphi)?如何将光标移动到文本的末尾(Delphi)?
【发布时间】:2010-10-08 03:06:57
【问题描述】:

这是我使用SendMessage 函数填充文本框的代码:

  C := 'Hey there';
  SendMessage(h1, WM_SETTEXT, 1, Integer(PChar(C)));

现在,如何将光标移动到文本的末尾?

【问题讨论】:

  • 哎哟!不是很德尔福式的。我认为 TextBox 不是 Delphi 组件?
  • SendMessage 函数是一个 API,没有规定只能在我们的项目中使用 windows API。例如我们使用这个函数来填充 yahoo messenger 的 Textbox 、 Editbox 、 Box 、 Field 。完全:“TextBox”是计算机上的一个虚拟框,我们使用键盘在其上键入!

标签: delphi focus sendmessage text-cursor


【解决方案1】:

如果您真的想对消息执行此操作,请查看:

  1. EM_SETSEL
  2. EM_EXSETSEL

还有完整的编辑参考:

http://msdn.microsoft.com/en-us/library/ff485923%28v=VS.85%29.aspx

在代码中(没有消息)你会做这样的事情:

Edit1.SelLength := 0;
Edit1.SelStart := 0;   // set caret before first character
...
Edit1.SelStart := 1;   // set caret before second character
...
Edit1.SelStart := Length(Edit1.Text) // set caret after the last character

有消息:

SendMessage(h1, EM_SETSEL, Length(C), Length(C));

【讨论】:

  • 谢谢亚军。但这个盒子不适合我的项目!我想用一个外部应用程序来做到这一点。
【解决方案2】:

我认为你的代码是错误的。你必须使用“EM_SETSEL”参数。我的问题用这段代码解决了:

  //Set a value for external textbox
  SendMessage(h1, WM_SETTEXT, 0, Integer(PChar(C)));
  //move the cursor to end of the textbox(editbox,field,...)
  SendMessage(h1, EM_SETSEL, length(C), length(C));

还是谢谢你:)

【讨论】:

  • 抱歉忘记更改消息常量。我发布了正确的链接。谢谢指出,我会马上解决的。
猜你喜欢
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
相关资源
最近更新 更多