用鼠标画一个矩形,但不是在 OpenfileDialog 忙的时候

问题描述

我有一个 Form1 和一个 PictureBox。我还订阅了 MouseDown、MouseMove 和 MouseUp 事件,以便能够用鼠标在 PictureBox 上绘制一个矩形。就其本身而言,它运行良好。现在我正在使用 OpenFileDialog。如果我在窗口中选择文件并单击“确定”,对话框就会消失,但是——这是我的问题——因为我移动了鼠标,立即绘制了一个矩形。我现在不希望这种情况发生。我已经尝试使用布尔变量来锁定 MouseMove 过程,但不幸的是这没有用。OpenFileDialog Here you can see the accidentally created rectangle

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:\Users\myName\Pictures\",or "C:\Users\myName\Desktop\"
            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

解决方法

Boolean 事件上将 True 字段设置为 MouseDown,然后仅在设置了该标志时才对 MouseUp 执行操作。