我正在尝试做一个我不知道是否可以做的功能。我有两个程序要混合使用。
第一个,允许我通过用鼠标左键单击来在PictureBox中绘制点,然后将x,y坐标存储在以下形式的标签中:
Dim X As Integer
Dim Y As Integer
Dim localMousePosition As Point
Dim arrayXPoints(10) As Integer
Dim arrayYPoints(10) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = Bitmap.FromFile("C:\Users\Dlozano\Desktop\Proyecto Calibración y Apunte M109\imágenes de prueba\negro640x480.png")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If MouseButtons = Windows.Forms.MouseButtons.Left And PictureBox1.Image IsNot Nothing Then
localMousePosition = PictureBox1.PointToClient(Cursor.Position)
X = localMousePosition.X
Y = localMousePosition.Y
PosicionRatonX.Text = X.ToString
PosicionRatonY.Text = Y.ToString
For i As Integer = 1 To 10
If arrayXPoints(i) = Nothing Then
arrayXPoints(i) = X
arrayYPoints(i) = Y
Exit For
Else
Continue For
End If
Next
UpdatePositionLabels()
DrawPoint(X, Y)
End If
End Sub
Private Sub DrawPoint(X As Integer, Y As Integer)
a = X
b = Y
Dim radius As Decimal = 10.0F
Dim centerImage As New Point(a, b)
Dim circle As CircleF = New CircleF(centerImage, 0)
img = New Image(Of Bgr, Byte)(PictureBox1.Image)
Dim imageCircle As Image(Of Bgr, Byte) = img
imageCircle.Draw(circle, New Bgr(Color.Brown), 10)
PictureBox1.Image = imageCircle.ToBitmap()
End Sub
在这里,我将一个黑色图像加载到PictureBox中,然后在PictureBox中单击时,将绘制一个红点,取x,y坐标并将其显示在标签中。
好的,现在我已经开发了另一个应用程序,可以像在Paint中工作一样在PictureBox中进行绘制:
Dim mustPaint As Boolean = False
Private lastPT As Point
Private signature As New GraphicsPath
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
lastPT = New Point(e.X, e.Y)
End If
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim curPt As New Point(e.X, e.Y)
signature.AddLine(lastPT, curPt)
lastPT = curPt
PictureBox1.Refresh()
End If
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
signature.StartFigure()
End If
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If Not IsNothing(signature) Then
e.Graphics.DrawPath(Pens.Black, signature)
End If
End Sub
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
mustPaint = True
End Sub
Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If mustPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
End If
End Sub
Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
mustPaint = False
End Sub
如您所见,在两种情况下我都使用鼠标事件。我要实现的是将两个程序合而为一,并使用两个按钮来选择我想要的功能。
我遇到的问题是,我不知道如何控制鼠标事件,因为现在正在同时绘制第一个程序的点和第二个程序的行。
有两种方法可以对程序说,我想通过两个按钮选择鼠标事件以用于第一个功能,或将鼠标事件用于第二个功能? (第一个按钮用于第一个功能,第二个按钮用于第二个功能)..就像将两个程序混合到一个我要执行的操作中。
非常感谢您能给我的任何帮助!
最佳答案
尝试类似:
Dim first As Boolean
Dim second As Boolean
Private Sub Button1.CLick(...)Handles Button1.CLick
first = true
second = false
End Sub
Private Sub Button2.Click(...)Handles Button2.CLick
first = false
second = true
End Sub
之后,只需添加
If first = true
和ElseIf second = true
即可调整要应用的代码。