检测特定应用程序是否打开了任何菜单

Detect whether a specific app has any menu opened

我如何检测特定应用(应用的 window 句柄和进程 ID 已知)当前是否有任何 MENU 打开(主菜单或弹出菜单)菜单)?

我对此进行了研究,但没有找到任何东西。

一个可能的实现可能涉及枚举目标应用程序window所属线程的顶层windows,以搜索其中是否有菜单window[=18] =].根据 documentation.

,这是“#32768”

以下示例在计时器事件处理程序中对 Windows 7 计算器执行相同的操作。如果程序的菜单或上下文菜单打开,该示例将输出调试字符串。

function EnumThreadWindowsCallback(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
const
  MenuWndClass = '#32768';
var
  ClassName: array[0..256] of Char;
begin
  Result := True;
  if (GetClassName(hwnd, ClassName, Length(ClassName)) = Length(MenuWndClass)) and
      (ClassName = MenuWndClass) then begin
    PBoolean(lparam)^ := True;
    Result := False;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  Wnd: HWND;
  ThrId: DWORD;
  MenuWnd: Boolean;
begin
  Wnd := FindWindow('CalcFrame', 'Calculator');
  if Wnd <> 0 then begin
    ThrId := GetWindowThreadProcessId(Wnd);
    MenuWnd := False;
    EnumThreadWindows(ThrId, @EnumThreadWindowsCallback, LPARAM(@MenuWnd));
    if MenuWnd then
      OutputDebugString('active menu');
  end;
end;