Hide the TitleBar, Change Border Style
and other read only form properties at run time

Often times it would be convenient to change the style of your form at runtime. Suppose you want to remove the TitleBar or change the border style to prevent users from resizing your form. Unfortunately, VB only allows you to do this at design time. However, your form is just a window and the characteristics of a window are dictated by attributes called style and extended style bits.

This program shows how to set those style bits so that you can remove or add the titlebar at run time, change it to a smaller toolwindow type titlebar and toggle the border style between sizable and not sizable.

Also, see how to programmatically hide or display the minimize, maximize and WhatsThisHelp buttons. If you remove the titlebar, you may still want a means to move the form and display the popup system menu that contains the move, size, minimize, maximize and close options. This sample explains how to accomplish all this.

All this functionality is contained in a class module that you can easily incorporate into your project.

Change read only form properties at run time.
Download Source Code

Discussion

To toggle the TitleBar or Caption, the Minimize and Maximize buttons, and change the form's border style to prevent resizing, you need to set the form's style bits. A call to the GetWindowLong API, passing in your form's handle and the GWL_STYLE flag retrieves the existing style value. This value is a long integer where each bit represents a different style property.

This value is modified by OR-ing in the new style (WS_CAPTION, WS_MINIMZEBOX, WS_MAXIMIZEBOX, WS_THICKFRAME and WS_SYSMENU respectively). Next a call to SetWindowLong sets the new style. Once set, the form needs to be redrawn to put the changes into effect. This is accomplished with a call to the SetWindowPos API.

The menu that appears when you click the icon displayed in the upper left side of a window's caption is called the System Menu. You can hide or display this menu in the exact same manner just described except you use the WS_SYSMENU constant. You can even change the options on that menu. To do so, see my Add Bitmaps to Your Menus and Remove/Disable the Close Button page.

To change the form's caption to a ToolWindow caption, which is not quite as tall as a normal titlebar and which uses a smaller font, or to hide/display the WhatsThisHelp button you need to set the form's extended style attributes. This is identical to setting the style bits except that you pass in the GWL_EXSTYLE flag to SetWindowLong.

Once you hide the system menu, you may want to display it when the user right clicks the form. This can be achieved by calling GetCursorPos to get the coordinates of where the user clicked. These coordinates are sent to your form via a call to the SendMessage API along with the WM_GETSYSMENU value. Upon receiving this message, the form will popup the system menu at the specified location.

Instructions

Download the source code and run it. Check the various options to hide, display and change the titlebar. When the titlebar is displayed, try hiding the minimize, maximize and WhatsThisHelp buttons.

When the titlebar is hidden, check the Move Form button then click anywhere on the form and drag it to a new locatoin. Then right click the form to popup the system menu.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page