티스토리 뷰

728x90
출처 : http://stackoverflow.com/questions/728701/what-is-the-send-api-for-accessing-menu-commands-from-outside-application
http://swissdelphicenter.com/torry/showcode.php?id=1104
http://www.delphisources.ru/pages/faq/base/get_res_names.html
http://www.swissdelphicenter.ch/torry/showcode.php?id=1710

// Grab sub menu for a Window (by handle), given by (0 based) indices in menu hierarchy
function GetASubmenu(const hW: HWND; const MenuInts: array of Integer): HMENU;
var
  hSubMenu: HMENU;
  I: Integer;
begin
  Result := 0;
  if Length(MenuInts) = 0 then
    Exit;
 
  hSubMenu := GetMenu(hW);
  if not IsMenu(hSubMenu) then
    Exit;
 
  for I in MenuInts do
  begin
    Assert(I < GetMenuItemCount(hSubMenu), format('GetASubmenu: tried %d out of %d items',[I, GetMenuItemCount(hSubMenu)]));
    hSubMenu := GetSubMenu(hSubMenu, I);
    if not IsMenu(hSubMenu) then
      Exit;
  end;
 
  Result := hSubMenu;
end;
 
// Get the caption for MenuItem ID
function GetMenuItemCaption(const hSubMenu: HMENU; const Id: Integer): string;
var
  MenuItemInfo: TMenuItemInfo;
begin
  MenuItemInfo.cbSize := 44;           // Required for Windows 95. not sizeof(AMenuInfo)
  MenuItemInfo.fMask := MIIM_STRING;
  // to get the menu caption, 1023 first chars should be enough
  SetLength(Result, 1023 + 1);
  MenuItemInfo.dwTypeData := PChar(Result);
  MenuItemInfo.cch := Length(Result)-1;
  if not GetMenuItemInfo(hSubMenu, Id, False, MenuItemInfo) then
    RaiseLastOSError;
  // real caption's size. Should call GetMenuItemInfo again if was too short
  SetLength(Result, MenuItemInfo.cch);
  {$WARN SYMBOL_PLATFORM OFF}
  if DebugHook > 0 then
    OutputDebugString(MenuItemInfo.dwTypeData);
end;
 
procedure Test;
var
  hwnd, hSubMenu: Cardinal;
  id : Integer;
begin
//  hwnd := FindWindow('Afx:00400000:8:00010013:00000000:03F61829', nil); // UltraEdit
//  hSubMenu := GetASubmenu(hwnd, [5,0]);

  hwnd := FindWindow('Notepad', nil); // get the 1st instance of Notepad...
  hSubMenu := GetASubmenu(hwnd, [3]); // 4th submenu Menu aka &View
 
  if hSubMenu > 0 then
  begin
    id := GetMenuItemID(hSubMenu, 0); // 1st Item in that sub menu (must not be a submenu itself)
    if id > -1 then
    begin
      PostMessage(hwnd, WM_COMMAND, id, 0); 
      ShowMessage('Done: ' + GetMenuItemCaption(hSubMenu, id));
    end
    else
      RaiseLastOSError;
  end
  else
    RaiseLastOSError;
end;

// 리소스 파일에 존재하는 메뉴 조회
procedure GetMenuInfo2(MainMenu: hMenu; MenuType : TMenuType);
var
  id : integer;
  i, nItems: Integer;
  hwnd, hSubMenu: hMenu;
  menuTitle : String;
begin
  nItems := GetMenuItemCount(MainMenu);
  for i := 0 to  nItems - 1 do begin
    id := GetMenuItemID(MainMenu, i);
    if id > -1 then begin
      menuTitle := GetSubMenuItemCaption(MainMenu, id);
      if Length(menuTitle)>0 then begin
        //ShowMessage(menuTitle);
        MenuType.Menus.Add(menuTitle);
      end;
    end else begin
      hSubMenu := GetSubMenu(MainMenu, i );
      GetMenuInfo2(hSubMenu, MenuType);
    end;
  end;
end;
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함