Thursday, January 13, 2011

Thursday, 1/13/11

Public Class Form1
'This has been slightly adapted by me From Visual Basic Cookbook 2005
Dim WithEvents DragBar As New PictureBox
Const HT_Caption As Integer = &H2
Const WM_NCLBUTTONDOWN As Integer = &HA1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = 0
DragBar.BackColor = Color.Blue
DragBar.Location = New Point(50, 50)
DragBar.Width = (Me.Width * 0.9)

Me.Controls.Add(DragBar)
End Sub

Private Sub DragBar_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles DragBar.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Left) Then
'---- Don't hold on to the mouse locally
DragBar.Capture = False

'----Trick the form into thinking it received a title click
Me.WndProc(Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
CType(HT_Caption, IntPtr), IntPtr.Zero))
'All of the activity within a Windows form happens through messages being processed through a Windows procedure, or WndProc. This method is as old as windows
End If
End Sub
End Class

1 comment:

Brad Jensen said...

I don't understand why the picturebox has to be created in code, I assume this means you don't draw it on the form in the ide, but if that is so, where is the height property set?

I assume from googling that setting the capture property of the dragbar picturebox to false means that the mousedown event will fire over and over again, instead of just once until the mousebutton is released.

by sending a message to click on the titlebar, I think maybe what this code does is cause the entire form to be dragged when the user clicks on the dragbar and moves the mouse.