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

发布于 2024-10-19 13:10:15 字数 116 浏览 3 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末的微笑 2024-10-26 13:10:16

我怀疑你可以测试:

(VB.NET):(

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

C#):

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

来检查控制键是否按下。

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.

心的憧憬 2024-10-26 13:10:16

您必须处理 KeyDownKeyUp 事件以确定 Ctrl 键是否为被压制。该值应该存储在类级别,因为除了 KeyDownKeyUp 事件之外,其他子例程也会使用它。

然后编写代码来处理表单的 MouseWheel 事件。向下滚动(朝您的方向)会导致 MouseEventArgsDelta 属性。向上滚动显然是相反的。目前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 事件< /a>
  4. MSDN :MouseEventArgs类

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
沫雨熙 2024-10-26 13:10:16

对于 CrystalReportViewer1

只需放入 CrystalReportViewer1.Zoom(ZoomValue)
而不是子缩放中的 Me.Text = ZoomValue.ToString() 行

For CrystalReportViewer1

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文