Okay, what I want to do is snap a movable panel to the form, like create a virtual grid kind of thing. The panels can be moved with the mouse and stop moving once the left mouse key is released.
For the movable panel I have used a Class, it creates an object called NewComp in the toolbox.
Any help would be greatly appreciated.
BTW: The snapping size can be anything, it is not important right now.
Thanks!
Public Class NewComp
Inherits Panel
Public Sub New()
Me.BackColor = Color.White
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.Height = 50
Me.Width = 50
BringToFront()
End Sub
Public Const WM_NCLBUTTONDOWN = &HA1
Private components As System.ComponentModel.IContainer
Public Const HTCAPTION = 2
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Capture = False
Dim msg As Message = _
Message.Create(Handle, WM_NCLBUTTONDOWN, _
New IntPtr(HTCAPTION), IntPtr.Zero)
Me.DefWndProc(msg)
ForeColor = Color.Gray
End If
End Sub
Private Sub TextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Left Then
Me.ForeColor = Color.DarkGray
End If
End Sub
Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
End Sub
' True while we are drawing the new line.
Private m_Drawing As Boolean
' Buffer for erasing rubberband lines.
Private m_BufferBitmap As Bitmap
Private m_BufferGraphics As Graphics
' The mouse position.
Private m_X1 As Integer
Private m_Y1 As Integer
Private m_X2 As Integer
Private m_Y2 As Integer
' Start drawing a rubberband line.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
' Do nothing if this isn'tthe left mouse button.
If e.Button <> MouseButtons.Left Then Exit Sub
m_Drawing = True
' Save a snapshot of the form.
'SaveSnapshot()
' Save the current mouse position.
m_X1 = e.X
m_X2 = e.X
m_Y1 = e.Y
m_Y2 = e.Y
End Sub
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'NewComp
'
Me.Size = New System.Drawing.Size(50, 50)
Me.ResumeLayout(False)
End Sub
Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
Me.Visible = False
End Sub
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Visible = False
End Sub
End Class