什么是用于从外部应用程序访问菜单命令的Send API

时间:2009-04-08 06:01:31

标签: delphi api menu send

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

2 个答案:

答案 0 :(得分:3)

Python中的以下代码激活了View / Status Bar菜单项。你应该没有麻烦将它转换为Delphi,因为它看起来像伪代码。它选择第四个菜单项(“视图”)和第一个项目(“状态栏”)。如果您愿意,可以通过浏览项目并使用GetMenuString来更改它以按文本搜索所需的项目。有关详细信息,请参阅MSDN。

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

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)

答案 1 :(得分: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;