如何改进这个矩形的选择和绘制?

时间:2014-05-13 05:34:38

标签: .net vb.net image

我需要在我的应用程序上显示一个区域选择器,以在屏幕上选择一个矩形区域。

我已经尝试修改 Hans Passant this code example,代码用鼠标选择一个区域并绘制一个矩形,它仅适用于形式,而不是在屏幕上...但这是一个问题,以后,首先我需要的是改善这个选择器的功能。

目前只能从左上角到右下角进行矩形选择,我需要一个更灵活的选择,一步到位画出所有方向(我的意思是一个矩形,可以选择向右,左,下,上等)

我需要的选择灵活性的一个真实例子基本上就像 Windows资源管理器选择器那样:

enter image description here

我应该在代码中做些什么更改?:

PS:我也接受一种切割方式来模仿Windows选择矩形的灵活性(可能使用WinAPI?)。

Public Class Form1

Dim SelectionRectangle As Rectangle

Private Shadows Sub Load() Handles MyBase.Load
    Me.DoubleBuffered = True
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
    SelectionRectangle = New Rectangle(e.X, e.Y, 0, 0)
    Me.Invalidate()
End Sub

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)

    If SelectionRectangle.Width > 0 AndAlso SelectionRectangle.Height > 0 Then

        Dim sb As New System.Text.StringBuilder
        sb.AppendFormat("Selection Location: {0}", SelectionRectangle.Location.ToString)
        sb.AppendLine()
        sb.AppendFormat("Selection Size: {0}", SelectionRectangle.Size.ToString)
        MessageBox.Show(sb.ToString)

    End If

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

    If e.Button = MouseButtons.Left _
    AndAlso (SelectionRectangle.Right >= SelectionRectangle.X) _
    AndAlso (SelectionRectangle.Bottom >= SelectionRectangle.Y) Then

        SelectionRectangle = New Rectangle(SelectionRectangle.Left,
                                           SelectionRectangle.Top,
                                           e.X - SelectionRectangle.Left,
                                           e.Y - SelectionRectangle.Top)
        Me.Invalidate()

    Else
        SelectionRectangle = New Rectangle(SelectionRectangle.X,
                                           SelectionRectangle.Y,
                                           0, 0)
    End If

End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    Using pen As New Pen(Color.Red, 1)
        e.Graphics.DrawRectangle(pen, SelectionRectangle)
    End Using

End Sub

End Class

2 个答案:

答案 0 :(得分:1)

也许你可以在GreenShot应用程序的源代码中找到有用的东西!? http://sourceforge.net/projects/greenshot/

因为使用矩形陷阱来为您需要的屏幕截图选择区域!

答案 1 :(得分:1)

解决方案根据 @Digital_Utopia 评论:

这种方式与Windows资源管理器选择器具有相同的灵活性,除了矩形没有填充颜色并且矩形未被释放'鼠标移动后#39;因为这不是我需要的。

Dim SelectionRectangle As Rectangle
Dim InitialPosition As Point

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
    ' Store the starting coordinates
    InitialPosition = e.Location
    SelectionRectangle = New Rectangle(InitialPosition.X, InitialPosition.Y, 0, 0)
    Me.Invalidate()
End Sub

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

    ' Me.SuspendLayout()

    If e.Button = MouseButtons.Left Then

        If (e.Location.X < InitialPosition.X) _
        AndAlso (e.Location.Y < InitialPosition.Y) Then ' Top-Left

            SelectionRectangle = New Rectangle(e.X,
                                               e.Y,
                                               InitialPosition.X - e.X,
                                               InitialPosition.Y - e.Y)

        ElseIf (e.Location.X > InitialPosition.X) _
        AndAlso (e.Location.Y < InitialPosition.Y) Then ' Top-Right

            SelectionRectangle = New Rectangle(InitialPosition.X,
                                               e.Y,
                                               e.X - InitialPosition.X,
                                               InitialPosition.Y - e.Y)

        ElseIf (e.Location.X < InitialPosition.X) _
        AndAlso (e.Location.Y > InitialPosition.Y) Then ' Bottom-Left

            SelectionRectangle = New Rectangle(e.X,
                                               InitialPosition.Y,
                                               InitialPosition.X - e.X,
                                               e.Y - InitialPosition.Y)

        ElseIf (e.Location.X > InitialPosition.X) _
        AndAlso (e.Location.Y > InitialPosition.Y) Then ' Bottom-Right

            SelectionRectangle = New Rectangle(InitialPosition.X,
                                               InitialPosition.Y,
                                               e.X - InitialPosition.X,
                                               e.Y - InitialPosition.Y)
        End If

        Me.Invalidate()

    End If

    ' Me.ResumeLayout()

End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    Using pen As New Pen(Color.Red, 1)
        e.Graphics.DrawRectangle(pen, SelectionRectangle)
    End Using

End Sub