How do you size your form in vb6 so that form lower border is at top of taskbar
Is there a reason you cannot just maximise the form? That would be my first impression.
If that's not a runner, you could try getting the taskbar height in the following way:
Private Const ABM_GETTASKBARPOS = &H5
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type APPBARDATA
cbSize As Long
hwnd As Long
uCallbackMessage As Long
uEdge As Long
rc As RECT
lParam As Long
End Type
Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
Function GetTaskBarSize()
Dim ABD As APPBARDATA
SHAppBarMessage ABM_GETTASKBARPOS, ABD
MsgBox "Width:" & ABD.rc.Right - ABD.rc.Left
MsgBox " Height:" & ABD.rc.Bottom - ABD.rc.Top
End Sub
and then setting your form's height to the screen's height less the taskbar's height.
Minus only the taskbar? That might not really be what you want. There can be other windows on the edges of the screen that are meant to "carve out" regions of the desktop. Also, note that sometimes the height of the taskbar is irrelevant, such as when it's docked to the left or right side of the screen.
Galwegian has shown how to get the height of the taskbar, but if you're really looking for the usable area of the desktop, use the SystemParametersInfo
function with the spi_GetWorkArea
flag instead. It will tell you the area of the desktop excluding all desktop toolbars. MSDN advises that if you're interested in the space available on something other than the primary monitor, you should call GetMonitorInfo
instead; it fills a record, and one of the fields is for the monitor's work area.
I'm going to second the idea that you might really just want to maximize your window. If you've already done that, and you want to know how much space you're taking up, then get the current size of your window, and then subtract the dimensions of your window's frame (which get "tucked under the edges" of the desktop when a window is maximized). You can use GetSystemMetrics
with the sm_CXFrame
and sm_CYFrame
flags for that.
I'm going to agree you probably want to maximize your window.
But if you really do want to know the area of the desktop excluding all desktop toolbars (taskbar, Microsoft Office toolbar, etc), here's some VB6 declarations for the SystemParametersInfo call and a sample function that centres forms on the screen, allowing for the toolbars. This is borrowed from 101 tech tips (PDF) from the old Visual Basic Programmers Journal.
Private Const SPI_GETWORKAREA = 48
Private Declare Function SystemParametersInfo& Lib "User32" Alias "SystemParametersInfoA" ( _
ByVal uAction As Long, _
ByVal uParam As Long, lpvParam As Any, _
ByVal fuWinIni As Long)
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Function CenterForm32 (frm As Form)
Dim ScreenWidth&, ScreenHeight&, ScreenLeft&, ScreenTop&
Dim DesktopArea As RECT
Call SystemParametersInfo (SPI_GETWORKAREA, 0, DesktopArea, 0)
ScreenHeight = (DesktopArea.Bottom - DesktopArea.Top) * Screen.TwipsPerPixelY
ScreenWidth = (DesktopArea.Right - DesktopArea.Left) * Screen.TwipsPerPixelX
ScreenLeft = DesktopArea.Left * Screen.TwipsPerPixelX
ScreenTop = DesktopArea.Top * Screen.TwipsPerPixelY
frm.Move (ScreenWidth - frm.Width) \ 2 + ScreenLeft, _
(ScreenHeight - frm.Height) \ 2 + ScreenTop
End Function