遍历菜单项

来源:互联网 发布:js判断控件是否隐藏 编辑:程序博客网 时间:2024/06/10 04:32
//遍历任何窗体的MenuItem
//用了两个多小时
//略做修改,就可以实现其他功能,GetMenuItemInfo提供的信息足够用了

procedure ExplorerMenuItem(MenuHandle: THandle);
var
  MenuIndex: Integer;
  MenuCaptionArray: array[1..255]  of Char;
  MenuCaptionString: String;
  ItemInfo: TMenuItemInfo;
  Buffer: array[0..20] of Char;
begin
  for MenuIndex := 0 to GetMenuItemCount(MenuHandle) - 1 do begin
    //Something todo. Here we get the caption
    GetMenuString(MenuHandle, MenuIndex, @MenuCaptionArray, 255, MF_BYPOSITION);
    MenuCaptionString := MenuCaptionArray;
    SetLength(MenuCaptionString,StrLen(PChar(MenuCaptionString)));
    ShowMessage('Menu Caption: ' + MenuCaptionString);
    //Get caption ends here.
     
    ItemInfo.cbSize := SizeOf(TMenuItemInfo);
    ItemInfo.fMask := MIIM_SUBMENU;
    ItemInfo.dwTypeData := Buffer;
    ItemInfo.cch := SizeOf(Buffer);
    GetMenuItemInfo(MenuHandle, MenuIndex, True, ItemInfo);
    //submenu, calls itself
    if ItemInfo.hSubMenu <> 0 then begin
      ExplorerMenuItem(ItemInfo.hSubMenu);
    end;
  end;
end;
--lw549的专栏
原创粉丝点击