问题描述
我正在尝试实现一个功能类似于组合框的 .net 表单控件,但我不知道在表单上的任何位置拦截鼠标事件以取消展开项目列表的正确方法.
I'm trying to implement a .net form control with functionality similar to a combo box, but I don't know the proper method to intercept mouse events anywhere on the form to un-expand the list of items.
如何在显示列表时阻止其他控件响应鼠标事件?
How do I prevent other controls from responding to mouse events while the list is being shown?
如何有效且安全地将鼠标单击事件捕获到表单上的任何位置,以隐藏展开的列表?
How do I efficiently and safely catch a mouse click event to anywhere on the form, to hide the expanded list?
推荐答案
只需将 ToolStripControlHost
与 ToolStripDropDown
一起使用,它就会像 一样工作组合框
下拉菜单.您不必担心处理鼠标事件.
Just use a ToolStripControlHost
along with the ToolStripDropDown
, and it will work just like the ComboBox
dropdown. You won't have to worry about handling the mouse events.
我以前用过这个:
Private Sub ShowControl(ByVal fromControl As Control, ByVal whichControl As Control)
'\ whichControl needs MinimumSize set:'
whichControl.MinimumSize = whichControl.Size
Dim toolDrop As New ToolStripDropDown()
Dim toolHost As New ToolStripControlHost(whichControl)
toolHost.Margin = New Padding(0)
toolDrop.Padding = New Padding(0)
toolDrop.Items.Add(toolHost)
toolDrop.Show(Me, New Point(fromControl.Left, fromControl.Bottom))
End Sub
在表单上使用 Button 控件的快速演示:
Quick Demo with a Button control on a form:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
ShowControl(Button1, New MonthCalendar)
End Sub
为了回答标题中的问题,我认为SetCapture 和 Release Capture 用于处理该类型的功能.
To answer the question in your title, I think the pinvoke calls of SetCapture and Release Capture are used to handle that type of functionality.
这篇关于组合框在显示列表项时,如何将鼠标事件拦截到窗体上的任意位置以隐藏列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!