Jump to content

Dynamic variables


MightyWeird
 Share

Recommended Posts

Depending on the current resolution of the user I would like to define a variable

I came up with this, but this does not seem to work.

The variable only gives me a value of 1 (consolewrite $dheight) for example.

I think the problem lies with eval(). I can only put strings (or text?), but not numbers (like @desktopheight - 200)

Is there another solution to this problem?

 

 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <ColorConstantS.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>

GLOBAL $x = @DesktopWidth

Switch @DesktopWidth
    Case "1920"
        consolewrite("case werkt 1920")
        ;assign("dwidth",1920 / 2 - 960,2)
        assign("dwidth",$x,2)
        assign("dwidth1",1220,2)
        assign("dwidth2",200,2)
        assign("dwidth3",935,2)
        assign("dwidth4",1920 / 4,2)
        assign("picheight",300,2)
        assign("Picwidth",1820,2)
        assign("Picstartleft",1920 / 2 - 1820 / 2,2)

EndSwitch

Switch @DesktopHeight
    Case "1080"
        assign("dheight",1080 - 430,2)
        ;ConsoleWrite('1080' & @CR)
    Case "900"
        assign("dheight",1080 - 200,2)
        ;Global $dheight = @DesktopHeight - 200
    Case "768"
        assign("dheight",1080 - 200,2)
        ;Global $dheight = @DesktopHeight - 200
    Case else
        assign("dheight",1080 - 200,2)
EndSwitch

        Global $dwidth = Eval("dwidth")
        Global $dwidth1 = Eval("dwidth1")
        Global $dwidth2 = Eval("dwidth2")
        Global $dwidth3 = Eval("dwidth3")
        Global $dwidth4 = Eval("dwidth4")
        GLOBAL $picheight = Eval("picheight")
        GLOBAL $Picwidth = Eval("Picwidth")
        GLOBAL $Picstartleft = Eval("Picstartleft")

        Global $dheight = Eval("dheight")
        If Not IsDeclared("dwidth") Then
        MsgBox($MB_SYSTEMMODAL, "", "The variable dwidth is not declared.")
        endif

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

Example()

Func Example()
    Local $sFilePath = "..\GUI\logo_autoit_210x72.gif"

    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 1080, 1920)
    GUICtrlCreatePic("..\GUI\msoobe.jpg", 0, 0, 400, 100)
    consolewrite ($dheight)
    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    Local $hChild = GUICreate("", 210, 72, 20, 15, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $hGUI)

    ; Create a picture control with a transparent image.
    GUICtrlCreatePic($sFilePath, 0, 0, 210, 72)

    ; Display the child GUI.
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUIs and all controls.
    GUIDelete($hGUI)
    GUIDelete($hChild)
EndFunc   ;==>Example

 

 

My previous code I did it like this (code below). But then I run into other problems.

 

If @DesktopWidth = 1920 Then
    Global $dwidth = @DesktopWidth / 2 - 960

 

Link to comment
Share on other sites

Why use Assign at all? With the code you posted there's absolutely no reason to go that route. Just use something like this:

Global $x = @DesktopWidth, $dwidth, $dwidth2, $dwidth3, $dwidth4, $picheight, $Picwidth, $Picstartleft

Switch @DesktopWidth
     Case "1920"
          ConsoleWrite("case werkt 1920")
          ;assign("dwidth",1920 / 2 - 960,2)
          $dwidth = $x
          $dwidth1 = 1220
          $dwidth2 = 200
          $dwidth3 = 935
          $dwidth4 = 1920 / 4
          $picheight = 300
          $Picwidth = 1820
          $Picstartleft = 1920 / 2 - 1820 / 2

You don't need to use Assign, all of your variables aren't "dynamic" they're just regular variables with "dynamic" values assigned to them. The variables themselves aren't changing name based on something, just the values assigned to them.

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

Link to comment
Share on other sites

Quote

You don't need to use Assign, all of your variables aren't "dynamic" they're just regular variables with "dynamic" values assigned to them. The variables themselves aren't changing name based on something, just the values assigned to them.

Thank you, I will try this out.

Still have a question.. I just don't understand the first line.

Quote
Global $x = @DesktopWidth, $dwidth, $dwidth2, $dwidth3, $dwidth4, $picheight, $Picwidth, $Picstartleft

I dont understand how this line works....Why does it needs to starts with $x =

 

 

 

 

 

Edited by MightyWeird
Link to comment
Share on other sites

  • Moderators

He is defining a number of variables in one line. $x is a variable that holds the @DesktopWidth as its value. The other variables are empty at the moment.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...