I created a custom control called "DragPoint". It's a general-purpose moving/resizing tool. But whenever I move it, it disappears after I let go.
VB.NET:
Imports System.Windows.Forms
Imports System.Drawing
Public Class DragPoint : Inherits Control
Dim mouseOffset As Point
Private DPS As DrawShape
Private BGC As Color = Color.DodgerBlue
Private BRC As Color = Color.Black
Private BRW As Integer = 1
Enum DrawShape
Ellipse
Rectangle
End Enum
Property Shape() As DrawShape
Get
Shape = DPS
End Get
Set(ByVal value As DrawShape)
DPS = value
Me.Refresh()
End Set
End Property
Property BackgroundColor() As Color
Get
BackgroundColor = BGC
End Get
Set(ByVal value As Color)
BGC = value
Me.Refresh()
End Set
End Property
Property BorderColor() As Color
Get
BorderColor = BRC
End Get
Set(ByVal value As Color)
BRC = value
Me.Refresh()
End Set
End Property
Property BorderWidth() As Integer
Get
BorderWidth = BRW
End Get
Set(ByVal value As Integer)
BRW = value
Me.Refresh()
End Set
End Property
Private Sub DragPoint_CursorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.CursorChanged
If Cursor = Cursors.Default Then
Cursor = Cursors.SizeAll
End If
End Sub
Private Sub DragPoint_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Cursor = Cursors.Default Then
Cursor = Cursors.SizeAll
End If
Dim graphics As Graphics = e.Graphics
Dim penWidth As Integer = BRW
Dim pen As Pen = New Pen(BRC, BRW)
If DPS = DrawShape.Ellipse Then
Dim brush As SolidBrush = New SolidBrush(BackgroundColor)
graphics.FillEllipse(brush, 0, 0, Width, Height)
graphics.DrawEllipse(pen, CInt(penWidth / 2), _
CInt(penWidth / 2), Width - penWidth, Height - penWidth)
ElseIf DPS = DrawShape.Rectangle Then
Dim brush As SolidBrush = New SolidBrush(BackgroundColor)
graphics.FillRectangle(brush, 0, 0, Width, Height)
graphics.DrawRectangle(pen, CInt(penWidth / 2), _
CInt(penWidth / 2), Width - penWidth, Height - penWidth)
End If
'Dim textBrush As SolidBrush = New SolidBrush(Color.Black)
'Dim fontHeight As Integer = 10
'Dim font As Font = New Font("Arial", fontHeight)
'graphics.DrawString(Text, font, textBrush, penWidth, _
' Height / 2 - fontHeight)
End Sub
Private Sub DragPoint_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
If Me.Width > Me.Height Then
Me.Height = Me.Width
ElseIf Me.Height > Me.Width Then
Me.Width = Me.Height
End If
End Sub
Private Sub DragPoint_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
mouseOffset = New Point(-e.X, -e.Y)
End Sub
Private Sub DragPoint_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim mousePos As Point = Control.MousePosition
mousePos.Offset(mouseOffset.X, mouseOffset.Y)
Location = mousePos
End If
End Sub
Private Sub InitializeComponent()
Me.SuspendLayout()
Me.ResumeLayout(False)
End Sub
End Class
Last edited: