Recipe 4.15. Creating and Moving a Borderless Form


You want to display a form without any of the typical window border elements. Also, you want the user to be able to move the window around by clicking and dragging a PictureBox control.


Sample code folder: Chapter 04\MoveBorderlessForm

Turning off the border elements is easy: set the form's FormBorderStyle property to None. Then you can manage the drawing of the form elements yourself.

Creating a fake titlebar that moves the form is a little more involved. Create a new Windows Forms application, and add two controls: a Button control named ActClose and a PictureBox control named DragBar. Change the button's Text property to Close. Change the picture box's BackColor property to ActiveCaption, one of the system colors. Also, change the form's FormBorderStyle property to None. The form should look something like Figure 4-17.

Figure 4-17. A borderless form with a pretend titlebar

Now, add the following source code to the form's code template:

 Const HT_CAPTION As Integer = &H2 Const WM_NCLBUTTONDOWN As Integer = &HA1 Private Sub DragBar_MouseDown(ByVal sender As Object, _       ByVal e As System.Windows.Forms.MouseEventArgs) _       Handles DragBar.MouseDown   ' ----- When the user clicks the left mouse button, act   '       as if the user actually clicked on the form's   '       title bar.   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))   End If End Sub Private Sub ActClose_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles ActClose.Click    Me.Close( ) End Sub 

Run the program, and drag the colored picture box control to move the form around the display.


All of the activity within a Windows form happens through messages being processed through a Windows procedure, or WndProc. This method has existed since the introduction of Windows. The .NET Framework put a bunch of pretty classes around the messy parts of this messaging system, but it's still there, and you can interact with it to suit your needs.

Normally, when you left-click on a form window (or a control, which is just a different type of window), a WM_LBUTTONDOWN message is passed to the relevant Windows procedure. That message ultimately triggers a call to one of your form's MouseDown event handlers.

Your application includes a "message pump" that makes calls to each form's WndProc procedure for message processing. But there is nothing to stop you from calling that procedure yourself. In fact, it's exposed as a form class member.

When the DragBar picture box control receives the mouse down event, it says, "Hey, I'll just send a fake message to my window's WndProc routine so that it thinks the user clicked on the titlebar." And that's what the code does. It sends a WM_NCLBUTTONDOWN message to the form. The "NCL" part of that code means "Non-Client," the area that contains the titlebar and borders. The HT_CAPTION flag tells the message that the click occurred in the caption area (the titlebar). This is all that's needed to trick the form.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: