加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用鼠标滚轮和VB.NET中的Ctrl控制WinForms的缩放级别

发布时间:2020-12-17 00:33:19 所属栏目:大数据 来源:网络整理
导读:如果我有一个winform,我是否可以通过使用Ctrl鼠标滚轮来知道如何控制应用程序中的字体缩放级别(以及应用程序窗口本身)?我看到Scroll Wheel事件中有一个Delta,但不确定它是如何工作的.有没有我可以研究的代码示例? 非常感谢所有的帮助! 您必须处理 KeyDown
如果我有一个winform,我是否可以通过使用Ctrl鼠标滚轮来知道如何控制应用程序中的字体缩放级别(以及应用程序窗口本身)?我看到Scroll Wheel事件中有一个Delta,但不确定它是如何工作的.有没有我可以研究的代码示例?

非常感谢所有的帮助!

您必须处理 KeyDownKeyUp事件才能确定是否按住Ctrl键.此值应存储在类级别,因为除了KeyDown和KeyUp事件之外,其他子例程将使用它.

然后编写代码来处理表单的MouseWheel事件.向下滚动(朝向您)会导致MouseEventArgs的Delta属性为负值.向上滚动显然是相反的. Delta属性的值始终为120.

微软的这个价值的原因如下:

Currently,a value of 120 is the standard for one detent. If higher resolution mice are introduced,the definition of WHEEL_DELTA might become smaller. Most applications should check for a positive or negative value rather than an aggregate total.

在您的上下文中,您只需检查Delta的符号并执行操作.

以下是实现基本“缩放”功能的示例代码:

Public Class Form1
    Enum ZoomDirection
        None
        Up
        Down
    End Enum

    Dim CtrlIsDown As Boolean
    Dim ZoomValue As Integer

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ZoomValue = 100
    End Sub

    Private Sub Form1_KeyDown_KeyUp(ByVal sender As Object,_
                                    ByVal e As KeyEventArgs) _
                Handles Me.KeyDown,Me.KeyUp

        CtrlIsDown = e.Control
    End Sub

    Private Sub Form1_MouseWheel(ByVal sender As Object,ByVal e As MouseEventArgs) _
                Handles Me.MouseWheel

        'check if control is being held down
        If CtrlIsDown Then
            'evaluate the delta's sign and call the appropriate zoom command
            Select Case Math.Sign(e.Delta)
                Case Is < 0
                    Zoom(ZoomDirection.Down)
                Case Is > 0
                    Zoom(ZoomDirection.Up)
                Case Else
                    Zoom(ZoomDirection.None)
            End Select
        End If
    End Sub

    Private Sub Zoom(ByVal direction As ZoomDirection)
        'change the zoom value based on the direction passed

        Select Case direction
            Case ZoomDirection.Up
                ZoomValue += 1
            Case ZoomDirection.Down
                ZoomValue -= 1
            Case Else
                'do nothing
        End Select

        Me.Text = ZoomValue.ToString()
    End Sub
End Class

有关您的问题的更多信息,请阅读以下内容:

> MSDN: Control.KeyDown Event
> MSDN: Control.KeyUp Event
> MSDN: Control.MouseWheel Event
> MSDN: MouseEventArgs Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读