用鼠标按下进入Picturebox控件

时间:2014-09-15 19:30:12

标签: vb.net picturebox

我正在写一个“快速'脏”的图像编辑器。用户可以将图像加载到pbOriginal中,在其中拖动裁剪方块,并将其显示在pbAltered中。该功能适用​​于我的目的。

然而,当用户按下鼠标按钮进入图片框时,我遇到了编码问题。 (有点像拖动事件,但我不想拖动任何东西;我只是想用鼠标向下捕获输入。)理想情况下,我想让它在检测到输入后立即启动选择过程并且检测到活动的LMB,但到目前为止我没有抛出任何东西似乎都有效。

以下是我的两个代码段。第一个检测MouseDown事件,第二个检测MouseMove事件。我希望用MouseEnter模仿MouseDown函数的动作。 (第二个功能是无关紧要的,但为了完整起见,我将其包括在内。)

Private Sub pbOriginal_MouseDown(sender As Object, e As MouseEventArgs) Handles pbOriginal.MouseDown

         If e.Button = Windows.Forms.MouseButtons.Left Then
            cropX = e.X
            cropY = e.Y
            cropPen = New Pen(Color.Red, 2)
            cropPen.DashStyle = DashStyle.DashDotDot
            Altered = Nothing
        End If
        pbOriginal.Refresh()

End Sub

Private Sub pbOriginal_MouseMove(sender As Object, e As MouseEventArgs) Handles pbOriginal.MouseMove

    Cursor = Cursors.Cross

    If pbOriginal.Image Is Nothing Then Exit Sub
    If cropX < 0 Or cropY < 0 Then Exit Sub

    If e.Button = Windows.Forms.MouseButtons.Left And e.X <= pbOriginal.Right - pbOriginal.Left And e.X >= 0 And e.Y <= pbOriginal.Bottom - pbOriginal.Top And e.Y >= 0 Then

        pbOriginal.Refresh()

        Dim OldWidth As Integer = cropWidth
        Dim OldHeight As Integer = cropHeight

        cropWidth = e.X - cropX
        cropHeight = e.Y - cropY

        'Verify that we're maintaining a square
        If cropWidth > cropHeight Then
            If cropWidth > OldWidth Then
                cropWidth = cropHeight
            Else
                cropHeight = cropWidth
            End If
        End If
        If cropHeight > cropWidth Then
            If cropHeight > OldHeight Then
                cropHeight = cropWidth
            Else
                cropWidth = cropHeight
            End If
        End If

        If cropWidth <= 0 Or cropHeight <= 0 Then
            Altered = Nothing
            btnCropNext.Enabled = False
            Exit Sub
        End If


        pbOriginal.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
        If cropWidth <> 0 And cropHeight <> 0 Then
            Dim WRatio As Double = Original.Width / pbOriginal.Width
            Dim HRatio As Double = Original.Height / pbOriginal.Height
            Dim rect As Rectangle = New Rectangle(cropX * WRatio, cropY * HRatio, cropWidth * WRatio, cropHeight * HRatio)
            'First we define a rectangle with the help of already calculated points
            Dim OriginalImage As Bitmap = New Bitmap(Original, Original.Width, Original.Height)
            'Original image
            Dim _img As New Bitmap(CInt(cropWidth * WRatio), CInt(cropHeight * HRatio)) ' for cropinf image
            Dim g As Graphics = Graphics.FromImage(_img) ' create graphics
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.PixelOffsetMode = Drawing2D.PixelOffsetMode.Default
            g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
            'set image attributes
            g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel)
            Altered = _img
            btnCropNext.Enabled = True

            OriginalImage.Dispose()
        Else
            Altered = Nothing
            btnCropNext.Enabled = False
        End If

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

为什么不使用MouseEnter Event,并测试LMB是否被点击,如果是这样使你的选择进展,其他明智的疏忽。 有关MouseEnter的更多信息 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseenter(v=vs.110).aspx

如果您不想使用MouseEnter,请MouseMove测试每张图片MouseOver和LMB。但我认为第一个更容易和更快地处理