and place it in same folder as the script
Now try commenting out line 8 (Run Notepad) and then manually run notepad while the script is running - try selecting the menus under "Foo", nothing will happen!
However, the "Foo" menu gets created, so ALL the same code is run as when Run Notepad is not commented out -
This is why it's so strange, literally the only thing different in the two scenarios is one has notepad Run by ahk and one does not, and it completely changes whether the script works or not.. Does anyone know why???
Code: Select all
#SingleInstance Force
#Persistent
#NoEnv
if not A_IsAdmin
Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%"
Run Notepad
SetTimer, CheckForWindow, 100
Return
CheckForWindow:
WinGet, active_processName, ProcessName, A
if (active_processName = "notepad.exe")
{
WinGet, hWnd, ID, A
WinGet, WinTitle, PID, A
WinTitle := "ahk_pid " WinTitle
result := CheckMenu(hWnd)
if (result = 0)
{
MainMenu := DllCall("GetMenu", "UInt", hWnd)
MyMenu := AppendMenu(MainMenu, "&Foo")
AppendMenuItem(MyMenu, 1000, "Apple")
MySubMenu := AppendMenu(MyMenu, "Open Me")
AppendMenuItem(MySubMenu, 1001, "Apple1")
RedrawMenuBar(hWnd)
HookMsg(WinTitle, WM_COMMAND:=0x111, "_Filter")
}
}
Return
_Filter(wParam, lParam, msg, hwnd) {
If (wParam=1000 AND msg = 273)
MsgBox, click on 1000
If (wParam=1001 AND msg = 273)
MsgBox, click on 1001
}
AppendMenu(MenuHandle, MenuName) {
MF_POPUP = 16
SubMenuHandle := DllCall("CreateMenu")
DllCall("AppendMenu", "Ptr", MenuHandle, "UInt", MF_POPUP, "UPtr", SubMenuHandle, "Str", MenuName)
Return SubMenuHandle
}
AppendMenuItem(MenuHandle, ItemID, ItemName) {
DllCall("AppendMenu", "Uint", MenuHandle, "UInt", 0, "Uint", ItemID, "Str", ItemName)
}
RedrawMenuBar(hWnd) {
DllCall("DrawMenuBar", "Ptr", hWnd)
}
CheckMenu(hWnd)
{
menu_hWnd := DllCall("GetMenu", "int", hWnd)
if (DllCall("GetMenuItemCount", "ptr", menu_hWnd) > -1) {
Loop, % DllCall("GetMenuItemCount", "ptr", menu_hWnd)
{
VarSetCapacity(string, 256)
SubMenu_hWnd := DllCall("GetSubMenu", "int", menu_hWnd, "int", A_Index)
string_result := DllCall("GetMenuString" , "int", menu_hWnd, "int", SubMenu_hWnd, "str", string, "int", 128, "int", 0x0000)
if (string = "&Foo")
Return 1
}
Return 0
}
Return 1
}
HookMsg(WinTitle, MsgNumber, Function) {
; hook.dll -- https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=7166
static hModule := DllCall("LoadLibrary", "Str", "hook.dll", "Ptr")
static _ := { base: {__Delete: "HookMsg"} }
static iThreadIdTarget, hWndTarget
If !_ {
DllCall("hook.dll\UnInstallFilterDLL", "Int", iThreadIdTarget, "Ptr", hWndTarget, "Ptr", A_ScriptHwnd)
Return DllCall("FreeLibrary", "Ptr", hModule)
}
hWndTarget := WinExist(WinTitle)
static WH_CALLWNDPROC := 4
, WH_GETMESSAGE := 3
, WM_USER := 1024
, UM_ADDMESSAGE := WM_USER + 0x100
iThreadIdTarget := DllCall("GetWindowThreadProcessId", "Ptr", hWndTarget, "Ptr", 0, "UInt")
hook := DllCall("hook.dll\InstallFilterDLL", "Int", WH_CALLWNDPROC, "Int", iThreadIdTarget, "Ptr", hWndTarget) ; 0 = Ok
hookG := DllCall("hook.dll\InstallFilterDLL", "Int", WH_GETMESSAGE, "Int", iThreadIdTarget, "Ptr", hWndTarget) ; 0 = Ok
DllCall("SendMessage", "Ptr", hWndTarget, "UInt", UM_ADDMESSAGE, "UPtr", MsgNumber, "Ptr", A_ScriptHwnd, "Ptr")
OnMessage(MsgNumber, Function)
_hWndTarget := hWndTarget
}