Restore position of minimized window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Restore position of minimized window

14 Jul 2018, 13:51

I'm trying to understand is there a way to restore minimized window in the same place where it was located before minimizing.

Code: Select all

#SingleInstance, Force

min(a, b) {
    Return, a < b ? a : b
}

max(a, b) {
    Return, a > b ? a : b
}

SysGet, MWA_, MonitorWorkArea
IniRead, GuiPositionX, Test.ini, IniSection_Main, IniKey_GuiPositionX
IniRead, GuiPositionY, Test.ini, IniSection_Main, IniKey_GuiPositionY
Gui, Show, w300 h200 Hide
Gui, +HwndGuiId +LastFound
WinGetPos,,, W, H
FinalX := max(MWA_Left, min(GuiPositionX, MWA_Right - W))
FinalY := max(MWA_Top, min(GuiPositionY, MWA_Bottom - H))
If (GuiPositionX != "ERROR" and GuiPositionY != "ERROR")
    Gui, Show, x%FinalX% y%FinalY%
Else
    Gui, Show, Center
Return

GuiClose:
    If !Minimized
    {
        WinGetPos, GuiX, GuiY,,, ahk_id %GuiId%
        IniWrite, %GuiX%, Test.ini, IniSection_Main, IniKey_GuiPositionX
        IniWrite, %GuiY%, Test.ini, IniSection_Main, IniKey_GuiPositionY
    }
ExitApp

GuiSize:
    If A_EventInfo = 1
        Minimized := True
Return
As you can see, it works very well. Try to move the window somewhere, then close it and then launch script again. The window will be reopened in the same position, as it should.

However, there exists one case, where it doesn't properly work. Launch the script, move the window somewhere from it's initial position, then minimize it and then close it using taskbar (without restoring). After this, you will see that window restored it's initial position, instead of second position, from which it was minimized.

So, obviously I want to fix it. In such case, the window should be restored to second position (just like Notepad or browser window). Is there way to fix it?
Last edited by john_c on 15 Jul 2018, 18:27, edited 2 times in total.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Restore position of minimized window

14 Jul 2018, 18:16

You will need these two functions:

Code: Select all

GetWindowPlacement(hWnd) {
    NumPut(VarSetCapacity(WINDOWPLACEMENT, 44, 0), WINDOWPLACEMENT, 0, "UInt")
    DllCall("GetWindowPlacement", "Ptr", hWnd, "Ptr", &WINDOWPLACEMENT)
    Result := {}
    Result.x := NumGet(WINDOWPLACEMENT, 28, "Int")
    Result.y := NumGet(WINDOWPLACEMENT, 32, "Int")
    Result.w := NumGet(WINDOWPLACEMENT, 36, "Int") - Result.x
    Result.h := NumGet(WINDOWPLACEMENT, 40, "Int") - Result.y
    Result.showCmd := NumGet(WINDOWPLACEMENT, 8, "UInt") ; 1 = normal, 2 = minimized, 3 = maximized
    Return Result
}

SetWindowPlacement(hWnd, x, y, w, h, showCmd) {
    NumPut(VarSetCapacity(WINDOWPLACEMENT, 44, 0), WINDOWPLACEMENT, 0, "UInt")
    NumPut(x, WINDOWPLACEMENT, 28, "Int")
    NumPut(y, WINDOWPLACEMENT, 32, "Int")
    NumPut(w + x, WINDOWPLACEMENT, 36, "Int")
    NumPut(h + y, WINDOWPLACEMENT, 40, "Int")
    NumPut(showCmd, WINDOWPLACEMENT, 8, "UInt")
    Return DllCall("SetWindowPlacement", "Ptr", hWnd, "ptr", &WINDOWPLACEMENT)
}
Storing the window position in the ini file:

Code: Select all

Pos := GetWindowPlacement(hWnd)
IniWrite % Pos.x, %IniFile%, Position, X
IniWrite % Pos.y, %IniFile%, Position, Y
IniWrite % Pos.w, %IniFile%, Position, Width
IniWrite % Pos.h, %IniFile%, Position, Height
IniWrite % Pos.showCmd, %IniFile%, Position, State
Restoring the window position:

Code: Select all

IniRead X, %IniFile%, Position, X
IniRead Y, %IniFile%, Position, Y
IniRead W, %IniFile%, Position, Width, 952
IniRead H, %IniFile%, Position, Height, 611
IniRead State, %IniFile%, Position, State, 1

If (FileExist(IniFile)) {
    SetWindowPlacement(hWnd, X, Y, W, H, State)
} Else {
    Gui Show, w%W% h%H%
}
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Restore position of minimized window

15 Jul 2018, 03:24

Alguimist wrote:...
Thanks! Could you please show how to add it to original script? I tried, but currently it doesn't properly work. Of course, I already changed "IniFile" variable to "Test.ini".
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Restore position of minimized window

15 Jul 2018, 15:02

john_c wrote:Could you please show how to add it to original script?

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Global hWnd, IniFile := "Test.ini"

Gui +hWndhWnd +Resize

IniRead X, %IniFile%, Position, X
IniRead Y, %IniFile%, Position, Y
IniRead W, %IniFile%, Position, Width, 952
IniRead H, %IniFile%, Position, Height, 611
IniRead State, %IniFile%, Position, State, 1

If (FileExist(IniFile)) {
    SetWindowPlacement(hWnd, X, Y, W, H, State)
} Else {
    Gui Show, w%W% h%H%
}
Return

GuiEscape:
GuiClose:
    GoSub SaveSettings
    ExitApp

SaveSettings:
    Pos := GetWindowPlacement(hWnd)
    IniWrite % Pos.x, %IniFile%, Position, X
    IniWrite % Pos.y, %IniFile%, Position, Y
    IniWrite % Pos.w, %IniFile%, Position, Width
    IniWrite % Pos.h, %IniFile%, Position, Height
    IniWrite % Pos.showCmd, %IniFile%, Position, State
Return

GetWindowPlacement(hWnd) {
    NumPut(VarSetCapacity(WINDOWPLACEMENT, 44, 0), WINDOWPLACEMENT, 0, "UInt")
    DllCall("GetWindowPlacement", "Ptr", hWnd, "Ptr", &WINDOWPLACEMENT)
    Result := {}
    Result.x := NumGet(WINDOWPLACEMENT, 28, "Int")
    Result.y := NumGet(WINDOWPLACEMENT, 32, "Int")
    Result.w := NumGet(WINDOWPLACEMENT, 36, "Int") - Result.x
    Result.h := NumGet(WINDOWPLACEMENT, 40, "Int") - Result.y
    Result.showCmd := NumGet(WINDOWPLACEMENT, 8, "UInt") ; 1 = normal, 2 = minimized, 3 = maximized
    Return Result
}

SetWindowPlacement(hWnd, x, y, w, h, showCmd) {
    NumPut(VarSetCapacity(WINDOWPLACEMENT, 44, 0), WINDOWPLACEMENT, 0, "UInt")
    NumPut(x, WINDOWPLACEMENT, 28, "Int")
    NumPut(y, WINDOWPLACEMENT, 32, "Int")
    NumPut(w + x, WINDOWPLACEMENT, 36, "Int")
    NumPut(h + y, WINDOWPLACEMENT, 40, "Int")
    NumPut(showCmd, WINDOWPLACEMENT, 8, "UInt")
    Return DllCall("SetWindowPlacement", "Ptr", hWnd, "ptr", &WINDOWPLACEMENT)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], NinjoOnline and 210 guests