Preserve Window Position and Size

To preserve window position & size the special values associated with Minimized and Maximized windows must be avoided.

The window size variable StrMainS must be included in the Preserve User Settings Code Snippet.

DoDefaultSettings() includes StrMainS = "200x100" or whatever initial size is required.
DoLoadSettings() includes StrMainS = strLoadLocal("MainS",StrMainS)
DoSaveSettings() includes doSaveLocal("MainS",StrMainS)

Requires: iuplua and Code Snippets Preserve User Settings and Split A String Into Numbers Using Separators.


require "iuplua"	-- To access GUI window builder

-- Flag if GUI Window is Normal rather than Minimised or Maximised --
function IsNormalWindow(dialogGUI)
	-- tblScrn[1] = origin x, tblScrn[2] = origin y, tblScrn[3] = width, tblScrn[4] = height
	local tblPosn = dialogGUI.screenposition:SplitNumbers()
	local intPosX = tblPosn[1]
	local intPosY = tblPosn[2]
	if intPosX < 0 and intPosY < 0 then	-- If origin is negative (-8, -8 = Maximised, -3200, -3200 = Minimised)
		return false			-- then is Maximised or Minimised
	return true
end -- function IsNormalWindow

IntMainX = iup.CENTER
IntMainY = iup.CENTER
StrMainS = "200x100"

local btnClose = iup.button { title = "Close Window", action = function() return iup.CLOSE end }
local dialogue = iup.dialog { title = "Snippet", startfocus = btnClose, rastersize = StrMainS, btnClose,
			move_cb = function(self,x,y) if IsNormalWindow(self) then IntMainX=x IntMainY=y end end,
			resize_cb = function(self) if IsNormalWindow(self) then StrMainS=self.rastersize end end,
			close_cb = btnClose.action,

dialogue.rastersize=nil		-- Allow window to be resized

if (iup.MainLoopLevel()==0) then iup.MainLoop() end
Last update: 16 Dec 2020