Jump to content

Recommended Posts

Posted (edited)

I'm building a file renaming tool that I want to be able to resize and when you exit the program, it writes the size and position of the GUI to an ini so the next time you open it, it will be the same as when you closed it. However, on my computer, WinGetClientSize is reporting a value of 33 pixels less than the actual client size. The most simple test is to run the script and close the gui without moving it. Then compare the default ini read value of "723|360|-1|-1" to the one written to an ini, which ends up on my computer as "723|327|597|360". I could just add 33 to the value, but I'm afraid different versions of Windows, screen resolutions and magnification settings on Windows won't always be exactly 33 pixels and every time someone opens the program, it would slowly shrink the size of the window and cover up items at the bottom of the GUI. Below is a simplified version of the script. Any thoughts would be appreciated.

Thanks!

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Static $staIniFile = "Settings.ini"
Static $staWinTitle = "My GUI"

$varScreenSize = IniRead($staIniFile, "Preferences", "GuiSize", "723|360|-1|-1")
$varScreenSize = StringSplit($varScreenSize, "|", 2)

$guiMainForm = GUICreate($staWinTitle, $varScreenSize[0], $varScreenSize[1], $varScreenSize[2], $varScreenSize[3])
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                $varGuiSize = WinGetClientSize($guiMainForm)
                $varGuiPos = WinGetPos($staWinTitle)
                IniWrite($staIniFile, "Preferences", "GuiSize", $varGuiSize[0] & "|" & $varGuiPos[1] & "|" & $varGuiPos[0] & "|" & $varGuiSize[1])
                Exit
        EndSwitch
WEnd

 

Edited by abberration
Posted

Just as an FYI, don't use Static for Global variables.

Also, if you want the size of the window itself, don't use WinGetClientSize, WinGetPos gives you the width and height, the client size doesn't take into account the title bar size and frame of the GUI.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted (edited)

BrewManNH, thanks for the suggestions. I will look further into static vs global a bit more for understanding. I know static is for things like pi being 3.14, so I assumed that since I wanted my gui's title to not change, I could define it as static.

I had problems with just using WinGetPos because the sizes reported didn't reflect the size needed to create the gui when the script was started the next time. The original code worked when I made that change in my second post. But now I have a new problem, which is related to the original problem. I changed the status of this thread from being solved back to unsolved. This code works perfectly:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Static $staIniFile = "Settings.ini"
Static $staWinTitle = "My GUI"

$varScreenSize = IniRead($staIniFile, "Preferences", "GuiSize", "723|360|-1|-1")
$varScreenSize = StringSplit($varScreenSize, "|", 2)

$guiMainForm = GUICreate($staWinTitle, $varScreenSize[0], $varScreenSize[1], $varScreenSize[2], $varScreenSize[3])
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                $varGuiSize = WinGetClientSize($guiMainForm)
                $varGuiPos = WinGetPos($staWinTitle)
                IniWrite($staIniFile, "Preferences", "GuiSize", $varGuiSize[0] & "|" & $varGuiSize[1] & "|" & $varGuiPos[0] & "|" & $varGuiSize[1])
                Exit
        EndSwitch
WEnd

I now have a problem with adding $WS_SIZEBOX to the GUICreate line (which isn't in the above code) changing the reported size of the WinGetClientSize. When I insert the $WS_SIZEBOX into the code, the width/height go from 723/360 to 721/336.  I'm using WinGetClientSize to define the size of the GUI and WinGetPos to put it in the right position on the screen. This combination works in the code above before enabling resizing, but not afterwards.

Again, I do appreciate your feedback. Any further advice will definitely be appreciated.

Edited by abberration
Posted

You want to use Const not Static, static is only used inside functions and never in the Global scope.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

I will look further into the definitions of the variables, but my new problem is between these two codes:

Works:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Static $staIniFile = "Settings.ini"
Static $staWinTitle = "My GUI"

$varScreenSize = IniRead($staIniFile, "Preferences", "GuiSize", "723|360|-1|-1")
$varScreenSize = StringSplit($varScreenSize, "|", 2)

$guiMainForm = GUICreate($staWinTitle, $varScreenSize[0], $varScreenSize[1], $varScreenSize[2], $varScreenSize[3])
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                $varGuiSize = WinGetClientSize($guiMainForm)
                $varGuiPos = WinGetPos($staWinTitle)
                IniWrite($staIniFile, "Preferences", "GuiSize", $varGuiSize[0] & "|" & $varGuiSize[1] & "|" & $varGuiPos[0] & "|" & $varGuiSize[1])
                Exit
        EndSwitch
WEnd

Does not work:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Static $staIniFile = "Settings.ini"
Static $staWinTitle = "My GUI"

$varScreenSize = IniRead($staIniFile, "Preferences", "GuiSize", "723|360|-1|-1")
$varScreenSize = StringSplit($varScreenSize, "|", 2)

$guiMainForm = GUICreate($staWinTitle, $varScreenSize[0], $varScreenSize[1], $varScreenSize[2], $varScreenSize[3], $WS_SIZEBOX)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                $varGuiSize = WinGetClientSize($guiMainForm)
                $varGuiPos = WinGetPos($staWinTitle)
                IniWrite($staIniFile, "Preferences", "GuiSize", $varGuiSize[0] & "|" & $varGuiSize[1] & "|" & $varGuiPos[0] & "|" & $varGuiSize[1])
                Exit
        EndSwitch
WEnd

My new problem is in line 10 with the addition of $WS_SIZEBOX. The ini files created by both show different results on what is recorded on the ini files but the code is identical except for adding $WS_SIZEBOX to the second code. Could this be a constant difference? Can I create a formula to apply a ratio to make the difference into a percentage for resizing? Is there a UDF that could help make this job easier? If I can't find a way, I might give up on remembering a person's last preference. Not the end of the world. I just thought it would be a nice feature to implement.

 

Posted
3 hours ago, BrewManNH said:

Also, if you want the size of the window itself, don't use WinGetClientSize, WinGetPos gives you the width and height, the client size doesn't take into account the title bar size and frame of the GUI.

I'll repeat what I said above so that maybe you'll notice it this time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

Thanks, BrewManNH. WinGetPos is reporting the correct size of the gui. I mixed up the parameters and was looking at x and y position instead of the width and height.

I have a new little issue. I will try my best to figure it out on my own. I won't keep beating this dead horse if I need help again. I will start a new thread.

Thanks again for your suggestions. Cheers!

Posted

Just figured out my new issue. It was with WinMove. Reading the word 'default' from an ini didn't translate into the keyword default to signify to use the default setting. Therefore, I wrote an if/then/else statement that if default is read then I wrote the line of code with the word default for the x and y coordinates in the actual line of the code and if default was not read but instead is a number, then to just use the ini read value. Works perfectly now. Now I can move on to other things in the program!

Cheers! :drinks:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...