MFC如何自动选择CEdit控件中的所有文本
我在 CMFCToolBar 上有一个 CMFCToolBarComboBoxButton。我希望每当 CMFCToolBarComboBoxButton 获得焦点时,它的整个文本都会被选择。
最优雅的实现方式是什么?
非常感谢!
阿迪巴尔达
I have a CMFCToolBarComboBoxButton on a CMFCToolBar. I want that whenever the CMFCToolBarComboBoxButton gets the focus its entire text will be selected.
What is the most elegant way to implement it?
Thanks a lot!
Adi Barda
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定最优雅的方法是什么,但我想最常见的方法是创建一个派生类并重写 OnSetFocus (未检查确切的方法名称),并在包含的编辑控件上调用 SetCurSel() 。 WM_FOCUS 仅发送到控件,并且没有通知消息,因此,除了执行一些使控件处理事件的操作之外,您没有太多选择 - 无论是将其反映到其他地方,还是只是实现该行为本身。 (我想从理论上讲,人们可以通过各种手段来拦截消息,但这肯定不是最优雅的方式......)
Not sure what the most elegant way is, but I guess the most common way to do this is to make a derived class and override OnSetFocus (exact method name not checked), and call SetCurSel() on the contained edit control. WM_FOCUS is only send to the control and there is no notification message for it afaik, so you'll have not many options besides doing something that will make the control handle the event - be it reflect it to somewhere else, or just implement the behavior itself. (I guess theoretically there's all sorts of finicking one can do with intercepting messages, but that's certainly not the most elegant way...)
这应该是标准编辑控件的默认行为。如果不是,则有其他东西正在显式删除该行为。也许您可以找到一些可以在组合框按钮上设置的标志?
如果不是:对编辑控件进行子类化,处理 WM_GETDLGCODE 消息。返回与 DLGC_HASSETSEL 进行或运算的值。如果控件设置了此标志,则 Windows 将自动选择焦点上的所有文本。
That should be the default behavior of the standard edit control. If it's not, something else is removing that behavior explicitly. Maybe you can find some flag you can set on the combobox button?
If not: subclass the edit control, handle the WM_GETDLGCODE message. Return a value ORed with
DLGC_HASSETSEL
. If the control has this flag set, then Windows will automatically select all text on focus.