Create a bitmap menu

Source: Internet
Author: User
Create a bitmap menu

In a common program, menus always exist in the form of text, and sometimes seem very monotonous. If you can add bitmap images to the menu, it will greatly increase your interest. This article describes how to use bitmap to create menu options.

Create a bitmap menu

---- Creating a bitmap menu is actually very simple. It requires some menu functions and bitmap functions of the Windows application programming interface (API, you need to include the declaration of these functions in the standard module of your application. For details, see the sample program. The procedure is as follows:

  1. Use the getsubmenu function to extract the sub-menu item handle, and use the createcompatibledc function to create a compatible device environment description table;

  2. In a loop, you can use the createcompatiblebitmap function, SelectObject function, and bitblt function to select bitmap loaded for each menu item into the compatible device environment;

  3. Use the modifymenu function to plot the real bitmap menu options;

  4. Use the deletedc function to release the device environment so that other programs can use them.

---- There are multiple ways to extract bitmap. In this example, four graphical box controls are set on the form and they are used to load four preset icons as the source file of the menu option bitmap, of course, you can also use other methods, such as using the loadpicture function to load bitmaps from the disk.

Sample program

  1. Start a new project in Visual Basic and use the default method to create form1.

  2. Create a new module and use the default method to create module1.bas.

  3. Add the following declaration statements and constants to the module1.bas module:


    Option explicit
    Declare function getmenu lib "USER32" (byval hwnd as long) as long
    Declare function getsubmenu lib "USER32" (byval hmenu as long,
    Byval NPOs as long) as long
    Declare function getmenuitemid lib "USER32" (byval hmenu as long,
    Byval NPOs as long) as long
    Declare function modifymenu lib "USER32" alias "modifymenua"
    (Byval hmenu as long, byval nposition as long, byval wflags as long,
    Byval widnewitem as long, byval lpstring as any) as long
    Declare function createcompatibledc lib "GDI32" (byval HDC as long) as long
    Declare function deletedc lib "GDI32" (byval HDC as long) as long
    Declare function createcompatiblebitmap lib "GDI32"
    (Byval HDC as long, byval nwidth as long, byval nheight as long) as long
    Declare function SelectObject lib "GDI32" (byval HDC as long,
    Byval hobject as long) as long
    Declare function bitblt lib "GDI32" (byval hdestdc as long, byval X as long,
    Byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc
    As long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
    Public const srccopy = & hcc0020

    Public const mf_byposition = & H400 &
    Public const mf_bitmap = & H4 &


    Note that the preceding statement must be written in one line.

  4. Add four graphic box controls on form1, set their name attribute to picture1, set their index attribute to 0, 1, 3, and set their autoredrew attribute to true, set their autoresize attribute to true and Their visable attribute to false.

  5. Set the picture attributes of the preceding four graphic box controls to face1.ico, face2.ico, face3.ico, and face4.ico.

  6. Add the first menu item on form1, set its title to "[& F] File", and set its name to mnufile. Add a sub-menu item under it, set its title to "[& E] exit", and set its name to mnuexit.

  7. Add the second menu item on form1 and set its title to "[& A] Facebook" and its name to mnuface. Add four sub-menu items under it, and set the names of the four sub-menu items to "[N] Normal", "[& S] Smile ", "[& L] Laugh" and "[& O] Sorrow ". Set their names to mnufacesel, and set the index of the four sub-menu items to, 2, and 3.

  8. Add the following code to the form_load event of form1:


    Private sub form_load ()
    Dim nloopctr as integer
    Dim lresult as long
    Dim htempdc as long
    Dim nwidth as integer
    Dim nheight as integer
    Dim ltempid as long
    Dim hmenuid as long
    Dim litemcount as long
    Dim hbitmap as long

    Nwidth = picture1 (nloopctr). width/screen. twipsperpixelx
    Nheight = picture1 (nloopctr). Height/screen. twipsperpixely
    Hmenuid = getsubmenu (getmenu (Me. hwnd), 1)

    Htempdc = createcompatibledc (picture1 (nloopctr). HDC)

    For nloopctr = 0 to 3
    Hbitmap = createcompatiblebitmap (picture1 (nloopctr). HDC, nwidth,
    Nheight)

    Ltempid = SelectObject (htempdc, hbitmap)

    Lresult = bitblt (htempdc, 0, 0, nwidth, nheight, (picture1 (nloopctr ).
    HDC), 0, 0, srccopy)

    Ltempid = SelectObject (htempdc, ltempid)

    Mnufacesel (nloopctr). Caption = ""

    Lresult = modifymenu (hmenuid, nloopctr, mf_byposition or mf_bitmap,
    Getmenuitemid (hmenuid, nloopctr), hbitmap)

    Next nloopctr

    Lresult = deletedc (htempdc)
    End sub

  9. Add the following code to the click event in the "exit" submenu:


    Private sub mnuexit_click (index as integer)
    Select case index
    Case 0
    Unload me
    End select
    End sub


    Run the sample program and click the "Facebook" menu. The bitmap sub-menu items formed by the four Facebook icons are displayed, as shown in 1. Click "file"/"exit" to exit the application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.