ComboBox 在下拉时如何捕获鼠标? [英] How does a ComboBox capture mouse when it is dropped-down?

查看:31
本文介绍了ComboBox 在下拉时如何捕获鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 ComboBox 下拉菜单(或其他与此相关的下拉菜单,包括上下文菜单)的行为进行建模,当您单击其他任何位置时,即使单击无法聚焦的内容,下拉菜单也会关闭.

I am trying to model the behavior of a ComboBox dropdown (or other drop downs for that matter, including context menus) where the drop down closes when you click anywhere else, even on something that can't be focused.

我尝试订阅诸如 MouseCaptureChanged、LostFocus 和 Leave 之类的事件.我有一个自定义的 UserControl,它充当下拉列表,我只想在用户单击其他任何地方时将其关闭.

I've tried subscribing for events such as MouseCaptureChanged, LostFocus, and Leave. I have a custom UserControl which is acting as a dropdown and I just want to close it up when the user clicks anywhere else.

这似乎是在许多控件中完成的事情,所以如果没有简单的方法来做到这一点,我会感到惊讶.

This seems like something that's done in many controls so I'd be surprised if there wasn't a simple way to do it.

到目前为止,我能想出的过于复杂的方法是使用 pinvoke 和 SetCapture() 函数,或者创建一个 MessageFilter.如果这些是唯一的选择,我不确定哪个更好.

So far the overcomplicated methods I can come up with to do this are using pinvoke and the SetCapture() function, or to create a MessageFilter. If these are the only options I am not sure which is better.

推荐答案

ComboBox 由 2 个控件构成.

The ComboBox is constructed from 2 controls.

  • 基础 - 不活动时可见(控制)
  • DropDownList - 在编辑模式或列表选择模式(窗口或表单)中可见

通常底座是可见的.当用户单击 ComboBox 时,Base 控件会隐藏,而 DropDownList 控件会出现.这个切换是在后台完成的,所以对于用户来说,控件似乎只是扩展了.

Normally the Base is visible. When the user clicks onto the ComboBox, the Base control hides and the DropDownList control shows up. This switch is done on the background, so for the user it seems the control just expanded.

您要捕获的事件是通过 DropDownList 窗口完成的.如果您单击客户区的某个位置,DropDownList 窗口将通过其 WndProc(Message% m) 方法接收 WM_KILLFOCUS 事件.然后将带有 WParam=526318 (HIWORD(WParam)=8) 并且 Base 控件知道他应该隐藏 DropDownList 窗口.

The event you want to catch is done through the DropDownList Window. If you click somewhere onto your client area, the DropDownList Window receives the WM_KILLFOCUS event through it's WndProc(Message% m) method. Then sends to the parent window (the Base control) a WM_COMMAND (OCM_COMMAND) message with WParam=526318 (HIWORD(WParam)=8) and the Base control knows he should hide the DropDownList Window.

因此,您需要做的是实现额外的下拉窗口并捕获 WM_KILLFOCUS 事件.

So, what you need to do is implement the additional DropDown Window and catch the WM_KILLFOCUS event.

这篇关于ComboBox 在下拉时如何捕获鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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