home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 22 / CD_ASCQ_22_0695.iso / win / prg / psmmm11.exe / SAMPLE.DEU / BMPMNU / BASIC / BMPMNU.BAS next >
BASIC Source File  |  1995-03-19  |  11KB  |  283 lines

  1. Option Explicit
  2.  
  3. Global Const MF_BYPOSITION = &H400
  4. Global Const MF_SEPARATOR = &H800
  5. Global Const MF_ENABLED = &H0
  6. Global Const MF_GRAYED = &H1
  7. Global Const MF_DISABLED = &H2
  8. Global Const MF_UNCHECKED = &H0
  9. Global Const MF_CHECKED = &H8
  10. Global Const MF_BITMAP = &H4
  11. Global Const MF_POPUP = &H10
  12. Global Const MF_MENUBARBREAK = &H20
  13. Global Const MF_MENUBREAK = &H40
  14.  
  15. Global Const WHITE_BRUSH = 0
  16. Global Const LF_FACESIZE = 32
  17.  
  18. Type RECT
  19.     left   As Integer
  20.     top    As Integer
  21.     right  As Integer
  22.     bottom As Integer
  23. End Type
  24.  
  25. Type LOGFONT
  26.     lfHeight         As Integer
  27.     lfWidth          As Integer
  28.     lfEscapement     As Integer
  29.     lfOrientation    As Integer
  30.     lfWeight         As Integer
  31.     lfItalic         As String * 1
  32.     lfUnderline      As String * 1
  33.     lfStrikeOut      As String * 1
  34.     lfCharSet        As String * 1
  35.     lfOutPrecision   As String * 1
  36.     lfClipPrecision  As String * 1
  37.     lfQuality        As String * 1
  38.     lfPitchAndFamily As String * 1
  39.     lfFaceName       As String * LF_FACESIZE
  40. End Type
  41.  
  42. Type TEXTMETRIC
  43.     tmHeight           As Integer
  44.     tmAscent           As Integer
  45.     tmDescent          As Integer
  46.     tmInternalLeading  As Integer
  47.     tmExternalLeading  As Integer
  48.     tmAveCharWidth     As Integer
  49.     tmMaxCharWidth     As Integer
  50.     tmWeight           As Integer
  51.     tmItalic           As String * 1
  52.     tmUnderlined       As String * 1
  53.     tmStruckOut        As String * 1
  54.     tmFirstChar        As String * 1
  55.     tmLastChar         As String * 1
  56.     tmDefaultChar      As String * 1
  57.     tmBreakChar        As String * 1
  58.     tmPitchAndFamily   As String * 1
  59.     tmCharSet          As String * 1
  60.     tmOverhang         As Integer
  61.     tmDigitizedAspectX As Integer
  62.     tmDigitizedAspectY As Integer
  63. End Type
  64.  
  65. Declare Function DestroyMenu Lib "User" (ByVal hMenu As Integer) As Integer
  66. Declare Function DrawMenuBar Lib "USER" (ByVal hWnd As Integer) As Integer
  67. Declare Function GetMenu Lib "User" (ByVal hWnd As Integer) As Integer
  68. Declare Function GetMenuItemCount Lib "User" (ByVal hMenu As Integer) As Integer
  69. Declare Function GetMenuItemID Lib "User" (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
  70. Declare Function GetMenuState Lib "User" (ByVal hMenu As Integer, ByVal wID As Integer, ByVal wFlags As Integer) As Integer
  71. Declare Function GetMenuString Lib "User" (ByVal hMenu As Integer, ByVal wIDItem As Integer, ByVal lpString As String, ByVal nMaxCount As Integer, ByVal wFlag As Integer) As Integer
  72. Declare Function GetSubMenu Lib "User" (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
  73. Declare Function ModifyMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpString As Any) As Integer
  74.  
  75. Declare Function CreateBitmap Lib "GDI" (ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal nPlanes As Integer, ByVal nBitCount As Integer, ByVal lpBits As Any) As Integer
  76. Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC As Integer) As Integer
  77. Declare Function CreateFontIndirect Lib "GDI" (lpLogFont As LOGFONT) As Integer
  78. Declare Function CreateIC Lib "GDI" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
  79. Declare Function DeleteDC Lib "GDI" (ByVal hDC As Integer) As Integer
  80. Declare Function DeleteObject Lib "GDI" (ByVal hObject As Integer) As Integer
  81. Declare Function ExtTextOut Lib "GDI" (ByVal hDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal wOptions As Integer, lpRect As Any, ByVal lpString As String, ByVal nCount As Integer, lpDx As Any) As Integer
  82. Declare Function FillRect Lib "User" (ByVal hDC As Integer, lpRect As RECT, ByVal hBrush As Integer) As Integer
  83. Declare Function GetStockObject Lib "GDI" (ByVal nIndex As Integer) As Integer
  84. Declare Function GetTextExtent Lib "GDI" (ByVal hDC As Integer, ByVal lpString As String, ByVal nCount As Integer) As Long
  85. Declare Function GetTextMetrics Lib "GDI" (ByVal hDC As Integer, lpMetrics As TEXTMETRIC) As Integer
  86. Declare Function SelectObject Lib "GDI" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
  87. Declare Function TextOut Lib "GDI" (ByVal hDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal lpString As String, ByVal nCount As Integer) As Integer
  88.  
  89.  
  90. Global gszFaceName   As String  'Name of the font that it will be used in the menu
  91. Global ghBitmap()    As Integer 'Array of handle of bitmap
  92. Global gnBitmapCount As Integer 'Number of handle in the array
  93.  
  94. '-------------------------------------------------------------------
  95. 'This procedure frees the bitmaps created for each menu item.
  96. 'This is necessary because, unlike the Juliet's rose(1) that will
  97. 'smell as well with another name, a bitmap menu item is quit different
  98. 'of a string menu item. With the first, you have to deal with bitmaps
  99. 'that are owned by the system so you have to delete them before ending
  100. 'the program. If you do not, a lot of resource will be lost.
  101. '
  102. ' Parameters
  103. '   No parameter
  104. '
  105. '(1) Romeo and Juliet, William SHAKESPEARE
  106. '-------------------------------------------------------------------
  107. Sub CleanBitmapMenu ()
  108.     
  109.     Dim i As Integer
  110.     Dim dummy As Integer
  111.  
  112.     On Error Resume Next
  113.     For i = 0 To gnBitmapCount
  114.         dummy = DeleteObject(ghBitmap(i))
  115.     Next i
  116.  
  117.     Erase ghBitmap
  118.  
  119. End Sub
  120.  
  121. '-------------------------------------------------------------------
  122. ' This function create a bitmap image of a text with the given font.
  123. '
  124. ' Parameters
  125. '    szText     : Text of the menu item
  126. '    fTopLevel  : Indicate if the menu is a top level menu
  127. '    szFaceName : Name of the font that will be used
  128. '
  129. ' Returns
  130. '    The return values is the handle of the bitmap image of
  131. '    the menu Item if the function is successful. Otherwise, it is
  132. '    0.
  133. '-------------------------------------------------------------------
  134. Function GetBitmapMenu (szText As String, fTopLevel As Integer, szFaceName As String) As Integer
  135.  
  136.     Dim lf      As LOGFONT
  137.     Dim tm      As TEXTMETRIC
  138.     Dim rc      As RECT
  139.     Dim dwSize  As Long
  140.     Dim wCX     As Long
  141.     Dim wCY     As Long
  142.     Dim hBitmap As Integer
  143.     Dim hDC     As Integer
  144.     Dim hdcMem  As Integer
  145.     Dim hFont   As Integer
  146.     Dim dummy   As Integer
  147.  
  148.     hDC = CreateIC("DISPLAY", Chr$(0), Chr$(0), Chr$(0))
  149.     dummy = GetTextMetrics(hDC, tm)
  150.     
  151.     lf.lfHeight = 1 * tm.tmHeight
  152.     lf.lfFaceName = szFaceName + Chr$(0)
  153.     
  154.     hdcMem = CreateCompatibleDC(hDC)
  155.     hFont = CreateFontIndirect(lf)
  156.     dummy = SelectObject(hdcMem, hFont)
  157.     dwSize = GetTextExtent(hdcMem, szText, Len(szText))
  158.   
  159.     wCX = dwSize And 65535
  160.     wCY = (dwSize / 65536)
  161.     'If the menu is a top level menu, we add a little space before and
  162.     'after the text.
  163.     If fTopLevel Then
  164.         hBitmap = CreateBitmap(wCX + 18, wCY, 1, 1, CLng(0))
  165.         dummy = SelectObject(hdcMem, hBitmap)
  166.                                               
  167.         rc.left = 0
  168.         rc.top = 0
  169.         rc.right = wCX + 18
  170.         rc.bottom = wCY
  171.       
  172.         dummy = FillRect(hdcMem, rc, GetStockObject(WHITE_BRUSH))
  173.         dummy = TextOut(hdcMem, 8, 0, szText, Len(szText))
  174.     Else
  175.         hBitmap = CreateBitmap(wCX, wCY, 1, 1, CLng(0))
  176.         dummy = SelectObject(hdcMem, hBitmap)
  177.                                               
  178.         rc.left = 0
  179.         rc.top = 0
  180.         rc.right = wCX
  181.         rc.bottom = wCY
  182.       
  183.         dummy = FillRect(hdcMem, rc, GetStockObject(WHITE_BRUSH))
  184.         dummy = TextOut(hdcMem, 0, 0, szText, Len(szText))
  185.     End If
  186.  
  187.     dummy = DeleteDC(hdcMem)
  188.     dummy = DeleteDC(hDC)
  189.     dummy = DeleteObject(hFont)
  190.   
  191.     GetBitmapMenu = hBitmap
  192.  
  193. End Function
  194.  
  195. '-------------------------------------------------------------------
  196. ' This call the procedure TranslateMenu witch change recursively
  197. ' all the "string" menu items in "bitmap" menu item. It also
  198. ' initialize some global variables.
  199. '
  200. ' Parameters
  201. '    hWnd       : Handle of the window that it owns the menu to
  202. '                 translate.
  203. '    szFaceName : Name of the font that will be used
  204. '-------------------------------------------------------------------
  205. Sub TranslateAllMenu (hWnd As Integer, szFaceName As String)
  206.     
  207.     Dim hMenu As Integer
  208.     Dim dummy As Integer
  209.  
  210.     If gnBitmapCount > 0 Then
  211.         Call CleanBitmapMenu
  212.     End If
  213.  
  214.     gnBitmapCount = 0
  215.     gszFaceName = szFaceName
  216.     hMenu = GetMenu(hWnd)
  217.     
  218.     Call TranslateMenu(hMenu, True)
  219.     dummy = DrawMenuBar(hWnd)
  220.  
  221. End Sub
  222.  
  223. '-------------------------------------------------------------------
  224. ' This procedure translate recursively all the menu and submenu of
  225. ' a window given to the "TranslateAllMenu".
  226. '
  227. ' Parameters
  228. '    hMenu      : Handle of the menu to translate.
  229. '    fTopLevel  : Indicate if the menu is a top level menu
  230. '-------------------------------------------------------------------
  231. Sub TranslateMenu (hMenu As Integer, fTopLevel As Integer)
  232.     
  233.     Dim NumberOfItem As Integer
  234.     Dim ItemNumber   As Integer
  235.     Dim hSubMenu     As Integer
  236.     Dim fMenuSate    As Integer
  237.     Dim dummy        As Integer
  238.     Dim hBmpMenu     As Integer
  239.     Dim menuId       As Integer
  240.     Dim nTextLen     As Integer
  241.     Dim szText       As String
  242.  
  243.     NumberOfItem = GetMenuItemCount(hMenu)
  244.     For ItemNumber = 0 To NumberOfItem - 1
  245.         hSubMenu = GetSubMenu(hMenu, ItemNumber)
  246.         If hSubMenu Then
  247.             Call TranslateMenu(hSubMenu, False)
  248.         End If
  249.  
  250.         fMenuSate = GetMenuState(hMenu, ItemNumber, MF_BYPOSITION)
  251.         'Do nothing if the menu item is already a bitmap or if it
  252.         'is a separator.
  253.         If (fMenuSate And MF_BITMAP) = MF_BITMAP Then
  254.         ElseIf (fMenuSate And MF_MENUBARBREAK) = MF_MENUBARBREAK Then
  255.         ElseIf (fMenuSate And MF_MENUBREAK) = MF_MENUBREAK Then
  256.         ElseIf (fMenuSate And MF_SEPARATOR) = MF_SEPARATOR Then
  257.         Else
  258.             'Get the text of the menu item and create a bitmap image of it.
  259.             szText = String$(70, Chr$(0))
  260.             nTextLen = GetMenuString(hMenu, ItemNumber, szText, Len(szText), MF_BYPOSITION)
  261.             szText = Left$(szText, nTextLen)
  262.             
  263.             hBmpMenu = GetBitmapMenu(szText, fTopLevel, gszFaceName)
  264.             'Save the handle of the bitmap with the aim to free the bitmap at the end
  265.             'of the program.
  266.             ReDim Preserve ghBitmap(gnBitmapCount + 1)
  267.             ghBitmap(gnBitmapCount) = hBmpMenu
  268.             gnBitmapCount = gnBitmapCount + 1
  269.            
  270.             'Change the menu item in a bitmap menu item. The way to do this is a little bit different if the menu
  271.             'is a "popup" menu or if it is a "submenu".
  272.             If hSubMenu Then
  273.                 dummy = ModifyMenu(hMenu, ItemNumber, fMenuSate Or MF_BITMAP Or MF_BYPOSITION Or MF_POPUP, hSubMenu, CLng(hBmpMenu))
  274.             Else
  275.                 menuId = GetMenuItemID(hMenu, ItemNumber)
  276.                 dummy = ModifyMenu(hMenu, ItemNumber, fMenuSate Or MF_BITMAP Or MF_BYPOSITION, menuId, CLng(hBmpMenu))
  277.             End If
  278.         End If
  279.     Next ItemNumber
  280.  
  281. End Sub
  282.  
  283.