home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 450 / ACCESS01.ZIP / Q88940.TXT < prev    next >
Encoding:
Text File  |  1992-11-01  |  5.9 KB  |  159 lines

  1. How to Dim (Gray) Menu Items with Access Basic
  2.  
  3. Summary:
  4.  
  5. Access Basic does not have any intrinsic command that allows you to
  6. dim (make unavailable) a menu item. There are also no properties
  7. associated with a form that will allow you to set this menu
  8. characteristic.
  9.  
  10. To change such characteristics of a menu item, you can use Windows API
  11. functions.
  12.  
  13. For more information about the structure of the Access menu system,
  14. query on the following words in the Microsoft Knowledge Base:
  15.  
  16.    MENU and SYSTEM and FINDWINDOW and GETMENU and GETSUBMENU
  17.  
  18. More Information:
  19.  
  20. The following API function is used to dim a menu item:
  21.  
  22. EnableMenuItem% (hMenu%, wIDEnableItem%, wEnable%)
  23. --------------------------------------------------
  24.  
  25. This function enables, disables, or grays a menu item.
  26.  
  27.    hMenu%         Specifies the menu.
  28.    wIDEnableItem% Specifies the menu item to be checked. The
  29.                   wIDEnableItem% parameter can specify pop-up menu
  30.                   items as well as menu items.
  31.    wEnable%       Specifies the action to take. It can be a
  32.                   combination of MF_DISABLED, MF_ENABLED, and
  33.                   MF_GRAYED. These values can be combined by using
  34.                   the bitwise 'OR' operator.
  35.    Return Value   The return value specifies the previous state of the
  36.                   menu item. The return value is -1 if the menu item
  37.                   does not exist.
  38.  
  39. Example
  40. -------
  41.  
  42. The example below designs a menu that will dim the menu item:
  43.  
  44. 1. Create a new macro. After adding the following actions and their
  45.    associated properties, save the macro and name it "Menu
  46.    Manipulation Macro." To display the "Macro Name" column, choose the
  47.    Macro Name command from the View menu.
  48.  
  49.     Macro Name             Action        Function Name
  50.     ----------------------------------------------------------
  51.  
  52.     GrayItem               RunCode       Gray_Menu_Item(0,0)
  53.     UnGray                 RunCode       UnGray_Menu_Item(0,0)
  54.  
  55. 2. Create a new macro. After adding the action below and its
  56.    associated property, save the macro as "Custom Demo Menu."
  57.  
  58.    Macro Name             Action        Function Name
  59.    ----------------------------------------------------------
  60.    Top Level Menu         AddMenu
  61.  
  62.    [Top Level Menu].AddMenu Action Arguments
  63.    -----------------------------------------
  64.    Menu Name          &Gray
  65.    Menu Macro Name    Menu Manipulation Macro
  66.  
  67. 3. Create a new blank form and display the Properties window of the
  68.    form by choosing the Properties command from the View menu.
  69.  
  70. 4. Set the OnMenu property to "Custom Demo Menu."
  71.  
  72. 5. From the File menu, choose Close to close the form. Save the form
  73.    as "Menu Manipulation Form."
  74.  
  75. 6. Create a new module from the Database window. Within the new
  76.    module, enter the Access Basic code listed further below. Save the
  77.    module as "Menu Manipulation Code."
  78.  
  79. 7. From the Database window, select the Menu Manipulation Form and
  80.    then choose the Open button to display the form in Form view. The
  81.    normal Access menu will disappear and be replaced by the custom
  82.    menu you created with the above steps.
  83.  
  84. 8. There are two options in the menu. Choose the GrayItem command and
  85.    the command will be dimmed (grayed). Choosing the UnGray command
  86.    will make the GrayItem menu command available.
  87.  
  88. '********************************************************************
  89. 'Declarations section of the module.
  90. '********************************************************************
  91. Option Explicit
  92.  
  93. 'Note: Each Declaration must be placed on a single line.
  94. Declare Function FindWindow% Lib "user" (ByVal lpClassName
  95.                     As Any, ByVal lpCaption As Any)
  96. Declare Function GetMenu% Lib "user" (ByVal hWnd%)
  97. Declare Function GetSubMenu% Lib "user" (ByVal hSubMenu%,
  98.                     ByVal nPos%)
  99. Declare Function EnableMenuItem% Lib "user" (ByVal hMenu%,
  100.                     ByVal wItem%, ByVal wEnable%)
  101.  
  102. Const MF_BYPOSITION = &H400
  103. Const MF_GRAYED = &H1
  104.  
  105. Const MyNull = 0&
  106. Const ClassName = "OMain"
  107.  
  108. Dim ChWnd%       'handle to the Microsoft Access window.
  109. Dim hMenuTop%    'handle to the Microsoft Access menu.
  110. Dim hSubMenu%    'handle to the pop-up menu
  111. Dim ItemID%      'command ID associated with menu item.
  112. Dim ReturnVal%
  113.  
  114. '===========================================================
  115. 'This function will initialize:
  116. '
  117. ' - The window handles associated with the Access form.
  118. ' - The handle to the menu of the specified window.
  119. ' - Menu handle of the specified pop-up menu of the window menu.
  120. '
  121. 'The variables here are global to the database.
  122. '===========================================================
  123. Sub Get_Menu_Handles (TopLevel%)
  124.  
  125.    ChWnd% = FindWindow(ClassName, MyNull)
  126.    hMenuTop% = GetMenu(ChWnd%)
  127.    hSubMenu% = GetSubMenu(hMenuTop%, TopLevel%)
  128.  
  129. End Sub
  130.  
  131. '===========================================================
  132. 'This function will dim a menu item. The text of a dimmed
  133. 'menu item is displayed in light gray text on the menu,
  134. 'but does not allow the user to select the item either by
  135. 'mouse or keypad. The macro action associated with the
  136. 'menu item will not execute when the user tries to select
  137. 'the menu item.
  138. '===========================================================
  139. Function Gray_Menu_Item (TopLevel%, SubLevel%)
  140.  
  141.    Call Get_Menu_Handles(TopLevel%)
  142.    Gray_Menu_Item = EnableMenuItem(hSubMenu, SubLevel%,
  143.                         MF_GRAYED Or MF_BYPOSITION)
  144.  
  145. End Function
  146.  
  147. '===========================================================
  148. 'This function will not ungray a menu item that also enables
  149. 'the menu item so the user can select the item and run the
  150. 'macro associated with the menu.
  151. '===========================================================
  152. Function UnGray_Menu_Item% (TopLevel%, SubLevel%)
  153.  
  154.    Call Get_Menu_Handles(TopLevel%)
  155.    UnGray_Menu_Item = EnableMenuItem(hSubMenu, SubLevel%,
  156.                           Not MF_GRAYED And MF_BYPOSITION)
  157.  
  158. End Function
  159.