Control zoom level of WinForms using mouse scroll wheel and Ctrl in VB.NET
Asked Answered
D

3

6

If I have a winform, may I know how can I control the zoom level of the font in the application (as well as the application window itself obviously) by using Ctrl + Mouse Scroll Wheel? I see there is a Delta in the Scroll Wheel event, but not sure how that works. Is there any code sample that I can look into?

Deeann answered 1/3, 2011 at 1:47 Comment(0)
B
5

You'll have to handle the KeyDown and KeyUp event in order to determine whether or not Ctrl key is being held down. This value should be stored at class-level because it will be used by other subroutines besides the KeyDown and KeyUp events.

You then write code to handle the form's MouseWheel event. Scrolling downwards (towards you) causes a negative value for the Delta property of the MouseEventArgs. Scrolling upwards is obviously the reverse. The value of the Delta property is always currently 120.

Microsoft's reason for this value is as follows:

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.

In your context you'll just check for the sign of the Delta and perform an action.

Here is a sample code implementing basic 'zoom' functionality:

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

Read on the following for more information about your question:

  1. MSDN: Control.KeyDown Event
  2. MSDN: Control.KeyUp Event
  3. MSDN: Control.MouseWheel Event
  4. MSDN: MouseEventArgs Class
Bilabial answered 1/3, 2011 at 10:43 Comment(6)
Thanks so much for such detailed answer! This has everything I was looking for!Deeann
Just one problem though...for some reason my code seems to be able to capture the mouse movements but not the key down and key up part unless I use the mouse the click on the form first...is that expected or I am just doing something silly?Deeann
BTW, sorry I do not have enough reputation to vote this answer up yet, I'd have done that if I could!Deeann
@zhuanyi: Sorry I totally forgot about that. Just set the form's KeyPreview property to TrueBilabial
Right before CtrlIsDown = e.Control? Thanks!Deeann
@zhuanyi: You can set that anywhere but preferably, it should be done in the Designer view otherwise do it during the form's initializer (New()) or at Form.Load.Bilabial
R
6

I suspect that you can just test:

(VB.NET):

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

(C#):

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

to check if the control key is down.

Reseda answered 1/12, 2011 at 14:33 Comment(1)
This should be the accepted answer. https://mcmap.net/q/971317/-control-key-plus-mouse-wheelMckay
B
5

You'll have to handle the KeyDown and KeyUp event in order to determine whether or not Ctrl key is being held down. This value should be stored at class-level because it will be used by other subroutines besides the KeyDown and KeyUp events.

You then write code to handle the form's MouseWheel event. Scrolling downwards (towards you) causes a negative value for the Delta property of the MouseEventArgs. Scrolling upwards is obviously the reverse. The value of the Delta property is always currently 120.

Microsoft's reason for this value is as follows:

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.

In your context you'll just check for the sign of the Delta and perform an action.

Here is a sample code implementing basic 'zoom' functionality:

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

Read on the following for more information about your question:

  1. MSDN: Control.KeyDown Event
  2. MSDN: Control.KeyUp Event
  3. MSDN: Control.MouseWheel Event
  4. MSDN: MouseEventArgs Class
Bilabial answered 1/3, 2011 at 10:43 Comment(6)
Thanks so much for such detailed answer! This has everything I was looking for!Deeann
Just one problem though...for some reason my code seems to be able to capture the mouse movements but not the key down and key up part unless I use the mouse the click on the form first...is that expected or I am just doing something silly?Deeann
BTW, sorry I do not have enough reputation to vote this answer up yet, I'd have done that if I could!Deeann
@zhuanyi: Sorry I totally forgot about that. Just set the form's KeyPreview property to TrueBilabial
Right before CtrlIsDown = e.Control? Thanks!Deeann
@zhuanyi: You can set that anywhere but preferably, it should be done in the Designer view otherwise do it during the form's initializer (New()) or at Form.Load.Bilabial
S
-1

For CrystalReportViewer1

Just put CrystalReportViewer1.Zoom(ZoomValue) instead of the line Me.Text = ZoomValue.ToString() in the Sub Zoom

Salespeople answered 7/6, 2018 at 21:54 Comment(1)
OP's question was not about CrystalReport. May be it is a good answer, but not to this question.Vibration

© 2022 - 2024 — McMap. All rights reserved.