使用WM_SETFOCUS和WM_KILLFOCUS [英] Using WM_SETFOCUS and WM_KILLFOCUS

查看:735
本文介绍了使用WM_SETFOCUS和WM_KILLFOCUS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi中,我有两个编辑框和一个按钮。默认情况下选择Edit1。我想使用消息来改变焦点。但是如果我这样做,这两个编辑,插入错误的框中的选择范围都被弄乱了。我使用消息的原因是我可以控制外部应用程序的焦点。这似乎起作用,直到一点,但显然windows内部状态有点乱。我没有外部程序的来源。

In Delphi I have two edit boxes and a button. Edit1 is selected by default. I want to change focus using messages. But if I do as below, it all gets messed up with selection ranges in both edits, caret in the wrong box etc. The reason I'm using messages is so I can control focus in an external application. This seems to work, up to a point but clearly the windows internal state is a bit scrambled. I don't have the source for the external program.

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostMessage(edit1.handle,WM_KILLFOCUS,0,0);
  PostMessage(edit2.handle,WM_SETFOCUS,0,0);
end;

...那么可以做到吗?我错过了一个消息?

... So can it be done? Am I missing a message?

推荐答案

WM_SETFOCUS和WM_KILLFOCUS都是Windows发送到窗口句柄的通知消息,当它们接收并丢失输入重点,你不应该发布自己的。相反,只需调用 SetFocus(edit2.handle) edit2.SetFocus()设置焦点。

WM_SETFOCUS and WM_KILLFOCUS are both notification messages that Windows sends to window handles when they receive and lose input focus, respectively, and you should not post those yourself. Instead, simply call SetFocus(edit2.handle) or edit2.SetFocus() to set the focus.

如果由于某种原因,您无法从按钮点击处理程序同步执行,您可以以自己的形式将自定义消息发布到本地消息处理程序,并将SetFocus调用从该消息处理程序。

If for some reason you can't do that synchronously from your button click handler, you can post a custom message to a local message handler in your own form and make the SetFocus call from that message handler.

这篇关于使用WM_SETFOCUS和WM_KILLFOCUS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆