win32 c++检测“输入”;在没有子类化的编辑控件中?
基本上,我想要一个 Enter 来触发一条消息,当编辑控件获得焦点并且用户按下 Enter 时,我可以捕获该消息。网上的所有解决方案似乎都是关于子类化的,但我想知道是否还有另一种方法?
例如,我的按钮有一个标识符 ID_BUTTON_SEND。这是我的想象;
case WM_COMMAND:
switch (LOWORD(wParam))
case ID_BUTTON_SEND
if ('enter was pressed')
do this
else
default
...你明白了:)我已经阅读了 http://support.microsoft.com/kb /102589 但坦率地说,选项 1 对我来说没有多大意义。
干杯
Basically I want an Enter to trigger a message I can catch when a edit control har focus and a user press enter. All solutions online seems to be about subclassing, but I was wondering if there was another way around it?
For example, my button has an identifier ID_BUTTON_SEND. Here's how I imagine it;
case WM_COMMAND:
switch (LOWORD(wParam))
case ID_BUTTON_SEND
if ('enter was pressed')
do this
else
default
...you get the idea :) I've read the http://support.microsoft.com/kb/102589 but frankly option 1 dosn't make much sense to me.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捕获此问题的最佳方法是在调用 TranslateMessage 之前。因此,如果使用 MFC,请重写 CWnd::PreTranslateMessage。如果仅使用 Win API,则只需在调用 TranslateMessage 之前检查消息泵中消息包含的内容即可。
Best way to catch this is before TranslateMessage gets called. So, if using MFC, override CWnd::PreTranslateMessage. If using only Win API, then just check in your message pump what the message contains before the call to TranslateMessage.
您可以捕获焦点更改事件,当编辑控件获取焦点事件时,只需将对话框默认按钮更改为 *ID_BUTTON_SEND* 按钮。然后,当焦点丢失时,删除此默认按钮标志。
这意味着每当用户在编辑控件具有焦点时按下 Enter 键时,对话框都会自动触发 *ID_BUTTON_SEND* 默认按钮。
您可以通过将 BS_DEFPUSHBUTTON 添加到按钮的 GWL_STYLE 来使该按钮成为默认按钮。
You could trap the focus change event and when the edit control gets the focus event just change the dialog default button to be the *ID_BUTTON_SEND* button. Then when the focus is lost remove this default button flag.
That would means that whenever the user hits enter when the edit control has the foucs the dialog would automatically fire the *ID_BUTTON_SEND* default button.
You can make the button the default button by adding the BS_DEFPUSHBUTTON to the GWL_STYLE of the button.
只是重申一下知识库文章。对于选项 1,您实际上可以简单地在 WM_COMMAND 中处理 IDOK。
这是检查 Enter 的更简单、更干净的方法。
Just to reiterate upon the KB article. For option 1 you can actually simply handle IDOK in WM_COMMAND.
This is a much easier and cleaner way to check for the Enter.