Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 control’s 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 control’s 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

The result appears in Figure 5.24, where you can see our bitmap in the File menu.


Figure 5.24  Using bitmapped menu items.

The listing for this form appears in is locate in the bitmap folder on this book’s accompaning CD-ROM. (Note that all the Windows functions we used must be declared before being used—we’ll see more about this later in the book.)

Using The Registry To Store A Most Recently Used (MRU) Files List

Your program’s users love your new application—but there’s always something new in the Suggestions box. Today’s suggestion asks whether you can add a Most Recently Used (MRU) list of files to the File menu. These lists are appended to the end of the File menu and let the user select recently opened files easily. In fact, the Visual Basic IDE has an MRU list, as you can see in Figure 5.25.


Figure 5.25  The Visual Basic MRU list.

In this example, we’ll support a very short MRU list—just one item—but the idea is easily extendable. Create a new Visual Basic project now named “mru”, and give Form1 a File menu with two items in it: Open (“mnuOpen”) and MRU (“mnuMRU”). Make the MRU item a control array by setting its Index property to 0 in the Menu Editor, and make it invisible by deselecting the Visible box in the Menu Editor so we can use it as a placeholder.

This example uses the Visual Basic GetSetting() and SetSetting() functions to access the Windows Registry. We’ll see how to use these functions in depth later in this book, but for now, we use GetSetting() when Form1 is first loaded to see if we’ve saved a file name for the MRU list in the Registry’s Settings/Doc1 section (here, we’ll use the application’s name as its Registry key, and we get that name from App.Title):

Private Sub Form_Load()
    Dim FileName As String
    FileName = GetSetting(App.Title, "Settings", "Doc1")

If we have saved a file name in the Registry, we should place it in the File menu, and we do that by loading a new menu item in the mnuMRU array, setting its caption to the file name, and making it visible this way:

Private Sub Form_Load()
    Dim FileName As String
    FileName = GetSetting(App.Title, "Settings", "Doc1")

    If FileName <> "" Then
        Load mnuMRU(1)
        mnuMRU(1).Caption = FileName
        mnuMRU(1).Visible = True
    End If
End Sub

That solves the case where we’ve stored a file name for the MRU list in the registry—but how do we store those names there in the first place? We do that when the user selects the Open item in the File menu. To get the file name from the user, we’ll use an Open Common Dialog box, so add a Common Dialog control named dlgCommonDialog to the form now (if you don’t know how to do that, see Chapter 17, which discusses file handling) and get a file name to open from the user this way:

Private Sub mnuOpen_Click()
    With dlgCommonDialog
        .DialogTitle = "Open"
        .CancelError = False
        .Filter = "All Files (*.*)|*.*"
        .ShowOpen
        If Len(.FileName) = 0 Then
            Exit Sub
        End If


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.