想将窗口中的菜单信息取出来用一个treeview来显示,但现在问题是使用 controls集合可以取得所有的菜单名,但是它的上级菜单是哪一个??这个问题困扰了我很久,但现在必须要用了...

解决方案 »

  1.   

    刚发贴就发现这里这个回题好象还比较好用,paste一下。然后再慢慢研究
    http://topic.csdn.net/t/20020611/13/794545.html所缺的API声明自已加进去,不知道这个函数合不合题意?   
        
      Public   Function   FindMenu(hMenu   As   Long,   strMenuCaption   As   String,   lpParent   As   Long,   lpPosition   As   Long)   As   Boolean   
      '   函数:   FindMenu(),通过menu.caption来识别菜单   
      '   目的:   递归寻找一个菜单项   
      '   出口:   lpParent       --   上级菜单   
      '               lpPosition   --   菜单项的位置   
      '   返回:   True               --   找到了   
      '               False             --   找不到   
      '   用法:   
      '   Dim   lngParent   as   long,lngPosition   as   long   
      '   FindMenu   GetMenu(me.hwnd),"Open",lngParent,lngPosition   
      '   debug.print   "位置:"   ,lngPosition   
        
              Dim   nCount   As   Long,   i   As   Long,   lngID   As   Long   
              Dim   strFound   As   String   
                
              nCount   =   GetMenuItemCount(hMenu)   
                
              For   i   =   0   To   nCount   -   1   
                      strFound   =   Space(256)   
                      GetMenuString   hMenu,   i,   strFound,   256,   MF_BYPOSITION   
                      strFound   =   Trim(strFound)   
                      strFound   =   Left(strFound,   Len(strFound)   -   1)   
        
                      If   strFound   =   strMenuCaption   Then   
                              lpParent   =   hMenu   
                              lpPosition   =   i   
                              FindMenu   =   True   
                              Exit   Function   
                      Else   
                              If   FindMenu(GetSubMenu(hMenu,   i),   strMenuCaption,   lpParent,   lpPosition)   Then   
                                      FindMenu   =   True   
                                      Exit   Function   
                              End   If   
                      End   If   
              Next   i   
      End   Function   
      

  2.   

    我实现过类似问题,当时是在代码中把每个菜单的tag值设置成父菜单的key值。如果是顶级菜单为空。然后遍历实现的