6

如果我有一个 winform,我可以知道如何使用 Ctrl + 鼠标滚轮来控制应用程序中字体的缩放级别(当然还有应用程序窗口本身)?我看到 Scroll Wheel 事件中有一个 Delta,但不确定它是如何工作的。有没有我可以查看的代码示例?

4

3 回答 3

6

我怀疑你可以测试:

(VB.NET):

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

(C#):

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

检查控制键是否按下。

于 2011-12-01T14:33:47.527 回答
5

您必须处理KeyDownandKeyUp事件才能确定Ctrl键是否被按住。这个值应该存储在类级别,因为它会被除KeyDownandKeyUp事件之外的其他子例程使用。

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

微软设置这个值的原因如下:

目前,120 的值是一个棘爪的标准。如果引入更高分辨率的鼠标,WHEEL_DELTA 的定义可能会变小。大多数应用程序应检查正值或负值,而不是总和。

在您的上下文中,您只需检查 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

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

  1. MSDN:Control.KeyDown 事件
  2. MSDN:Control.KeyUp 事件
  3. MSDN:Control.MouseWheel 事件
  4. MSDN:MouseEventArgs 类
于 2011-03-01T10:43:41.280 回答
0

对于 CrystalReportViewer1

只需在 Sub Zoom 中放置 CrystalReportViewer1.Zoom(ZoomValue) 而不是 Me.Text = ZoomValue.ToString() 行

于 2018-06-07T21:54:45.450 回答