当前位置:文档之家› Winform 屏蔽ComboBox控件鼠标滚轮滚动

Winform 屏蔽ComboBox控件鼠标滚轮滚动


方法1,直接在MouseWheel事件中写代码,缺点,每个控件都需要写代码才行.
Private Sub cboMacGroup_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles cboMacGroup.MouseWheel
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub


方法2, 用addHandler为MouseWheel绑定一个处理函数,可以多个控件共用同一个函数,但仍然是每个控件都需要AddHandler 后才生效

AddHandler comboBoxBeingWatched.MouseWheel, AddressOf buttonHandler

Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub



方法3: 建一个类,继承ComboBox控件,重写OnMouseWheel函数, 然后项目中全部使用MyComboBox,对于新建的窗体用这个方法最好,但如果项目已经完成,需要逐个替换,也是有点麻烦。
Friend Class MyComboBox
Inherits ComboBox

Protected Overrides Sub OnMouseWheel(ByVal e As MouseEventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
End Class



方法4:类似方法3,新建类继承ComboBox,重写WndProc方法,屏蔽鼠标滑轮滚动消息
c#
public class NoScrollCombo : ComboBox
{
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
protected override void WndProc(ref Message m)
{
if (m.HWnd != this.Handle)
{
return;
}

if (m.Msg == 0x020A) // WM_MOUSEWHEEL
{
return;
}

base.WndProc(ref m);
}
}

https://www.doczj.com/doc/6c12574778.html,
Public Class NoScrollCombo
Inherits ComboBox


Protected Overrides Sub WndProc(ByRef m As Message)
If m.HWnd <> Me.Handle Then
Return
End If

If m.Msg = &H20A Then ' WM_MOUSEWHEEL
Return
End If

MyBase.WndProc(m)
End Sub
End Class

方法5:
最后说一下DataGridView中使用ComboBox列时的屏蔽方法:
Private Sub dgItems_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgItems.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim cbo As ComboBox = e.Control
AddHandler cbo.MouseWheel, AddressOf disableMouseWheel
End If
End Sub

Private Sub disableMouseWheel(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub



上面提供一些基本思路,可以在此基础上再写一个通用的函数,在窗体内执行一次就行。
思路是遍历窗体所有控件,如果是ComboBox则用addHandler绑定上

面的disableMouseWheel函数,
如果是DataGridView则用addHandler为EditingControlShowing事件绑定一个函数,类似方法5

相关主题
文本预览
相关文档 最新文档