我正在尝试执行一项我不知道是否可以执行的功能。我有两个我想要混合的程序。
第一个允许我通过左键单击鼠标在PictureBox中绘制点,然后将x,y coordenates存储在以下形式的标签中:
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共同显示并在标签中显示。
好的,现在我开发了另一个应用程序,我可以在PictureBox中绘制,就像我在Paint中工作一样:
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
您可以注意到,我在两种情况下都使用鼠标事件。我想要实现的是将两个程序放在一个,并使用两个按钮,能够选择我想要的功能。
我遇到的问题是,我不知道如何控制鼠标事件,因为现在正在绘制第一个程序的点,同时绘制第二个程序的行。
有一种方法可以说我想让第一个功能使用鼠标事件,或者使用两个按钮选择第二个功能的鼠标事件? (第一个功能的第一个按钮和第二个功能的第二个按钮)..我喜欢将两个程序混合成一个我想要做的事情。
非常感谢你能给我的任何帮助!!
答案 0 :(得分:1)
尝试类似:
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
即可调整将应用的代码。
答案 1 :(得分:1)
最后我通过使用两个Checkbox来完成此操作:
Imports Emgu.CV
Imports Emgu.CV.Util
Imports Emgu.CV.Structure
Imports System.Drawing
Imports Emgu.CV.VideoSurveillance
Imports System.Drawing.Imaging
Imports System.Reflection
Imports System.Drawing.Drawing2D
Public Class Form1
Dim img As Image(Of Bgr, Byte)
Dim a As Integer
Dim b As Integer
Dim X As Integer
Dim Y As Integer
Dim localMousePosition As Point
Dim arrayXPoints(10) As Integer
Dim arrayYPoints(10) As Integer
Dim mustPaint As Boolean = False
Private lastPT As Point
Private signature As New GraphicsPath
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
#Region "Point"
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If CheckBoxPoint.Checked = True Then
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 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
Private Sub UpdatePositionLabels()
TextX1.Text = arrayXPoints(1)
TextX2.Text = arrayXPoints(2)
TextX3.Text = arrayXPoints(3)
TextX4.Text = arrayXPoints(4)
TextX5.Text = arrayXPoints(5)
TextX6.Text = arrayXPoints(6)
TextX7.Text = arrayXPoints(7)
TextX8.Text = arrayXPoints(8)
TextX9.Text = arrayXPoints(9)
TextX10.Text = arrayXPoints(10)
TextY1.Text = arrayYPoints(1)
TextY2.Text = arrayYPoints(2)
TextY3.Text = arrayYPoints(3)
TextY4.Text = arrayYPoints(4)
TextY5.Text = arrayYPoints(5)
TextY6.Text = arrayYPoints(6)
TextY7.Text = arrayYPoints(7)
TextY8.Text = arrayYPoints(8)
TextY9.Text = arrayYPoints(9)
TextY10.Text = arrayYPoints(10)
End Sub
Private Sub processImage(bitmap As Bitmap)
'Define a and b to determinate the position of the PictureBox
a = PictureBox1.Width / 2 '- radius
b = PictureBox1.Height / 2 '- radius
Dim centerPictureBox2 As PointF = New PointF(a, b)
Dim i As Integer
Dim n As Integer
'lock image
Dim bitmapData As BitmapData
bitmapData = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat)
'locating objects
Dim blobCounter As AForge.Imaging.BlobCounter = New AForge.Imaging.BlobCounter
blobCounter.FilterBlobs = True
blobCounter.MinHeight = 5
blobCounter.MinWidth = 5
blobCounter.ProcessImage(bitmapData)
Dim blobs As AForge.Imaging.Blob() = blobCounter.GetObjectsInformation()
bitmap.UnlockBits(bitmapData)
n = blobs.Length
'step 3 - check objects' type and highlight
Dim shapeChecker As AForge.Math.Geometry.SimpleShapeChecker = New AForge.Math.Geometry.SimpleShapeChecker()
Dim g As Graphics = Graphics.FromImage(bitmap)
Dim yellowPen As Pen = New Pen(Color.Yellow, 2)
For i = 0 To n
If i < n Then
Dim edgePoints As List(Of AForge.IntPoint) = blobCounter.GetBlobsEdgePoints(blobs(i))
Dim center As AForge.DoublePoint
Dim radius As Double
'is circle?
If shapeChecker.IsCircle(edgePoints, center, radius) Then
'g.DrawEllipse(yellowPen, CType((center.X - radius), Decimal), CType((center.Y - radius), Decimal), CType((radius * 2), Decimal), CType((radius * 2), Decimal))
Dim centerImage As New PointF(center.X, center.Y)
Dim circle As CircleF = New CircleF(centerImage, radius)
Dim pointOfCircle As CircleF = New CircleF(centerImage, 0)
Dim pointOFCenterPictureBox2 As CircleF = New CircleF(centerPictureBox2, 0)
Dim imageCircle As Image(Of Bgr, Byte) = img '.CopyBlank()
Dim imagePointOfCircle As Image(Of Bgr, Byte) = img
Dim imagePointOfPictureBox2 As Image(Of Bgr, Byte) = img
imageCircle.Draw(circle, New Bgr(Color.Green), 10)
imagePointOfCircle.Draw(pointOfCircle, New Bgr(Color.Green), 10)
imagePointOfPictureBox2.Draw(pointOFCenterPictureBox2, New Bgr(Color.Yellow), 10)
PictureBox1.Image = imageCircle.ToBitmap()
PictureBox1.Image = imagePointOfCircle.ToBitmap()
PictureBox1.Image = imagePointOfPictureBox2.ToBitmap()
End If
End If
Next
Clipboard.SetDataObject(bitmap)
End Sub
Private Sub ResetPositionLabels()
For i As Integer = 1 To 10
arrayXPoints(i) = 0
Next
For i As Integer = 1 To 10
arrayYPoints(i) = 0
Next
UpdatePositionLabels()
End Sub
Private Sub DistanceBetweenCenters(centerImage As PointF)
' Dim centerIdeal As New PointF(a, b)
' Dim distanceBetween As Decimal
' If centerIdeal.X > centerImage.X And centerIdeal.Y > centerImage.Y Then
' distanceBetween = Math.Sqrt((Math.Abs(centerIdeal.X - centerImage.X) ^ 2) + (Math.Abs(centerIdeal.Y - centerImage.Y) ^ 2))
' ElseIf centerIdeal.X <= centerImage.X And centerIdeal.Y > centerImage.Y Then
' distanceBetween = Math.Sqrt((Math.Abs(centerImage.X - centerIdeal.X) ^ 2) + (Math.Abs(centerIdeal.Y - centerImage.Y) ^ 2))
' ElseIf centerIdeal.X > centerImage.X And centerIdeal.Y <= centerImage.Y Then
' distanceBetween = Math.Sqrt((Math.Abs(centerIdeal.X - centerImage.X) ^ 2) + (Math.Abs(centerImage.Y - centerIdeal.Y) ^ 2))
' ElseIf centerIdeal.X <= centerImage.X And centerIdeal.Y <= centerImage.Y Then
' distanceBetween = Math.Sqrt((Math.Abs(centerImage.X - centerIdeal.X) ^ 2) + (Math.Abs(centerImage.Y - centerIdeal.Y) ^ 2))
' End If
' TextBox1.Text = "Dist. centros: " & distanceBetween
' TextBoxCentroPantalla.Text = "Centro pantalla: " & a & ", " & b
' TextBoxWidth.Text = "Width: " & PictureBox2.Width
' TextBoxHeight.Text = "Height: " & PictureBox2.Height
End Sub
Private Sub RestartButton_Click(sender As Object, e As EventArgs) Handles RestartButton.Click
Timer1.Stop()
PictureBox1.Image = Nothing
PictureBox1.Image = Bitmap.FromFile("C:\Users\Dlozano\Desktop\Proyecto Calibración y Apunte M109\imágenes de prueba\negro640x480.png")
PosicionRatonX.Text = Nothing
PosicionRatonY.Text = Nothing
ResetPositionLabels()
End Sub
Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles CargaFoto.Click
' Show the Open File dialog. If the user clicks OK, load the
' picture that the user chose.
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
PictureBox1.Load(OpenFileDialog1.FileName)
End If
Dim imgbm As Bitmap = PictureBox1.Image
img = New Image(Of Bgr, Byte)(imgbm)
processImage(PictureBox1.Image)
Timer1.Start()
End Sub
Private Sub CheckBoxLine_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxLine.CheckedChanged
If CheckBoxLine.Checked = True Then
CheckBoxPoint.Checked = False
CheckBoxPoint.Enabled = False
Else
CheckBoxPoint.Enabled = True
End If
End Sub
Private Sub CheckBoxPoint_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxPoint.CheckedChanged
If CheckBoxObjective.Checked = True Then
CheckBoxLine.Checked = False
CheckBoxLine.Enabled = False
Else
CheckBoxLine.Enabled = True
End If
End Sub
#End Region
#Region "Line"
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If CheckBoxLine.Checked = True Then
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 If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If CheckBoxLine.Checked = True Then
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 If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If CheckBoxLine.Checked = True Then
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
signature.StartFigure()
End If
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 CheckBoxLine.Checked = True Then
If Not IsNothing(signature) Then
e.Graphics.DrawPath(Pens.Black, signature)
End If
End If
End Sub
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If CheckBoxLine.Checked = True Then
mustPaint = True
End If
End Sub
Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If CheckBoxLine.Checked = True Then
If mustPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
End If
End If
End Sub
Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
If CheckBoxLine.Checked = True Then
mustPaint = False
End If
End Sub
#End Region
End Class
如果可以帮助某人,我发布所有代码。 我使用另一个项目完成了这项工作,我从计算机中的路径加载图像,全黑图像。加载图像后,代码检查图像中是否有任何圆圈,并将该图像设置为白色并绘制圆圈和该圆圈的中心。这就是我母亲的计划。该程序有三个按钮,一个用于加载图片,另一个用于重置表单,另一个用于关闭表单
我添加的是用于控制鼠标事件的复选框。在这两个区域内,我检查是否启用了其中一个复选框。如果是,我禁用另一个复选框,并且在具有我想要对鼠标事件执行的功能的函数内部,我询问是否单击了复选框。如果是,则该功能实现其目的。
此外,我正在注册我在PictureBox上放置点的协调人。
我希望这对某人有用,如果我必须解释一些事情,请告诉我。
我的最终解决方案非常类似于@TGamer所说的。感谢您的回复,因为它对我有很大的帮助!