Search In Site

25 June, 2013

How To Add Bitmaps To Menus

Adding Bitmaps To Menus
You can even add bitmaps to Visual Basic menu items, although you can’t use Visual Basic directly to do that. To see how to do that, we’ll create an example that will load in a small bitmap file, image.bmp, and display it in a menu item.
This is going to take some Windows work, which we’ll introduce later in the book (if you don’t understand what’s going on, it will become clear later). First, create a new project and give Form1 a File menu with one item in it. Add a Picture control, Picture1, to the form, setting that controls Visible property to False, and its AutoRedraw property to True. We’ll use that control to load in the image file when the form loads:

Private Sub Form_Load()
Picture1.Picture = LoadPicture(App.Path & "\image.bmp")
...
End Sub


To insert a bitmap into a menu item, we’ll need a handle to a bitmap. We have access to the image in the

Picture control, so we create a device context with the Windows CreateCompatibleDC() function, and an empty bitmap with the Windows CreateCompatibleBitmap() function (note that all the Windows

functions we used must be declared before being used we’ll see more about this later in the book):

Private Sub Form_Load()
Picture1.Picture = LoadPicture(App.Path & "\image.bmp")
Dim dcMemory As Long
Dim hMemoryBitmap As Long
dcMemory = CreateCompatibleDC(Picture1.hdc)
hMemoryBitmap = CreateCompatibleBitmap(Picture1.hdc, 60, 30)
...
End Sub


Next, we select (that is, install) the new bitmap into the device context using SelectObject:

Private Sub Form_Load()
Picture1.Picture = LoadPicture(App.Path & "\image.bmp")
Dim dcMemory As Long
Dim hMemoryBitmap As Long
dcMemory = CreateCompatibleDC(Picture1.hdc)
hMemoryBitmap = CreateCompatibleBitmap(Picture1.hdc, 60, 30)
Dim pObject As Long
pObject = SelectObject(dcMemory, hMemoryBitmap)
...
End Sub


Now that we’ve created our new device context and installed a bitmap, we can copy the image from the

Picture controls device context to the new device context this way using the Windows BitBlt() function:

Private Sub Form_Load()
picture1.Picture = LoadPicture(App.Path & "\image.bmp")
Dim dcMemory As Long
Dim hMemoryBitmap As Long
dcMemory = CreateCompatibleDC(Picture1.hdc)
hMemoryBitmap = CreateCompatibleBitmap(Picture1.hdc, 60, 30)
Dim pObject As Long
pObject = SelectObject(dcMemory, hMemoryBitmap)
dummy = BitBlt(dcMemory, 0, 0, 60, 30, Picture1.hdc, 0, 0, &HCC0020)
dummy = SelectObject(dcMemory, pObject)
...
End Sub


Finally, we use the Windows ModifyMenu() function to modify the menu, installing our new bitmap:

Private Sub Form_Load()
Picture1.Picture = LoadPicture(App.Path & "\image.bmp")
Dim dcMemory As Long
Dim hMemoryBitmap As Long
dcMemory = CreateCompatibleDC(Picture1.hdc)
hMemoryBitmap = CreateCompatibleBitmap(Picture1.hdc, 60, 30)
Dim pObject As Long
pObject = SelectObject(dcMemory, hMemoryBitmap)
dummy = BitBlt(dcMemory, 0, 0, 60, 30, Picture1.hdc, 0, 0, &HCC0020)
dummy = SelectObject(dcMemory, pObject)
dummy = ModifyMenu(GetSubMenu(GetMenu(Me.hwnd), 0), 0, &H404, 0,_
hMemoryBitmap) 
End Sub

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget