Focusing on Forms and Menus in Visual Basic
(Page 1 of 5 )
In this final article in a four-part series, we take a closer look at what you can do with forms and menus. It is excerpted from chapter four of the
Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
4.14 Centering a Form
Problem
You want a form to be in the center of the display when it first appears.
Solution
Set the form’s StartPosition property to CenterScreen.
Discussion
That was easy, but there may be cases where you need to set this property to Manual, but you still want the form to appear centered sometimes. To accomplish this, add the following code to the Loadevent handler for your form:
Me.Location = New Point((_
Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2, _
(Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2)
4.15 Creating and Moving a Borderless Form
Problem
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.
Solution
Sample code folder: Chapter 04\MoveBorderlessForm
Turning off the border elements is easy:set the form’sFormBorderStyleproperty 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: aButtoncontrol namedActCloseand aPictureBox control namedDragBar. Change the button’sTextproperty toClose. Change the picture box’sBackColorproperty toActiveCaption, one of the system colors. Also, change the form’sFormBorderStyle property toNone. 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.
Discussion
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), aWM_LBUTTONDOWNmessage is passed to the relevant Windows procedure. That message ultimately triggers a call to one of your form’sMouseDownevent handlers.
Your application includes a “message pump” that makes calls to each form’sWndProcprocedure 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 theDragBar picture box control receives the mouse down event, it says, “Hey, I’ll just send a fake message to my window’sWndProcroutine so that it thinks the user clicked on the titlebar.” And that’s what the code does. It sends aWM_NCLBUTTONDOWNmessage 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.
Next: 4.16 Creating a Fading Form >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Check it out today at your favorite bookstore. Buy this book now.
|
|