Code: Select all
WinActive("ahk_class CalcFrame")
Code: Select all
ahk_class ApplicationFrameWindow
ahk_exe ApplicationFrameHost.exe
Code: Select all
WinActive("ahk_class CalcFrame")
Code: Select all
ahk_class ApplicationFrameWindow
ahk_exe ApplicationFrameHost.exe
Code: Select all
#b::
isCalculatorActive()
return
isCalculatorActive() {
Process, Exist, Calc.exe
cpid := ErrorLevel
calcFound := false
if (cpid > 0) {
calcFound := true
if (WinActive("ahk_class CalcFrame")) {
message("Calculator (before Win10) is active...")
}
else {
message("Calculator (before Win10) is running but not active...")
}
}
else {
Process, Exist, Calculator.exe
cpid := ErrorLevel
if (cpid > 0) {
calcFound := true
if (WinActive("ahk_pid " cpid)) {
message("Calculator (Win10+) is active...")
}
else {
message("Calculator (Win10+) is running but not active...")
}
}
}
if (!calcFound) {
message("Calculator is not running or active...")
}
}
Code: Select all
#b::
isCalculatorActive()
return
isCalculatorActive() {
Process, Exist, Calc.exe
calcPid := ErrorLevel
calcFound := false
if (calcPid > 0) {
calcFound := true
if (WinActive("ahk_class CalcFrame")) {
message("Calculator (before Win10) is active...")
}
else {
message("Calculator (before Win10) is running but not active...")
}
}
else {
Process, Exist, Calculator.exe
calcPid := ErrorLevel
if (calcPid > 0) {
calcFound := true
; this is returning the PID for ApplicationFrameHost.exe,
; but I am expecting the PID for Calculator.exe
; see - http://stackoverflow.com/questions/32360149/name-of-process-for-active-window-in-windows-8-10
WinGet, curPid, pid, A
message("calcPid: " . calcPid . "`ncurPid: " . curPid)
if (WinActive("ahk_pid " . calcPid)) {
message("Calculator (Win10+) is active...")
}
else {
message("Calculator (Win10+) is running but not active...")
}
}
}
if (!calcFound) {
message("Calculator is not running or active...")
}
}
message(msg) {
MsgBox % msg
}
Code: Select all
MsgBox % getActiveProcess()
getActiveProcess() {
handle := DllCall("GetForegroundWindow", "Ptr")
DllCall("GetWindowThreadProcessId", "Int", handle, "int*", pid)
WinGet, name, ProcessName, ahk_pid %pid%
return name
}
Code: Select all
#s::MsgBox % getActiveProcess()
getActiveProcess() {
handle := DllCall("GetForegroundWindow", "Ptr")
DllCall("GetWindowThreadProcessId", "Int", handle, "int*", pid)
global true_pid := pid
callback := RegisterCallback("enumChildCallback")
DllCall("EnumChildWindows", "Int", handle, "ptr", callback, "int", pid)
WinGet, name, ProcessName, ahk_pid %true_pid%
return name
}
enumChildCallback(hwnd, pid) {
DllCall("GetWindowThreadProcessId", "Int", hwnd, "int*", child_pid)
msgbox % child_pid " " pid
if (child_pid != pid)
global true_pid := child_pid
return 1
}
Code: Select all
DetectHiddenWindows, on
;#s::MsgBox % getActiveProcess() ; one example of use
;"full" example use
#s::
IfWinExist, ahk_exe calculator.exe
if (getActiveProcess() = "calculator.exe")
MsgBox Calc active
else
MsgBox Calc open, not active
else
MsgBox Calc not open
return
getActiveProcess() {
handle := DllCall("GetForegroundWindow", "Ptr")
DllCall("GetWindowThreadProcessId", "Int", handle, "int*", pid)
global true_pid := pid
callback := RegisterCallback("enumChildCallback", "Fast")
DllCall("EnumChildWindows", "Int", handle, "ptr", callback, "int", pid)
handle := DllCall("OpenProcess", "Int", 0x0400, "Int", 0, "Int", true_pid)
length := 259 ;max path length in windows
VarSetCapacity(name, length)
DllCall("QueryFullProcessImageName", "Int", handle, "Int", 0, "Ptr", &name, "int*", length)
SplitPath, name, pname
return pname
}
enumChildCallback(hwnd, pid) {
DllCall("GetWindowThreadProcessId", "Int", hwnd, "int*", child_pid)
if (child_pid != pid)
global true_pid := child_pid
return 1
}
Code: Select all
IfWinExist ahk_exe calculator.exe
Msgbox Ok
Try detecting if it is active. (Assuming you're on Windows 10).CodeKiller wrote:I don't really understand this whole post...
This always works...Code: Select all
IfWinExist ahk_exe calculator.exe Msgbox Ok
In my task manager calculator is... calculator.exe...
Even for that though, your code doesn't work It needs DetectHiddenWindows, on.CodeKiller wrote:Ok sorry, miss the "activate" part in your problem and focus on "detect if the windows exist".
Code: Select all
WinGetActiveProcessName() {
WinGet name, ProcessName, A
if (name = "ApplicationFrameHost.exe") {
ControlGet hwnd, Hwnd,, Windows.UI.Core.CoreWindow1, A
if hwnd {
WinGet name, ProcessName, ahk_id %hwnd%
}
}
return name
}
Code: Select all
SifJar: 1.012600
just me: 2.156200
Lexkios: 0.793800
Code: Select all
SifJar: 0.615600
just me: 0.668800
Lexkios: 0.106200
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#s::
start := A_TickCount
loop, 5000
SifJar()
SifJarTime := (A_TickCount - start)/5000
start := A_TickCount
loop, 5000
just_me()
justmeTime := (A_TickCount - start)/5000
start := A_TickCount
loop, 5000
lexikos()
lexikosTime := (A_TickCount - start)/5000
times := "SifJar: " . SifJarTime . "`r`njust me: " . justmeTime . "`r`nLexkios: " . lexikosTime
clipboard := times
MsgBox % times
SifJar() {
handle := DllCall("GetForegroundWindow", "Ptr")
DllCall("GetWindowThreadProcessId", "Int", handle, "int*", pid)
global true_pid := pid
callback := RegisterCallback("enumChildCallback", "Fast")
DllCall("EnumChildWindows", "Int", handle, "ptr", callback, "int", pid)
handle := DllCall("OpenProcess", "Int", 0x0400, "Int", 0, "Int", true_pid)
length := 259 ;max path length in windows
VarSetCapacity(name, length)
DllCall("QueryFullProcessImageName", "Int", handle, "Int", 0, "Ptr", &name, "int*", length)
SplitPath, name, pname
return pname
}
enumChildCallback(hwnd, pid) {
DllCall("GetWindowThreadProcessId", "Int", hwnd, "int*", child_pid)
if (child_pid != pid)
global true_pid := child_pid
return 1
}
Lexikos() {
WinGet name, ProcessName, A
if (name = "ApplicationFrameHost.exe") {
ControlGet hwnd, Hwnd,, Windows.UI.Core.CoreWindow1, A
if hwnd {
WinGet name, ProcessName, ahk_id %hwnd%
}
}
return name
}
just_me() {
If !WinExist("A")
Return ""
WinGet, ActivePID, PID
WinGet, CtrlList, ControlListHwnd
Loop, Parse, CtrlList, `n
{
WinGet, ChildPID, PID, ahk_id %A_LoopField%
If (ChildPID <> ActivePID) {
ActivePID := ChildPID
Break
}
}
DHW := A_DetectHiddenWindows
DetectHiddenWindows, On
WinGet, ProcName, ProcessName, ahk_pid %ActivePID%
DetectHiddenWindows, %DHW%
Return ProcName
}
Yup, mid range laptop from ~5 years agojust me wrote:Seemingly you run the benchmark on a relative slow machine.
Which makes sense A little bit more overhead in a command is worth making the commands easier than DllCalls IMOlexikos wrote:The two scripts are not doing the same things. ControlGet has to retrieve the class name and track the number of controls of each class that it has encountered to find the one I specified, whereas SifJar's code just looks for a control with a different PID. In general, I suppose that some of the built-in commands do more work than strictly necessary to make them easier to use.
Code: Select all
DetectCalculator() {
calc_id := WinExist("Calculator ahk_class ApplicationFrameWindow")
if !(calc_id) {
MsgBox, % "Calculator is not running."
} else {
if WinActive(ahk_id %calc_id%) {
MsgBox, % "Calculator is active."
} else {
MsgBox, % "Calculator is running but not active."
}
}
}
Code: Select all
;Launch_App2 is Calculator Key
Launch_App2:: ActivateCalculator()
ActivateCalculator() {
if WinExist("Calculator ahk_class ApplicationFrameWindow") {
WinActivate
} else {
Run, calc
}
}
Code: Select all
ActivateCalculator() {
IfWinExist Calculator
WinActivate
else
Run, calc
}
Code: Select all
#numpad0:: ; <-- Open/Activate/Minimize Windows Calculator
{
if WinExist("Calculator ahk_class CalcFrame") or WinExist("Calculator ahk_class ApplicationFrameWindow")
if WinActive()
WinMinimize
else
WinActivate
else
Run calc.exe
return
}
Users browsing this forum: No registered users and 351 guests