Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake a form stick to the edge of the screen
Keywordsform, screen, edge, subclassing, stick
CategoriesTips and Tricks, Controls
 
User the SysInfo control to tell where the work area is. Position the form across the top of the work area.

To prevent the form from moving, subclass.

 
Private Sub Form_Load()
    ' Position the form.
    PositionForm

    ' Install the new WindowProc.
    OldWindowProc = SetWindowLong( _
        hwnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub

' Put the form along the right edge of the work area.
Private Sub PositionForm()
    g_DesiredX = SysInfo1.WorkAreaLeft
    g_DesiredY = SysInfo1.WorkAreaTop
    g_DesiredWidth = SysInfo1.WorkAreaWidth

    g_DesiredXPix = ScaleX(g_DesiredX, vbTwips, vbPixels)
    g_DesiredYPix = ScaleX(g_DesiredY, vbTwips, vbPixels)
    g_DesiredWidthPix = ScaleX(g_DesiredWidth, vbTwips, _
        vbPixels)

    Move g_DesiredX, g_DesiredY, g_DesiredWidth
End Sub
 
The new WindowProc routine resets the form's size and position.
 
Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, lParam As WINDOWPOS) As _
    Long
Dim new_aspect As Single

    ' Keep the aspect ratio.
    If msg = WM_WINDOWPOSCHANGING Then
        ' Reset the position.
        lParam.x = g_DesiredXPix
        lParam.y = g_DesiredYPix

        ' Reset the width.
        lParam.cx = g_DesiredWidthPix
    End If

    ' Continue normal processing. VERY IMPORTANT!
    NewWindowProc = CallWindowProc( _
        OldWindowProc, hwnd, msg, wParam, _
        lParam)
End Function
 
When the SysInfo control receives the SettingChanged event, some setting changed. It might have been the location of the task bar so reposition the form.
 
' Some setting changed, possibly the location of the
' task bar. Reposition the form.
Private Sub SysInfo1_SettingChanged(ByVal Item As Integer)
    PositionForm
End Sub
 
Note: I have been told this does not work properly in Windows 2000. The SysInfo control seems to ignore the task bar when calculating the work area. For an example that doesn't use the SysInfo control, see Center a form taking the taskbar into account.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated