
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class TransPanelForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Label
Friend WithEvents Panel2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Label
Me.Panel2 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.BackColor = System.Drawing.Color.Transparent
Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Left
Me.Panel1.Location = New System.Drawing.Point(10, 10)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(270, 266)
Me.Panel1.TabIndex = 0
'
'Panel2
'
Me.Panel2.BackColor = System.Drawing.Color.Transparent
Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel2.Dock = System.Windows.Forms.DockStyle.Right
Me.Panel2.Location = New System.Drawing.Point(290, 10)
Me.Panel2.Name = "Panel2"
Me.Panel2.Size = New System.Drawing.Size(272, 266)
Me.Panel2.TabIndex = 0
'
'TransPanelForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(572, 286)
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.Panel1)
Me.DockPadding.All = 10
Me.Name = "TransPanelForm"
Me.Text = "TransPanelForm"
Me.ResumeLayout(False)
End Sub
#End Region
''' <summary>The current rectangle that is dragged.</summary>
Private DragRect As Rectangle
''' <summary>A collection of rectangles.</summary>
Private FinalRects As New ArrayList
''' <summary>The position at which the mouse was pressed.</summary>
Private MouseDownPos As Point
''' <summary>Is the left mouse button down?</summary>
Private LeftMouseDown As Boolean
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(e)
Dim rect As Rectangle = Me.ClientRectangle
Dim brush As New LinearGradientBrush(rect, Color.White, Color.LightSteelBlue, LinearGradientMode.ForwardDiagonal)
e.Graphics.FillRectangle(brush, rect)
brush.Dispose()
End Sub
Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
MyBase.OnSizeChanged(e)
Me.Invalidate()
End Sub
''' <summary>Calculates the DragRect.</summary>
Private Function CalcDragRect(ByVal mouseX As Integer, ByVal mouseY As Integer) As Rectangle
Return Rectangle.FromLTRB(Math.Min(mouseX, MouseDownPos.X), _
Math.Min(mouseY, MouseDownPos.Y), _
Math.Max(mouseX, MouseDownPos.X), _
Math.Max(mouseY, MouseDownPos.Y))
End Function
#Region " Panel1_Mouse... "
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
If e.Button = MouseButtons.Left Then
MouseDownPos = New Point(e.X, e.Y)
LeftMouseDown = True
DragRect = New Rectangle(e.X, e.Y, 0, 0)
Panel2.Invalidate()
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
If LeftMouseDown Then
LeftMouseDown = False
DragRect = Me.CalcDragRect(e.X, e.Y)
Panel2.Invalidate()
FinalRects.Add(DragRect)
End If
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If e.Button = MouseButtons.Left Then
DragRect = Me.CalcDragRect(e.X, e.Y)
Panel1.Invalidate()
End If
End Sub
#End Region
''' <summary>Draws a rectangle.</summary>
''' <remarks>Instead of just doing 'g.DrawRectangle(pen, rect)',
''' this method draws a line if the width and/or height
''' of the rectangle is zero. This way a line is drawn,
''' instead of just nothing.</remarks>
Public Shared Sub DrawRect(ByVal g As Graphics, ByVal rect As Rectangle, ByVal pen As Pen)
If (rect.Width = 0) AndAlso (rect.Height = 0) Then
g.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Y + 0.01!)
ElseIf (rect.Width > 0) AndAlso (rect.Height = 0) Then
g.DrawLine(pen, rect.X, rect.Y, rect.Right, rect.Y)
ElseIf (rect.Width = 0) AndAlso (rect.Height > 0) Then
g.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Bottom)
Else '(rect.Width > 0) AndAlso (rect.Height > 0)
g.DrawRectangle(pen, rect)
End If
End Sub
Private Sub Panel2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint
For Each rect As Rectangle In FinalRects
DrawRect(e.Graphics, rect, Pens.Red)
Next
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
If LeftMouseDown Then
Dim pen As New Pen(Color.Blue)
pen.DashStyle = DashStyle.Dash
DrawRect(e.Graphics, DragRect, pen)
pen.Dispose()
End If
End Sub
End Class
|