Tag Archives: Form

How to Create Custom/Abnormal Shaped Forms in VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)

  A earlier article showed how to change the shape of a form in .net based VBs. Unfortunately VB 6.0 (Classic Versions, Pre DotNet versions) does not have native support for Region based changes in the Form. Thankfully Microsoft added some API calls in Windows 98 and above that you can use to create custom/abnormal form shapes/regions. This article will show how to create a Elliptic Form Shape. But I will include the APIs to create the basic shapes below…

  • Elliptic Shaped
  • Round Shaped
  • Polygon Shaped

Below are the API Codes required to get this done.




    Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

    Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

    Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

    Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

    Private Declare Function CreatePolygonRgn Lib "gdi32" (ByVal lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long



Now you simply need to make a variable to recieve the handle to the newly setup region…

        'Will hold the handle to the new region.
        Dim hRegion As Long
        'Below is the code to create a basic elliptic region…
        'Create an Elliptic Shaped Region.

        hRegion = CreateEllipticRgn(0, 0, 100, 200)


Now that a new region has been created, simply call the SetWindowRgn Function passing the handle to the Form you want to change the shape in the first parameter. The other parameters are for setting up the elliptic shape on how your want it to look.


        'Change the Form to the new region shape.
        SetWindowRgn(frmRegion.hWnd, hRegion, True)




  Thats all there is to it! Easy to use APIs makes it possible to do custom form regions using the WindowRgn based functions that are available in Windows 98 and Above. As usual I do have a example for Visual Basic 6.0 at my http://www.vbcodesource.com website. Have fun!


How to Move or Drag a Form without a Titlebar using VB 6.0 and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

There is more than one way to move a form without a title bar. For instance using API’s and Subclassing. But you can also use regular code and simply track the position of the mouse at certain events and can thus move the form without Subclassing. The regular code way is what I actually prefer and will show in this article.

The basics of the source code is keeping track of the mouse location when the form’s  _MouseDown event is fired and in the form’s _MouseMove event while the left Mouse-Button is still active. The first sets of codes will be for Visual Basic .NET including Visual Basic 2005/08 and Newer. Visual Basic 6.0 codes are further below.

Visual Basic.NET, VB 2008, VB.NET 2010, and Higher…

Put these in your programs Declaration area.

    'These should go into your projects Declare area.
    Dim moveStartX As Integer

    Dim moveStartY As Integer

    Dim moveEndX As Integer

    Dim moveEndY As Integer

The variables above will hold the mouse starting position for when the left button is pressed and the ending position for when the mouse is moved.

Next, you want to go to your Forms_MouseDown() event…

    Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
      Handles Me.MouseDown
        'Keep track of the mouse starting position when the mouse button is pressed.
        moveStartX = e.X

        moveStartY = e.Y

    End Sub

The final set of codes needs to go into your Forms_MouseMove() event…

    Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
      Handles Me.MouseMove
        'Calculate the mouse positions in relation to its starting position.
        moveEndX = e.X - moveStartX

        moveEndY = e.Y - moveStartY
        'Make sure the Left Mouse Button is pressed and then move the form to its new location.
        If e.Button = Windows.Forms.MouseButtons.Left Then

            Me.Location = New Point(moveEndX + Me.Location.X, moveEndY + Me.Location.Y)

        End If

    End Sub

So, all of the VB.NET codes above will move your Form pretty much the same way as if you had moved the form with the Titlebar..

Visual Basic 6.0

You can put these in the Declaration area.

    'Declare the variables that will hold the mouse locations. This is almost exactly the same
    'as .net but the variables types needs to be changed from Integer to Long since the
    'Visual Basic 5.0/6.0 32Bit variable is Long and DotNets is Integer.
    Dim moveStartX As Long

    Dim moveStartY As Long

    Dim moveEndX As Long

    Dim moveEndY As Long

These go in the Forms_MouseDown event.

        'You want to put these codes in your Forms_MouseDown event. This will keep track of
        'where the mouse is positioned when the button is clicked.
        moveStartX = X

        moveStartY = Y

Then these go in the Forms_MouseMove Event.

        'Put the codes below in your Forms_MouseMove event…
        'Calculate the mouse position from its starting point to its current location.
        moveEndX = X - moveStartX

        moveEndY = Y - moveStartY

        If Button = 1 Then

            Me.Left = Me.Left + moveEndX

            Me.Top = Me.Top + moveEndY

        End If

That is all you have to do to get your form moveable if you have a non-title based form design. Have fun!


Minor Revision: 2015