Detecting When Any New Window Is Created or Displayed?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Barfunkle
Posts: 1
Joined: 04 Jun 2023, 16:06

Detecting When Any New Window Is Created or Displayed?

Post by Barfunkle » 04 Jun 2023, 16:17

I'm trying to make a script that will automatically center a window once it is "created" or displayed on screen. However, I'm having a difficult time finding a method that allows me to detect such an event. The method I'm currently using is to just call my CenterWindow() function anytime the active window changes. This works, however since it always calls CenterWindow() whenever the active window changes, it will repeatedly center already opened windows. For example: I open "File Explorer" -> The window gets centered. Now, I open "Command Prompt" -> The window gets centered. I move either the File Explorer window or the Command Prompt window to another area on the screen. When I switch focus back to either window, they get centered again. How can modify my script so that ONLY windows that are actually "created" or freshly displayed will get centered? I'm aware that I should probably use a different approach for detecting new windows, so could anyone help?

Here is my current script:

Code: Select all

#Persistent

OnWinActiveChange(hWinEventHook, vEvent, hWnd)

{
    static _ := DllCall("user32\SetWinEventHook", UInt, 0x3, UInt, 0x3, Ptr, 0, Ptr, RegisterCallback("OnWinActiveChange"), UInt, 0, UInt, 0, UInt, 0, Ptr)
    DetectHiddenWindows, On
    WinGetClass, vWinClass, % "ahk_id " hWnd
    
    CenterActiveWindow()
}

CenterActiveWindow()
{
    winHandle := WinExist("A")

    VarSetCapacity(monitorInfo, 40), NumPut(40, monitorInfo)
    monitorHandle := DllCall("MonitorFromWindow", "Ptr", winHandle, "UInt", 0x2)
    DllCall("GetMonitorInfo", "Ptr", monitorHandle, "Ptr", &monitorInfo)

    workLeft      := NumGet(monitorInfo, 20, "Int")
    workTop       := NumGet(monitorInfo, 24, "Int")
    workRight     := NumGet(monitorInfo, 28, "Int")
    workBottom    := NumGet(monitorInfo, 32, "Int")

    WinGetPos,,, W, H, A
    WinGet, Style, Style, A

    if (Style & 0x80000)
    {
        WinMove, A,, workLeft + (workRight - workLeft) // 2 - W // 2, workTop + (workBottom - workTop) // 2 - H // 2
    }
}

OnWinActiveChange(0, 0, 0)

Loop
    Sleep, 100

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Detecting When Any New Window Is Created or Displayed?

Post by mikeyww » 04 Jun 2023, 18:01

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33
Global center := "chrome"
DllCall("RegisterShellHookWindow", "UInt", A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "winCenter")
Return

winCenter(wParam, lParam) {
 If (wParam != WINDOWCREATED := 1)
  Return
 WinGet pname, ProcessName, % winTitle := "ahk_id" lParam
 SplitPath pname,,,, fnBare
 Sleep 50
 If (fnBare = center)
  MsgBox 262208, Status, % fnBare
}


Post Reply

Return to “Ask for Help (v1)”