Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
168 lines
6.7 KiB
Python
168 lines
6.7 KiB
Python
"""Probe which message path opens the real CBB context menu on a private desktop.
|
|
|
|
Production must not switch the input desktop, so the winning variant has to be a
|
|
posted/sent message, not pywinauto's click_input. Tries, in order:
|
|
1. posted WM_MOUSEMOVE/WM_RBUTTONDOWN/WM_RBUTTONUP with client coords (current production path)
|
|
2. posted WM_CONTEXTMENU with screen coords to the tree
|
|
3. sent (synchronous) WM_MOUSEMOVE/WM_RBUTTONDOWN/WM_RBUTTONUP with client coords
|
|
4. posted WM_CONTEXTMENU with screen coords to the top-level frame
|
|
Results land in build/context-menu-variants.json.
|
|
"""
|
|
import ctypes, json, subprocess, time
|
|
from pathlib import Path
|
|
import faulthandler
|
|
import win32con, win32gui, win32process, win32service
|
|
|
|
# A blocked SendMessage or a UIA call into a modal menu loop would otherwise
|
|
# hang this probe forever; dump the stack and exit instead.
|
|
faulthandler.dump_traceback_later(120, exit=True)
|
|
|
|
def step(message):
|
|
print(f'[{time.strftime("%H:%M:%S")}] {message}', flush=True)
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
LIBRARY = ROOT/'build'/'background-fixture'/'Mentor_lib_EE7.9'/'mentor_lib.lmc'
|
|
NAME = 'Audio_D2128_ESD'
|
|
OUTPUT = ROOT/'build'/'context-menu-variants.json'
|
|
DESKTOP = 'CbbContextMenuProbe'
|
|
|
|
desktop = win32service.CreateDesktop(DESKTOP, 0, win32con.GENERIC_ALL, None)
|
|
desktop.SetThreadDesktop()
|
|
from pywinauto import Desktop
|
|
from pywinauto.controls.common_controls import TreeViewWrapper
|
|
from pywinauto import win32defines
|
|
|
|
startup = win32process.STARTUPINFO(); startup.lpDesktop = 'winsta0\\' + DESKTOP
|
|
exe = r'D:\Mentor\7.9.5EE\SDD_HOME\lm\win32\bin\LibraryManager.exe'
|
|
hp, ht, pid, tid = win32process.CreateProcess(exe, subprocess.list2cmdline([exe, str(LIBRARY)]), None, None, False, 0, None, None, startup)
|
|
ht.Close()
|
|
lm = None
|
|
for _ in range(120):
|
|
windows = [w for w in Desktop(backend='win32').windows()
|
|
if w.is_visible() and w.window_text().startswith('Library Manager:') and w.process_id() == pid]
|
|
if len(windows) == 1:
|
|
lm = windows[0]
|
|
break
|
|
time.sleep(.5)
|
|
if lm is None:
|
|
raise SystemExit('Library Manager did not open')
|
|
|
|
tree = TreeViewWrapper([w for w in lm.descendants() if 'TreeView' in w.class_name()][0].handle)
|
|
item = tree.get_item(['Central Libraries', LIBRARY.name, 'Reusable Blocks', 'Logical-Physical', NAME], exact=True)
|
|
item.ensure_visible()
|
|
ctypes.windll.user32.SetFocus(tree.handle)
|
|
tree.send_message(win32defines.TVM_SELECTITEM, win32defines.TVGN_CARET, item.elem)
|
|
print('selected:', item.is_selected(), 'tree:', tree.handle, flush=True)
|
|
|
|
ui = Desktop(backend='uia').window(handle=lm.handle)
|
|
hits = [entry for entry in ui.descendants(control_type='TreeItem') if entry.window_text() == NAME]
|
|
if len(hits) != 1:
|
|
raise SystemExit(f'CBB 条目定位不唯一: {len(hits)}')
|
|
rect = hits[0].rectangle()
|
|
sx, sy = rect.mid_point().x, rect.mid_point().y
|
|
client = ctypes.wintypes.POINT(sx, sy)
|
|
ctypes.windll.user32.ScreenToClient(tree.handle, ctypes.byref(client))
|
|
client_xy = (client.x, client.y)
|
|
screen_xy = (sx, sy)
|
|
print('screen:', screen_xy, 'client:', client_xy, flush=True)
|
|
|
|
|
|
def visible_menus():
|
|
return [m for m in Desktop(backend='uia').windows(control_type='Menu') if m.is_visible()]
|
|
|
|
|
|
def dump(menus):
|
|
out = []
|
|
for menu in menus:
|
|
items = []
|
|
for entry in menu.descendants(control_type='MenuItem'):
|
|
try:
|
|
items.append({'name': entry.window_text(),
|
|
'automation_id': str(entry.element_info.automation_id or ''),
|
|
'enabled': bool(entry.is_enabled())})
|
|
except Exception:
|
|
items.append({'name': '(stale)', 'automation_id': '', 'enabled': False})
|
|
try:
|
|
hwnd = int(getattr(menu, 'handle', 0) or 0)
|
|
except Exception:
|
|
hwnd = 0
|
|
out.append({'hwnd': hwnd, 'items': items})
|
|
return out
|
|
|
|
|
|
def menu_hwnds():
|
|
found = []
|
|
def visit(h, _):
|
|
if win32gui.GetClassName(h) == '#32768':
|
|
tid_ = win32process.GetWindowThreadProcessId(h)[1]
|
|
if tid_ == pid:
|
|
found.append(h)
|
|
return True
|
|
win32gui.EnumWindows(visit, None)
|
|
return found
|
|
|
|
|
|
def dismiss():
|
|
for _ in range(12):
|
|
handles = menu_hwnds()
|
|
if not handles:
|
|
return True
|
|
for h in handles:
|
|
win32gui.PostMessage(h, win32con.WM_KEYDOWN, win32con.VK_ESCAPE, 0)
|
|
win32gui.PostMessage(h, win32con.WM_KEYUP, win32con.VK_ESCAPE, 0)
|
|
win32gui.PostMessage(h, win32con.WM_CANCELMODE, 0, 0)
|
|
time.sleep(.4)
|
|
return not menu_hwnds()
|
|
|
|
|
|
def mouse_messages(send):
|
|
lp = ((client_xy[1] & 0xffff) << 16) | (client_xy[0] & 0xffff)
|
|
deliver = win32gui.SendMessage if send else win32gui.PostMessage
|
|
deliver(tree.handle, win32con.WM_MOUSEMOVE, 0, lp)
|
|
deliver(tree.handle, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, lp)
|
|
deliver(tree.handle, win32con.WM_RBUTTONUP, 0, lp)
|
|
|
|
|
|
lp_screen = ((screen_xy[1] & 0xffff) << 16) | (screen_xy[0] & 0xffff)
|
|
frame = lm.handle
|
|
variants = [
|
|
('posted-mouse-client', lambda: mouse_messages(False)),
|
|
('posted-contextmenu-tree', lambda: win32gui.PostMessage(tree.handle, win32con.WM_CONTEXTMENU, tree.handle, lp_screen)),
|
|
('sent-mouse-client', lambda: mouse_messages(True)),
|
|
('posted-contextmenu-frame', lambda: win32gui.PostMessage(frame, win32con.WM_CONTEXTMENU, frame, lp_screen)),
|
|
]
|
|
|
|
results = []
|
|
for label, trigger in variants:
|
|
step(f'variant {label}: dismissing leftovers')
|
|
if not dismiss():
|
|
results.append({'variant': label, 'error': 'previous menu could not be dismissed'})
|
|
break
|
|
step(f'variant {label}: triggering')
|
|
try:
|
|
trigger()
|
|
except Exception as error:
|
|
results.append({'variant': label, 'error': f'trigger failed: {error}'})
|
|
continue
|
|
time.sleep(.8)
|
|
step(f'variant {label}: enumerating menus')
|
|
menus = visible_menus()
|
|
step(f'variant {label}: dumping {len(menus)} menu(s)')
|
|
results.append({'variant': label, 'menus': dump(menus)})
|
|
step(f'{label} -> ' + json.dumps(results[-1], ensure_ascii=False)[:300])
|
|
OUTPUT.write_text(json.dumps({'library': str(LIBRARY), 'block': NAME,
|
|
'screen': screen_xy, 'client': client_xy,
|
|
'variants': results}, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
|
|
dismiss()
|
|
OUTPUT.write_text(json.dumps({'library': str(LIBRARY), 'block': NAME,
|
|
'screen': screen_xy, 'client': client_xy,
|
|
'variants': results}, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
print('written', OUTPUT, flush=True)
|
|
try:
|
|
win32gui.PostMessage(frame, win32con.WM_CLOSE, 0, 0)
|
|
except Exception:
|
|
pass
|
|
time.sleep(2)
|
|
subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True)
|