Forms, Windows and Controls



How to move a control on a form at runtime.
How to center a form on a parent window.
How to make a form stay 'on top' of all other windows 32bit.
How to make a form stay 'on top' of all other windows 16bit.
How to center a form on the screen.
How to center a form within another form.
How to hide and show a specific window or icon.
How to hide, show, minimize MDI child forms.
How to move a form with no title bar.
How to position a shelled window.


 

'-------------------------------------------------------------------                                    
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to make a form stay 'on top' of all other windows..
'Note: This has only been tested with VB 3 and VB 4-16, if you convert
'this for use with other versions please let me know.-Burt Abreu
'-------------------------------------------------------------------

' Declare this API and these Constants:

Declare Function SetWindowPos% Lib "user" (ByVal h%, ByVal hb%, ByVal x%, 
ByVal y%, ByVal cx%, ByVal cy%, ByVal f%)

Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2

' Add the following code to the appropriate event:

' Set TopMost
Dim success%
success%  = SetWindowPos%(Form1.hWnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)

' Remove TopMost
Dim success%
success% = SetWindowPos%(Form1.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,Flags)



'-------------------------------------------------------------------
'Author: Dan Pupius
'email : DanP600@aol.com
'web   : http://users.aol.com/danp600
'Posted:11/15/97
'
'How to make a form stay 'on top' of all other windows..
'This version has been tested with VB4-32
'-------------------------------------------------------------------
'Here's a cool little API function for VB4 on Win 32s.
'It basically allows a window to "Always be on top"

This goes in the declarations section of a module

Global Const SWP_NOMOVE = 2
Global Const SWP_NOSIZE = 1
Global Const HWND_TOPMOST = -1
Global Const HWND_NOTOPMOST = -2
Global Const FLOAT = 1, SINK = 0

Public Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal
hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long,
ByVal cy As Long, ByVal wFlags As Long)

This is a sub within the module

Sub FloatWindow(X As Integer, action As Integer)

' When called by a form:
'
' If action <> 0 makes the form float (always on top)
' If action = 0 "unfloats" the window.
'

Dim wFlags As Integer, result As Integer
wFlags = SWP_NOMOVE Or SWP_NOSIZE

If action <> 0 Then     ' Float
    Call SetWindowPos(X, HWND_TOPMOST, 0, 0, 0, 0, wFlags)
Else                    ' Sink
    Call SetWindowPos(X, HWND_NOTOPMOST, 0, 0, 0, 0, wFlags)
End If

End Sub

Then to call the procedure you use this code

Dim f as integer
f = Screen.ActiveForm.hWnd
Call FloatWindow(f, FLOAT)

to "float" the form, ie.  allways ontop
or

Dim f as integer
f = Screen.ActiveForm.hWnd
Call FloatWindow(f, SINK)

to sink.

Alternatively instead of Screen.ActiveForm.hWnd you can specify a window, ie
Me.hWnd to float/sink the current form.




'-------------------------------------------------------------------
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to center a form on the screen.
'-------------------------------------------------------------------
' Center the desired form on the screen

Sub CenterFrm (aForm As Form)
Dim x, y
        
        x = (Screen.Width - aForm.Width) / 2
        y = (Screen.Height - aForm.Height) / 2

        aForm.Move x, y

End Sub

' Example: 

        CenterFrm Form1


'-------------------------------------------------------------------
'Author: Joe Markowski
'E-Mail: jsmarko@mail.eclipse.net
'Posted:02/24/98
'
'How to center a form within another form.
'-------------------------------------------------------------------
'centers the form in App rename frmMain to the name of your Main Form. 
Public Sub CenterForm(frmTarget As Form)

    frmTarget.Move (frmMain.Left + (frmMain.Width - frmTarget.Width) / 2), _
    (frmMain.Top + (frmMain.Height - frmTarget.Height) / 2)

End Sub

To use it just call the sub
 
CenterForm Me


 

'-------------------------------------------------------------------
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to hide and show a specific window or icon. 
'Note: This has only been tested with VB 3 and VB 4-16, if you convert
'this for use with other versions please let me know.-Burt Abreu
'-------------------------------------------------------------------
' Use these 2 API's and the code below to hide/show a window
' or icon. Works nicely to hide the Graphics Server icon, for example.

' Declare these API's and Constants:

Declare Function FindWindow% Lib "User" (ByVal lpClassName As Any, 
ByVal lpWindowName As Any)
Declare Function ShowWindow% Lib "User" (ByVal hWnd%, ByVal nCmdShow%)

Const SW_HIDE = 0
Const SW_SHOW = 5

' Put the code below in the appropriate event:
' Form_Load or Activate most likely.

Form_Load()
' This example hides the "Graphics Server" icon
Dim Handle As Integer
Dim WindowName As String

        ' Set the name of the window
        WindowName = "Graphics Server"

        ' Find that window
        Handle = FindWindow(0&, WindowName)

        ' Change last param to SW_SHOW to show the icon again
        X% = ShowWindow(Handle, SW_HIDE)

End Sub



'-------------------------------------------------------------------
 'Author: Gordon F. MacLeod
 'web   : www.cadvision.com
 'Posted:11/01/97
 '
 'How to hide, show, minimize MDI child forms. 
 'Note: This has only been tested with VB 3 and VB 4-16, if you convert
 'this for use with other versions please let me know.-Burt Abreu
'-------------------------------------------------------------------
' Here's how to hide/show/minimize/...etc. MDI children

' Declare API and Constants

Declare Function ShowWindow% Lib "User" (ByVal hWnd%, ByVal nCmdShow%)

Global Const SW_HIDE = 0
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWMAXIMIZED = 3
Global Const SW_SHOWNOACTIVE = 4
Global Const SW_SHOW = 5
Global Const SW_MINIMIZE = 6
Global Const SW_SHOWMINNOACTIVE = 7
Global Const SW_SHOWNA = 8
Global Const SW_RESTORE = 9

' Examples: 

Dim Re%

        ' Hide the child form
        Re% = ShowWindow(frmMDIChild.hWnd, SW_HIDE)

        ' Show the child form
        Re% = ShowWindow(frmMDIChild.hWnd, SW_SHOW)

        ' Minimize the child form
        Re% = ShowWindow(frmMDIChild.hWnd, SW_MINIMIZE)



'-------------------------------------------------------------------
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to move a form with no title bar. 
'Note: This has only been tested with VB 3 and VB 4-16, if you convert
'this for use with other versions please let me know.-Burt Abreu
'-------------------------------------------------------------------          
' Here's how to move a form that contains no title bar.

' Declare these API's and Constant

Declare Sub ReleaseCapture Lib "user" ()
Declare Function SendMessage& Lib "user" (ByVal hWnd%, ByVal wMsg%, ByVal wParam%, 
lParam As Any)

Const WM_NCLBUTTONDOWN = &HA1

' In the Form's MouseDown event add this code:

Dim r%

        If button = 1 Then
                ReleaseCapture
                r% = SendMessage(hWnd, WM_NCLBUTTONDOWN, 2, 0)
        End If



'-------------------------------------------------------------------
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to position a shelled window.
'Note: This has only been tested with VB 3 and VB 4-16, if you convert
'this for use with other versions please let me know.-Burt Abreu
'------------------------------------------------------------------- 
' Here's how to position a shelled window where ever
' you want it on the screen.

' Declare the 2 API's

Declare Function FindWindow% Lib "User" (ByVal lpClassName&, ByVal lpWindowName$)
Declare Sub SetWindowPos Lib "User" (ByVal hWnd%, ByVal hWndInsertAfter%, ByVal X%, 
ByVal Y%, ByVal cx%, ByVal cy%, ByVal wFlags%)

' Add this code to the appropriate event:

Dim r%
Dim myhWnd%

' Shell to executable and load the file

        r = Shell("Notepad c:\autoexec.bat", 1)

        DoEvents ' Let the app load completely

' Get the executable window handle based on window's title

    myhWnd = FindWindow(0, "Notepad - AUTOEXEC.BAT")

' Move the window to the desired location
' Parms 3 and 4 are top and left, 
' 5 and 6 are window height and width
 
    SetWindowPos myhWnd, -1, 10, 10, 200, 300, SWP_SHOWWINDOW


'-------------------------------------------------------------------
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to move a control on a form at runtime.
'Note: This has only been tested with VB 3 and VB 4-16, if you convert
'this for use with other versions please let me know.-Burt Abreu
'-------------------------------------------------------------------

' Here's how to move a control around on a form at runtime:

' Declare these APIs and Constants

Declare Sub ReleaseCapture Lib "user" ()
Declare Function SendMessage& Lib "user" (ByVal hWnd%, ByVal wMsg%, ByVal wParam%, 
lParam As Any)

Const WM_SYSCOMMAND = &H112
Const SC_MOVE = &HF012

' In this example we'll use a picture box, in the MouseDown event add:

Sub Picture1_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim ret%
If Button = 1 Then
   ReleaseCapture
   ret = SendMessage(Picture1.hWnd, WM_SYSCOMMAND, SC_MOVE, 0)
End If

End Sub



'-------------------------------------------------------------------
'Author: Gordon F. MacLeod
'web   : www.cadvision.com
'Posted:11/01/97
'
'How to center a form on a parent window.
'Note: This has only been tested with VB 3 and VB 4-16, if you convert
'this for use with other versions please let me know.-Burt Abreu
'-------------------------------------------------------------------

' This procedure centers a Form over a Parent window.
' This dialog-centering algorithm works well when you're
' not sure of the resolution of the target computer.

' Declare this Type in a .Bas module:

Type RECT
   Left As Integer
   Top As Integer
   Right As Integer
   Bottom As Integer
End Type

' Declare this API
 Declare Sub GetWindowRect Lib "User" (ByVal hWnd%, lpRect As RECT)

' Add the DialogCenterParent rountine:

Sub DialogCenterParent (ByVal hWndParent As Integer, frmDialog As Form)
Dim iLeft As Integer
Dim iTop As Integer
Dim iMidX As Integer
Dim iMidY As Integer
Dim rcParent As RECT

' Find the ideal center point.
If hWndParent = 0 Then
   'No parent, so center over the enter screen
   iMidX = Screen.Width / 2
   iMidY = Screen.Height / 2
Else

'Center over the form's parent.

Call GetWindowRect(hWndParent, rcParent)
     ' Next 2 lines as one single line.
     iMidX = ((rcParent.Left * Screen.TwipsPerPixelX) + 
              (rcParent.Right * Screen.TwipsPerPixelY)) / 2
     ' Next 2 lines as one single line.
     iMidY = ((rcParent.Top * Screen.TwipsPerPixelY) + 
              (rcParent.Bottom * Screen.TwipsPerPixelY)) / 2
End If

' Find the form's upper left based on that

iLeft = iMidX - (frmDialog.Width / 2)
iTop = iMidY - (frmDialog.Height / 2)

' If the form is outside the screen, move it inside

If iLeft < 0 Then
   iLeft = 0
   ElseIf (iLeft + frmDialog.Width) > Screen.Width Then
   iLeft = Screen.Width - frmDialog.Width
End If

If iTop < 0 Then
   iTop = 0
   ElseIf (iTop + frmDialog.Height) > Screen.Height Then
   iTop = Screen.Height - frmDialog.Height
End If

' Move the form to it's new position
frmDialog.Move iLeft, iTop

End Sub

' Call the routine in the Form_Load event of the form to center:

' Substitute your Parent for Form1
  Call DialogCenterParent(Form1.hWnd, Me) 

HP Projectors | Key Man Life Insurance | Benefits of VOIP | Web Site Revenue | Necklace