home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch12 / menumod / menumod.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-20  |  6.3 KB  |  174 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Menu with Bitmap Graphics"
  4.    ClientHeight    =   2070
  5.    ClientLeft      =   165
  6.    ClientTop       =   735
  7.    ClientWidth     =   4680
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   2070
  10.    ScaleWidth      =   4680
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.PictureBox Picture1 
  13.       AutoRedraw      =   -1  'True
  14.       AutoSize        =   -1  'True
  15.       BorderStyle     =   0  'None
  16.       Height          =   735
  17.       Left            =   480
  18.       ScaleHeight     =   735
  19.       ScaleWidth      =   975
  20.       TabIndex        =   0
  21.       Top             =   1080
  22.       Visible         =   0   'False
  23.       Width           =   975
  24.    End
  25.    Begin VB.Label Label1 
  26.       Caption         =   "You can switch the menu items between text and graphics by clicking on Bitmaps->Display Graphics."
  27.       Height          =   615
  28.       Left            =   240
  29.       TabIndex        =   1
  30.       Top             =   360
  31.       Width           =   4215
  32.    End
  33.    Begin VB.Menu Bitmaps 
  34.       Caption         =   "Bitmaps"
  35.       Begin VB.Menu MyMenu 
  36.          Caption         =   "Happy Face"
  37.          Index           =   0
  38.       End
  39.       Begin VB.Menu MyMenu 
  40.          Caption         =   "Donut"
  41.          Index           =   1
  42.       End
  43.       Begin VB.Menu MyMenu 
  44.          Caption         =   "-"
  45.          Index           =   2
  46.       End
  47.       Begin VB.Menu MyMenu 
  48.          Caption         =   "Display Graphics"
  49.          Checked         =   -1  'True
  50.          Index           =   3
  51.       End
  52.       Begin VB.Menu Exit 
  53.          Caption         =   "Exit"
  54.          Index           =   1
  55.       End
  56.    End
  57. Attribute VB_Name = "Form1"
  58. Attribute VB_GlobalNameSpace = False
  59. Attribute VB_Creatable = False
  60. Attribute VB_PredeclaredId = True
  61. Attribute VB_Exposed = False
  62. Option Explicit
  63. Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
  64. Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, _
  65.     ByVal nPos As Long) As Long
  66. Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, _
  67.     ByVal nPos As Long) As Long
  68. Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" _
  69.     (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, _
  70.     ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
  71. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  72. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _
  73.     ByVal nWidth As Long, ByVal nHeight As Long) As Long
  74. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
  75.     ByVal hObject As Long) As Long
  76. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
  77.     ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
  78.     ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) _
  79.     As Long
  80. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  81. Const MF_STRING = &H0&
  82. Const SRCCOPY = &HCC0020
  83. Const MF_BYPOSITION = &H400&
  84. Const MF_BITMAP = &H4&
  85. Private Sub Exit_Click(Index As Integer)
  86.     Unload Me
  87. End Sub
  88. Private Sub Form_Load()
  89.     Call DisplayBitmapMenu
  90. End Sub
  91. Private Sub MyMenu_Click(Index As Integer)
  92.     'Display text
  93.     If MyMenu(3).Checked Then
  94.         MyMenu(3).Checked = False
  95.         Call DisplayTextMenu
  96.     Else
  97.         MyMenu(3).Checked = True
  98.         Call DisplayBitmapMenu
  99.     End If
  100. End Sub
  101. Private Sub DisplayTextMenu()
  102.     Dim hMenuID As Long
  103.     Dim menuID As Long
  104.     Dim menuPos As Integer
  105.     Dim retValue As Long
  106.    'Get handle to menu
  107.     hMenuID = GetSubMenu(GetMenu(Me.hwnd), 0)
  108.     menuPos = 0
  109.     menuID = GetMenuItemID(hMenuID, menuPos)
  110.     retValue = ModifyMenu(hMenuID, menuPos, MF_BYPOSITION Or MF_STRING, _
  111.                             menuID, "Happy Face")
  112.     menuPos = 1
  113.     menuID = GetMenuItemID(hMenuID, menuPos)
  114.     retValue = ModifyMenu(hMenuID, menuPos, MF_BYPOSITION Or MF_STRING, _
  115.                             menuID, "Donut")
  116. End Sub
  117. Private Sub DisplayBitmapMenu()
  118.     Dim Width As Integer
  119.     Dim Height As Integer
  120.     Dim hTmpDC As Long
  121.     Dim hMenuID As Long
  122.     Dim hBitmap As Long
  123.     Dim retValue As Long
  124.     Dim tmpID As Long
  125.     Dim fileName As String
  126.     Dim menuPos As Integer
  127.     Dim menuID As Long
  128.     'Set menu position and file name
  129.     menuPos = 0
  130.     fileName = App.Path & "\face03.ico"
  131.     Picture1.Picture = LoadPicture(fileName)
  132.     Width = Picture1.Width / Screen.TwipsPerPixelX
  133.     Height = Picture1.Height / Screen.TwipsPerPixelY
  134.     'Get handle to menu
  135.     hMenuID = GetSubMenu(GetMenu(Me.hwnd), menuPos)
  136.     'Create device context to store bitmap
  137.     hTmpDC = CreateCompatibleDC(Picture1.hdc)
  138.         
  139.     'Create the bitmap for the picture
  140.     hBitmap = CreateCompatibleBitmap(Picture1.hdc, Width, Height)
  141.                     
  142.     'Select bitmap into temporary dc
  143.     tmpID = SelectObject(hTmpDC, hBitmap)
  144.     'Copy contents from picture control to DC
  145.     retValue = BitBlt(hTmpDC, 0, 0, Width, Height, Picture1.hdc, _
  146.                         0, 0, SRCCOPY)
  147.                         
  148.     'Deselect bitmap
  149.     tmpID = SelectObject(hTmpDC, tmpID)
  150.     'Modify the menu
  151.     menuID = GetMenuItemID(hMenuID, menuPos)
  152.     retValue = ModifyMenu(hMenuID, menuPos, MF_BYPOSITION Or MF_BITMAP, _
  153.                             menuID, hBitmap)
  154.     'Second menu item
  155.     menuPos = 1
  156.     fileName = App.Path & "\donuts.ico"
  157.     Picture1.Picture = LoadPicture(fileName)
  158.      'Create the bitmap for the picture
  159.     hBitmap = CreateCompatibleBitmap(Picture1.hdc, Width, Height)
  160.                     
  161.     'Select bitmap into temporary dc
  162.     tmpID = SelectObject(hTmpDC, hBitmap)
  163.     retValue = BitBlt(hTmpDC, 0, 0, Width, Height, Picture1.hdc, _
  164.                         0, 0, SRCCOPY)
  165.                         
  166.     tmpID = SelectObject(hTmpDC, tmpID)
  167.     menuID = GetMenuItemID(hMenuID, menuPos)
  168.     retValue = ModifyMenu(hMenuID, menuPos, MF_BYPOSITION Or MF_BITMAP, _
  169.                             menuID, hBitmap)
  170.                             
  171.     'Clean up
  172.     retValue = DeleteDC(hTmpDC)
  173. End Sub
  174.