Visual Basic.NET
  Home arrow Visual Basic.NET arrow Focusing on Forms and Menus in Visual Basi...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Focusing on Forms and Menus in Visual Basic
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2007-03-01

    Table of Contents:
  • Focusing on Forms and Menus in Visual Basic
  • 4.16 Creating a Fading Form
  • 4.17 Creating a Nonrectangular Form
  • 4.18 Changing Menus at Runtime
  • 4.19 Creating Shortcut Menus

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Visual Basic.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Visual Basic 2005 Cookbook," published by...
     

    Buy this book now. 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.

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek