Inside Visual Basic

July 1999

Resting Forms on the Taskbar

by Deborah L. Cooper

By default, Windows displays its taskbar on the bottom of your screen. You can set the taskbar's Always On Top and Auto Hide properties so the taskbar pops into view when you move the mouse pointer to the bottom of the screen. Alternately, you can instruct Windows to permanently display the taskbar on the screen at all times by deselecting the Auto Hide property. However, with the taskbar always visible on the screen, information is sometimes obscured--especially if a user maximizes an application's window. You can use the SystemParametersInfo function in your Visual Basic application to force the system to display your form so it rests on the taskbar. That way, you don't have to worry if the user maximizes your application's window with his taskbar visible all the time.

Sample project

To see how this works, create a new project. On the default form that appears, add a Command Button control. Next, add the code from Listing A to the Click event for the Command Button. Finally, create a new module and add the code from Listing B to the module.

Listing A: Code added to Click event for the Command Button


Private Sub Command1_Click()
	Dim RC As RECT
	Dim X As Long
	X = SystemParametersInfo _
		(SPI_GETWORKAREA, vbNull, RC, 0)
	Me.Move RC.Left * _
		Screen.TwipsPerPixelX, RC.Top * _
		Screen.TwipsPerPixelY, RC.Right * _
		Screen.TwipsPerPixelX, RC.Bottom * _
		Screen.TwipsPerPixelY
End Sub

Listing B: Code added to the new module


Public Declare Function _
		SystemParametersInfo Lib "user32" _
		Alias "SystemParametersInfoA" _
		(ByVal uAction As Long, ByVal _
		uParam As Long, lpvParam As Any, _
		ByVal fuWinini As Long) As Long
Public Const SPI_GETWORKAREA = 48
Type RECT
	Left As Long
	Top As Long
	Right As Long
	Bottom As Long
End Type

Before you run this demonstration program, make sure your taskbar is visible on your screen. If the taskbar isn't visible, select Start Settings Taskbar and clear the Auto Hide check box.

To run the demonstration program, press [F5]. The form now appears on the screen. Then, click the command button. The form is maximized and remains above the taskbar and isn't obscured by the taskbar at all.

The SystemParametersInfo function

Resting a form on the taskbar is accomplished by using one single function--SystemParametersInfo. The SystemParametersInfo function lets you retrieve (or set) a large number of system settings under Windows. Since the number of system settings in Windows is so large, we'll only discuss the one we're interested in--the work area of the screen. To retrieve the work area of the screen, call the SystemParametesInfo function with the SPI_GETWORKAREA parameter. This parameter tells the SystemParametersInfo function to retrieve the coordinates of the available area on the screen. The coordinates are returned in a RECT structure.

Once you've retrieved the screen's work area in the RECT structure, you can use Visual Basic's Move function to move the form's top, bottom, left, and right borders to the size of the coordinates stored in the RECT structure. This effectively maximizes the form to the size of the screen's work area.

Conclusion

In the demonstration program, the code to rest the form on the taskbar is placed in the Click event of a Command Button control. Ideally, you would place this code in the form's Form_Load event so it's automatically executed when your form loads. Now your applications can make the most of the available screen space, regardless of the user's taskbar preferences.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.