Forms and Windows VB Help

This section describes Win32 API functions that you can use to extend the usual Visual Basic functions to enhance your Forms, windows; and menus. Using these functions, you can create menus that incorporate bitmaps and dynamically change menu items: You will also learn how to track the mouse and manipulate windows of other active applications on the Desktop.

Creating Menus with Bitmaps

The menus you create with Visual Basic’s Menu Editor can contain text only, Some applications, however, can display images in their menus. It’s possible to add bitmaps to your menus by calling several Win32 API functions..The processes time consuming, but well worth it. The Menu BMP application (see Figure 13.4)creates a menu with bitmaps at runtime.



… . .
VB6 at Work:, The MenuBMP Project

This application uses the following functions:

  1. GetMenu()
  2. GetSubMenu()
  3. ModifYMenu()
  4. CreateCompatibleDC()
  5. CreateCompatibleBihnap()
  6. SelectObject()

To manipulate menus with API functions, you must. obtain the menu’s handle . Menus are objects; and as such, they have a handle that identifies them to the operating system just as Forms and bitmaps do). In effect, you can manipulate the menus of other applications that are running at the time by obtaining the handle of their menu.

To obtain a menu’s handle, use the GetMenu() function, which is declared as follows:

Private Declare Function GetMenu lib’ ‘user32’ (ByVal hWnd As long) As Long

The hWnd argument is the handle of the Form whose menu you want to manipulate. To obtain the handle of the current Form’s menu, use the following expression:

MenuHandle = GetMenu(Me.hWnd)

First-level menus contain submenus that are identified by their order in the main  menu. Submenus also have handles, which can be obtained with the GelSubMenu() function:

Private Declare Function GetSubMenu Lib “user32”
(ByVal hMenu As Long, ByVal nPos As Long) As Long

The hMenu argument is the menu handle returned by the GetMenu() function, and nPos is the-position of the submenu. If nPos is 0, the GetSubMenu() function returns the handle of the first submenu, which quite often, is the File menu.

After you retrieve the handle of a submenu, you can modify it with the Modify- Menu() function, which is declared as follows:

This function. can change a menu entry, and it requires some explanation. The hMenu argument is the handle of the submenu retrieved by the GetSubMenu() function. The nPosition argument identifies the menu item to be changed. If the MF_BYCOMMAND flag in the wFlags argument is set, this argument refers to the ID of the command to be changed. If the MF_BYPOSITION flag is set, this argument specifies the position of the item in the submenu. The wFlags argument is-a combination of flags, described in Table 13.4.


To assign a bitmap to a menu item, you set the MF_BITMAP flag in the wFlags argument of the ModifyMenu() function. Modifying the menu Is relatively simple, but specifying the bitmap to be assigned in the place of the item’s caption isn’t. Firsf, you must create a device context (a memory area where a.bitmap can be stored) with the CreateComptltibIeDC() function. The bitmap comes from an image (a BMP file) that is first loaded onto a bidden PictureBox control. The bitmap on the hidden PictureBoxcontrol is copied to the device context with the CreateCompatibleBitmap() function. When. the bitmap is selected into the device context, you can assign it to a menu item, as if it was a string. Code 13.5 shows, how the bitmaps are added 10 the menu of the MenuBMP application.

Notice that the bitmap’s dimensions must be passed as arguments to the Create CompatibleBitmap() function. When you design the bitmap with an image processing application, you must size the bitmap so that it will fit nicely in the menu. Don’t make the bitmaps too Long or too narrow. The bitmaps.used in this section’s examples were designed with the Paint Shop: Pro application.You can use any image processing application to design bitmaps for your menus, including. ImageEdit, which can be found in the Tools folder on the Visual BasicCD.

The Bitmaps menu of.the MenuBMP application contains three commands, which correspond to three different fonts, as you can see in Figure 13.4. The three bitmap’s are stored in the Verdana.bmp, Serif.bmp, and Comic.bmp files, in the same folder as the application. After the bitmaps have been designed, you can start coding the application. The MenuBMP application’s complete listing is shown next.

The MenuBMP Application
Code 13.5:

Posted on November 6, 2015 in The Windows API

Share the Story

Back to Top
Share This