我正在使用以下代码使用滚轮放大图片框1中的\ out图像,但现在我想使用按钮而不是放大按钮和缩小按钮
谢谢提前 此代码来自:How to zoom in a Picturebox with scrollwheel in vb.netPublic Class Form1
Private _originalSize As Size = Nothing
Private _scale As Single = 1
Private _scaleDelta As Single = 0.0005
Private Sub Form_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
'if very sensitive mouse, change 0.00005 to something even smaller
_scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005
If e.Delta < 0 Then
_scale -= _scaleDelta
ElseIf e.Delta > 0 Then
_scale += _scaleDelta
End If
If e.Delta <> 0 Then _
PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)), _
CInt(Math.Round(_originalSize.Height * _scale)))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'init this from here or a method depending on your needs
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Size = Panel1.Size
_originalSize = Panel1.Size
End If
End Sub
End Class
答案 0 :(得分:0)
以下代码可以满足您的需求。当鼠标在按钮1上按下时,图片框将按比例缩小。您当然可以添加检查来设置最小尺寸。人们可能会对Application.DoEvents
的使用感到不满,但在这种情况下,如果您因为用户按下鼠标按钮并且没有做任何其他可能导致问题的事情,那么对您来说可能没问题。
基本上发生的事情是..当用户点击button1时,程序将变量mouseIsDown
设置为True并执行ShrinkPictureBox中的代码此循环将一直运行,直到mouseIsDown
设置为假。
Application.Doevents
使系统能够侦听MouseUp
事件。发生这种情况时,mouseIsDown
设置为false,循环结束。
button2也是如此,除了执行EnlargeBox代码。
我想在此添加Application.DoEvents
不要轻易使用。大多数时候这是一个坏主意,因为它允许用户点击你可能不希望他们点击的东西,如果一个程序正在忙着做某事。
Private _originalSize As Size = Nothing
Private _scale As Single = 1
Private _scaleDelta As Single = 0.00005
Dim mouseIsDown As Boolean = False
Private Sub ShrinkPictureBox()
Do While mouseIsDown
Application.DoEvents()
If PictureBox1.Size.Width > 2 Then
_scale -= _scaleDelta
PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)),
CInt(Math.Round(_originalSize.Height * _scale)))
PictureBox1.Refresh()
End If
Loop
End Sub
Private Sub EnlargePictureBox()
Do While mouseIsDown
Application.DoEvents()
_scale += _scaleDelta
PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)),
CInt(Math.Round(_originalSize.Height * _scale)))
PictureBox1.Refresh()
Loop
End Sub
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
mouseIsDown = True
ShrinkPictureBox()
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
mouseIsDown = False
End Sub
Private Sub Button2_MouseDown(sender As Object, e As MouseEventArgs) Handles Button2.MouseDown
mouseIsDown = True
EnlargePictureBox()
End Sub
Private Sub Button2_MouseUp(sender As Object, e As MouseEventArgs) Handles Button2.MouseUp
mouseIsDown = False
End Sub