0

是否有发送 api 来访问另一个应用程序中的菜单命令?例如,我正在尝试访问记事本的查看菜单。我要怎么做?我已经使用 GetSystemMenu 获得了菜单,但我无法访问它。我认为这已经有一个 API,但我不知道。

4

2 回答 2

3

Python 中的以下代码激活查看/状态栏菜单项。将它转换为 Delphi 应该没有问题,因为它看起来像伪代码。它选择第 4 个菜单项(“视图”)和第 1 个菜单项(“状态栏”)。如果需要,您可以将其更改为通过遍历项目并使用GetMenuString. 有关详细信息,请参阅 MSDN。

请注意,它不会进行任何错误检查。另请注意,它希望记事本的标题为“无标题 - 记事本”。(您可以将其更改None为搜索任何内容;我想那将是nil在 Delphi 中。)

from win32gui import *
from win32con import *
hwnd = FindWindow('Notepad', 'Untitled - Notepad') # use Winspector Spy to find window class name and title
hmenu = GetMenu(hwnd)
hviewmenu = GetSubMenu(hmenu, 3)                   # 3rd menu item across, starting from 0
id = GetMenuItemID(hviewmenu, 0)                   # 0th menu item down ("Status Bar")
PostMessage(hwnd, WM_COMMAND, id, 0)
于 2009-04-08T08:18:47.403 回答
2

这里有一些 Delphi 代码。
请注意,如果您没有真正的菜单,这将不起作用。
来自帮助:“GetMenu 不适用于浮动菜单栏。浮动菜单栏是模仿标准菜单的自定义控件;它们不是菜单。要获得浮动菜单栏的句柄,请使用 Active Accessibility API。”

例如,它不适用于 Delphi 本身......

// 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;
于 2009-04-08T22:57:11.160 回答