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?
3 Answers
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.

- 1
- 1

- 21,638
- 3
- 67
- 78
-
This should be the accepted answer. https://stackoverflow.com/questions/268796/control-key-plus-mouse-wheel – PerpetualStudent Dec 18 '18 at 13:14
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:

- 12,339
- 9
- 70
- 108
-
Thanks so much for such detailed answer! This has everything I was looking for! – AZhu Mar 02 '11 at 14:46
-
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? – AZhu Mar 02 '11 at 19:27
-
BTW, sorry I do not have enough reputation to vote this answer up yet, I'd have done that if I could! – AZhu Mar 02 '11 at 19:27
-
@zhuanyi: Sorry I totally forgot about that. Just set the form's `KeyPreview` property to `True` – Alex Essilfie Mar 02 '11 at 20:34
-
-
@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`. – Alex Essilfie Mar 03 '11 at 10:28
For CrystalReportViewer1
Just put CrystalReportViewer1.Zoom(ZoomValue) instead of the line Me.Text = ZoomValue.ToString() in the Sub Zoom

- 1
- 1
-
OP's question was not about CrystalReport. May be it is a good answer, but not to this question. – Adrian W Jun 07 '18 at 22:46