C++/Win32 API - SetFocus 按钮不起作用
HWND button = CreateWindowEx(0, "BUTTON", ...);
SetFocus(button); // Button no get focus! :(
另外,我的表单上还有其他控件,我可以对其进行 SetFocus() 操作。
谢谢,马丁
HWND button = CreateWindowEx(0, "BUTTON", ...);
SetFocus(button); // Button no get focus! :(
Also, I have other controls on my form that I am able to SetFocus() to.
Thanks, Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自从我不得不这样做以来已经很久了,但是...
如果这是一个对话框,我会告诉您通过 PostMessage() 发送 WM_NEXTDLGCTL。默认对话框项消息处理程序将为您处理剩下的设置键盘焦点和选择激活。但是,如果我正确阅读的话,情况会有所不同。您正在即时创建原始的父窗口和子窗口。如果是这种情况,请 SetFocus() 到父窗口,并通过将其置于顶部来处理父窗口上的 WM_SETFOCUS,然后将焦点设置在子窗口上。 WM_SETFOCUS 和 WM_KILLFOCUS 旨在允许您切换控件的“激活”状态,并且大多数会为您处理它(除非您的窗口是所有者绘制控件或类似控件)。但是在原始窗口中,当您的基本父窗口被发送焦点时,如果您托管任何窗口,您需要适当地确保正确的子窗口拥有它(将其视为管理您自己的“对话框”)。同样,如果这是一个对话框,通常这是由默认对话框过程为您完成的,但作为原始窗口,您有点需要自己管理它。
虽然我无法想象如何,但我希望这有所帮助。
It has been FOREVER since I've had to do this, but...
Were this a dialog, I would tell you to send a WM_NEXTDLGCTL via PostMessage(). The default dialog item message handler would take care of the rest for you setting keyboard focus and selection activation. However, this is a different case if I read this correctly. You're creating both parent and child windows raw on the fly. If this is the case, SetFocus() to the parent window, and handle WM_SETFOCUS on the parent window by bringing it to top, then setting focus on the child window. WM_SETFOCUS, and WM_KILLFOCUS were designed to allow you to switch the 'activated' state of your controls, and most handle it for you (unless your window is an owner draw control or some such). But in a raw window, when your base parent window is sent the focus, you need to appropriately ensure the proper child has it if you're hosting any (think of it as managing your own 'dialog'). Again, normally this is done by the default dialog procedure for you if this were a dialog, but being raw windows you're kind of stuck managing it all yourself.
Though I can't imagine how, I hope that helped somewhat.
SetFocus 是一个函数,而不是一个过程。将其作为函数调用并检查其返回值。返回值要么为 null,因为您在 CreateWindowEx() 调用中出错,并且“button”不是有效句柄,要么它是一个与线程的消息队列不关联的窗口,要么返回值不为 null(它现在是先前聚焦的窗口句柄)并且您确实拥有焦点(但不知何故未能检测到它)。
SetFocus is a function, not a procedure. Call it as a function and check its returned value. Either the retuned value is null because you made an error in the CreateWindowEx() call and "button" isn't a valid handle or it's a window not associated with your thread's message queue, or the return value is not null (it's now the prior focused window's handle) and you do have the focus (but are somehow failing to detect it).
尝试在按钮上设置
WS_TABSTOP
样式。Try setting the
WS_TABSTOP
style on the button.如果您创建该按钮来响应 WM_INITDIALOG 消息,您应该返回 FALSE 以防止对话框过程更改焦点。
If you create that button in respond of the WM_INITDIALOG message you should return FALSE to prevent dialog box procedure to change the focus.