我有一个类可以更改图片框的鼠标事件参数,并允许用户在运行时调整它的大小。我正在尝试为每个图片框添加一个复选框,以便在选中复选框时保持图片框的宽高比。
如果我为表单上的每个复选框单独添加代码,我可以使用它,但我想将它保存在一个单独的类中,以便它适用于任何复选框。
以下是调整图片框大小的类的代码
Public Class ResizeableControl
Public WithEvents mControl As Control
Public mPreserveAspectRatio As Boolean
Dim AtRightEdge As Boolean = False
Dim AtBottomEdge As Boolean = False
Dim InBoxWidth As Boolean = False
Dim InBoxHeight As Boolean = False
Dim DraggingHorizontal As Boolean
Dim DraggingVerticle As Boolean
Dim DraggingCorner As Boolean
Const DragMarginWidth As Integer = 6
Const DragMarginHeight As Integer = 6
Public dragOrigin As Point
Dim MoveBox As Boolean = False
Dim LastPos As Point
Public Sub New(ByVal Control As Control, ByVal preserveAspectRatio As Boolean)
mControl = Control
mPreserveAspectRatio = preserveAspectRatio
End Sub
Private Sub mControl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
If MoveBox Then
Dim movement As Point = Cursor.Position
' move image by the distance the mouse moved hoizontally and vertically
movement.Offset(-LastPos.X, -LastPos.Y)
mControl.Location = movement
ElseIf DraggingHorizontal Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y) 'How far did the mouse move? (Distance = newPoint - oldPoint)
If mPreserveAspectRatio = True Then
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
Else
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
End If
dragOrigin = e.Location 'Next time we will measure from the now-current mouse position
ElseIf DraggingVerticle Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
End If
dragOrigin = e.Location
ElseIf DraggingCorner Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
End If
dragOrigin = e.Location
Else
' Is mouse within right six-or-so pixels?
AtRightEdge = e.X > (mControl.Width - DragMarginWidth)
' Is mouse within bottom six-or-so pixels?
AtBottomEdge = e.Y > (mControl.Height - DragMarginHeight)
' Is mouse within the box?
InBoxWidth = e.X < (mControl.Width - DragMarginWidth)
InBoxHeight = e.Y < (mControl.Height - DragMarginHeight)
' Set the cursor accordingly
If (AtBottomEdge And AtRightEdge) Then
mControl.Cursor = Cursors.SizeNWSE
ElseIf (InBoxWidth And InBoxHeight) Then
mControl.Cursor = Cursors.SizeAll
ElseIf (AtBottomEdge) Then
mControl.Cursor = Cursors.SizeNS
ElseIf (AtRightEdge) Then
mControl.Cursor = Cursors.SizeWE
Else
mControl.Cursor = Cursors.Default
End If
End If
End Sub
Private Sub mControl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
' If the user presses the mouse button at the bottom right corner, begin dragging
If (InBoxWidth And InBoxHeight) Then
Dim movement As Point = Cursor.Position
movement.Offset(-mControl.Location.X, -mControl.Location.Y)
LastPos = movement
MoveBox = True
ElseIf (AtBottomEdge And AtRightEdge) Then
dragOrigin = e.Location
DraggingCorner = True
ElseIf AtRightEdge Then
dragOrigin = e.Location
DraggingHorizontal = True
ElseIf AtBottomEdge Then
dragOrigin = e.Location
DraggingVerticle = True
End If
End Sub
Private Sub mControl_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
' Stop dragging
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
End Sub
Private Sub mControl_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
mControl.Cursor = Cursors.Default
End Sub
End Class
我为每个图片框创建了一个类的新实例(我只有2个,这里保持简单但有更多)当表单加载但后来我无法弄清楚如何更改宽高比布尔值(mPreserveAspectRatio) 。我曾尝试在复选框更改状态时更改布尔值,但这不起作用。我想我可能需要以某种方式创建一个公共方法来改变布尔值,但不能包围我的头。以下是我一直在尝试的最新代码
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mControl As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox1, False)
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox2, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox2, False)
End If
End Sub
End Class
答案 0 :(得分:0)
我明白了。你指出我正确的方向杰夫。我确实需要将控件添加到表单中。这就是我做的。
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Control1 As New ResizeableControl(PictureBox1, True)
Me.mControl1 = Control1
Dim Control2 As New ResizeableControl(PictureBox2, True)
Me.mControl2 = Control2
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Me.mControl1.mPreserveAspectRatio = True
Else
Me.mControl1.mPreserveAspectRatio = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Me.mControl2.mPreserveAspectRatio = True
Else
Me.mControl2.mPreserveAspectRatio = False
End If
End Sub
End Class