Gradient Color / Transparent Form Backgrounds
Add Bitmaps to Menus & Disable the Close Button

See how to make a form's background fade from a light color to darker one and how to make it transparent.

This program also shows how add bitmaps to your menus and disable the Close ("X"), Minimize and Maximize buttons as well as the Move and Size menu options in the caption bar.

Form with Gradient Background and pictures on the menu.
Gradient Background.
the dimmed "X" button and menu images

Transparent form over an Explorer window.

Transparent Form over an Explorer window.

Download Source Code

Gradient Background

Making the background of the form a gradient color is as simple as looping from the top of the form to the bottom and using its Line method and the RGB function to draw each line a different color.

Transparent Background

To clear the background of the form you need to set the Extended Style Bits of the form. This is accomplished by calling SetWindowLong with the GWL_EXSTYLE flag which denotes you are about to change the style. The extended style is then set to WS_EX_TRANSPARENT to clear the background.

Transparent forms are handy for splash screens or for creating hot spots over another form. Transparent forms should always be displayed modally since Windows does not repaint them. For another example of gradient backgrounds, see my Irregularly Shaped Forms program.

With Windows 2000 or newer, you can use the SetLayeredWindowAttributes to make windows with varying degrees of translucency, from invisible windows to fully opaque. Check it out.

Adding Bitmaps to Menu Items

You can add checked and unchecked bitmaps to your menu items by using the SetMenuItemBitmaps function. When you click the Add Images button, a call to GetMenu is made to return the handle to the form's menu object. Using that handle, the GetMenuItemCount function retrieves the number of top level menu items.

The program loops through the menu items and calls GetSubMenu to get the handle to each submenu. For each submenu the number of submenu items is determined by another call to the GetMenuItemCount API. A second loop is used to call SetMenuItemBitmaps for each submenu item.

SetMenuItemBitmaps takes the handle to the top level and submenu as parameters as well as the bitmaps to use for checked and unchecked menu elements. If either the checked bitmap or unchecked bitmap parameter is Null, the system displays nothing next to that menu item. If both parameters are Null, the system displays the default check-mark bitmap when the item is checked, and removes the bitmap when the item is not checked.

Here I have used 13x13 pixel bitmaps which is the proper size on my PC. What you need to do is use the GetSystemMetrics function with the SM_CXMENUCHECK and SM_CYMENUCHECK values to retrieve the bitmap dimensions. See my System Metrics Information program for details.

Removing System Menu Items

The menu that appears when you click the icon displayed in the left side of a window's caption is called the System Menu. You can disable any of the System Menu items on your form. Typically these items are: Restore, Move, Size, Minimize, Maximize, a separator bar, and Close.

To remove an option from the System Menu, first call the GetSystemMenu function to get a handle to the System Menu. Then call the RemoveMenu API with the MF_BYPOSITION flag and the ordinal of the menu item to delete. Menu item ordinal numbers are identified in the RemoveMenuItem enumeration. Removing the Close menu item also disables the Close or "X" button on the right hand side of the form's caption. Since menu items are referenced by ordinal position, you must remove them in descending order.

To remove the Minimize and Maximize buttons from a form's caption you need to set the style of the form. A call to GetWindowLong retrieves the existing style which is then modified to reflect the button to remove. Finally a call to SetWindowLong set the new style. This is similar to the approach used above to create a transparent form except here we are changing the form's style and not its extended style attributes.

Instructions

Download the source code and run it. Select a color then click Make Gradient to dither the form's background. Click Make Transparent to make a transparent form. The transparent background example works best when you compile the program and run the .Exe.




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