我有一个Form1和一个PictureBox。我还订阅了MouseDown、MouseMove和MouseUp活动,以便能够用鼠标在PictureBox上绘制矩形。就其本身而言,它运行良好。现在我使用的是OpenFileDialog。如果我在窗口中选择文件并单击"确定",对话框将消失,但这是我的问题,因为我移动了鼠标,所以会立即绘制一个矩形。我不希望现在发生这种事。我已经尝试过使用布尔变量来锁定MouseMove过程,但不幸的是,它没有起作用。OpenFileDialog在这里你可以看到意外创建的矩形
Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class Form1
Private Mausstartpunkt As Point ' Mouse start point
Private Mausendpunkt As Point ' Mouse end point
Public Shared Property Picture1 As Bitmap 'dann bleibt es im Anwendungs-Scope, wenn die Klasse verfällt.
Private Pfad_Bild As String = ""
Private Pfad_speichern As String = ""
Private It_is_allowed_to_draw As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If PictureBox1.Image IsNot Nothing AndAlso It_is_allowed_to_draw AndAlso Not String.IsNullOrEmpty(Pfad_Bild) Then
Dim rect As Rectangle = PointsToRectangle(Mausstartpunkt, Mausendpunkt)
AllesGrafische.Paint_the_Rectangle(e.Graphics, rect)
End If
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
Mausstartpunkt = System.Windows.Forms.Control.MousePosition
Mausstartpunkt = New Point(Mausstartpunkt.X - 8, Mausstartpunkt.Y - 31)
End If
If e.Button = MouseButtons.Right Then ' clears the rectangle
Mausstartpunkt = New Point(0, 0)
Mausendpunkt = New Point(0, 0)
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
Mausendpunkt = System.Windows.Forms.Control.MousePosition
Mausendpunkt = New Point(Mausendpunkt.X - 8, Mausendpunkt.Y - 31)
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button = MouseButtons.Left Then
Mausendpunkt = System.Windows.Forms.Control.MousePosition
Mausendpunkt = New Point(Mausendpunkt.X - 8, Mausendpunkt.Y - 31)
PictureBox1.Invalidate()
End If
End Sub
Public Shared Function PointsToRectangle(ByVal p1 As Point, ByVal p2 As Point) As Rectangle 'https://www.vb-paradise.de/index.php/Thread/20037-Rechteck-mit-Maus-zeichnen-Erledigt/?postID=124893#post124893
Dim r As New Rectangle With {
.Width = Math.Abs(p1.X - p2.X),
.Height = Math.Abs(p1.Y - p2.Y),
.X = Math.Min(p1.X, p2.X),
.Y = Math.Min(p1.Y, p2.Y)
}
Return r
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Nothing
Using OFD As New CommonOpenFileDialog
OFD.Title = "Datei zum Öffnen auswählen"
OFD.Filters.Add(New CommonFileDialogFilter("Bilder", ".jpg;.jpeg;.bmp"))
OFD.Filters.Add(New CommonFileDialogFilter("PNG", ".png"))
OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD.ShowDialog() = CommonFileDialogResult.Ok Then
Pfad_Bild = OFD.FileName
Pfad_speichern = Pfad_Bild.Substring(0, Pfad_Bild.LastIndexOf("", StringComparison.Ordinal) + 1) ' for example "C:UsersmyNamePictures", or "C:UsersmyNameDesktop"
Else
Return
End If
End Using
Picture1 = New Bitmap(Pfad_Bild)
PictureBox1.Image = Picture1
It_is_allowed_to_draw = True
End Sub
这是我的图形类的代码
Imports System.Drawing.Drawing2D
Public NotInheritable Class AllesGrafische
Public Shared Sub Paint_the_Rectangle(ByVal g As Graphics, ByVal recta As Rectangle)
If g IsNot Nothing Then
g.SmoothingMode = SmoothingMode.AntiAlias
g.CompositingQuality = CompositingQuality.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBilinear
Using Pen_Hellblau As Pen = New Pen(Color.FromArgb(0, 200, 255), 1.0F)
g.DrawRectangle(Pen_Hellblau, recta)
End Using
End If
End Sub
End Class
在MouseDown
事件中将Boolean
字段设置为True
,然后仅在设置了该标志的情况下对MouseUp
执行操作。