Controlling WinForms Scale Levels Using the Mouse Scroll Wheel and Ctrl in VB.NET

If I have winform, can I find out how I can control the font zoom level in the application (as well as the application window itself) using Ctrl + Mouse Scroll Wheel? I see that there is a Delta in the Scroll Wheel event, but I don’t know how it works. Is there any code I can learn?

Thanks so much for all the help!

+5
source share
2 answers

You will need to process KeyDownand KeyUpto determine if the key is held down Ctrl. This value must be stored at the class level, because it will be used by other routines other than KeyDownand KeyUp.

Then you write code to process the form MouseWheel. Scrolling down (towards you) causes a negative value for the Deltaproperty MouseEventArgs. Scrolling up is obviously the other way around. The value of the Delta property is always 120.

Microsoft's reason for this value is as follows:

A value of 120 is currently the standard for a single stopper. If higher resolution mice are introduced, the definition of WHEEL_DELTA may be reduced. Most applications should check for a positive or negative value, not a general population.

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 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

:

+4

, :

(VB.NET):

If (ModifierKeys And Keys.Control) = Keys.Control Then

(#):

if( (ModifierKeys  & Keys.Control) == Keys.Control )

, .

+4

All Articles