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
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?