Jump to content

Baffled by variable usage


qwert
 Share

Recommended Posts

This script is on the forums as a way to modify the style of a window (and it works fine). But can someone please explain how the variable $Style is being used? It seems to have two -- or even three -- entirely different usages.

;The function WinSetStyle() 
can set styles not only for AutoIt GUI, and for other windows too.
#include <GuiConstants.au3>
$NewStyle = False
$Gui = GUICreate("Gui Style", 260, 100)
$Style = GUICtrlCreateButton("Set Style", 45, 50, 150, 20)
GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case - 3
            Exit
        Case $Style
            If Not $NewStyle Then
                WinSetStyle($Gui, $WS_POPUPWINDOW + $WS_THICKFRAME, $WS_EX_CLIENTEDGE + $WS_EX_TOOLWINDOW)
                GUICtrlSetData($Style, 'Undo Style')
                $NewStyle = True
            Else
                WinSetStyle($Gui)
                GUICtrlSetData($Style, 'Set Style')
                $NewStyle = False
            EndIf
    EndSwitch
WEnd

;Set Window Styles
Func WinSetStyle($hWnd, $Style = -1, $ExStyle = 0)
    Local Const $GWL_STYLE = -16
    Local Const $GWL_EXSTYLE = -20
    Local Const $SWP_NOMOVE = 0x2
    Local Const $SWP_NOSIZE = 0x1
    Local Const $SWP_SHOWWINDOW = 0x40
    Local Const $SWP_NOZORDER = 0x4
    Local $iFlags = BitOR($SWP_SHOWWINDOW, $SWP_NOSIZE, $SWP_NOMOVE, $SWP_NOZORDER)
    If $Style = -1 Then $Style = $WS_MINIMIZEBOX + $WS_CAPTION + $WS_POPUP + $WS_SYSMENU
    DllCall("User32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "int", $Style)
    DllCall("User32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_EXSTYLE, "int", $ExStyle)
    DllCall("User32.dll", "int", "SetWindowPos", "hwnd", $hWnd, "hwnd", 0, "int", 0, "int", 0, _
            "int", 0, "int", 0, "int", $iFlags)
EndFunc

Thanks for any help.

Link to comment
Share on other sites

This script is on the forums as a way to modify the style of a window (and it works fine). But can someone please explain how the variable $Style is being used? It seems to have two -- or even three -- entirely different usages.

Thanks for any help.

Confusing example. The $Style variable in the GuiGetMsg() loop has nothing to do with the $Style variable inside the WinSetStyle() function. The first is a Global scope variable used to hold the control ID of the GUI button. The second is a Local scope variable used only internally to the WinSetStyle() function to represent the actual 32bit numeric style parameter. This has the Control ID variable renamed for clarity:

;The function WinSetStyle() can set styles not only for AutoIt GUI, and for other windows too.
#include <GuiConstants.au3>
$NewStyle = False
$Gui = GUICreate("Gui Style", 260, 100)
$StyleButton = GUICtrlCreateButton("Set Style", 45, 50, 150, 20)
GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case - 3
            Exit
        Case $StyleButton
            If Not $NewStyle Then
                WinSetStyle($Gui, $WS_POPUPWINDOW + $WS_THICKFRAME, $WS_EX_CLIENTEDGE + $WS_EX_TOOLWINDOW)
                GUICtrlSetData($StyleButton, 'Undo Style')
                $NewStyle = True
            Else
                WinSetStyle($Gui)
                GUICtrlSetData($StyleButton, 'Set Style')
                $NewStyle = False
            EndIf
    EndSwitch
WEnd

;Set Window Styles
Func WinSetStyle($hWnd, $Style = -1, $ExStyle = 0)
    Local Const $GWL_STYLE = -16
    Local Const $GWL_EXSTYLE = -20
    Local Const $SWP_NOMOVE = 0x2
    Local Const $SWP_NOSIZE = 0x1
    Local Const $SWP_SHOWWINDOW = 0x40
    Local Const $SWP_NOZORDER = 0x4
    Local $iFlags = BitOR($SWP_SHOWWINDOW, $SWP_NOSIZE, $SWP_NOMOVE, $SWP_NOZORDER)
    If $Style = -1 Then $Style = $WS_MINIMIZEBOX + $WS_CAPTION + $WS_POPUP + $WS_SYSMENU
    DllCall("User32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "int", $Style)
    DllCall("User32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_EXSTYLE, "int", $ExStyle)
    DllCall("User32.dll", "int", "SetWindowPos", "hwnd", $hWnd, "hwnd", 0, "int", 0, "int", 0, _
            "int", 0, "int", 0, "int", $iFlags)
EndFunc

Hope that helps!

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

That explains everything I was confused about.

So can global variables be referenced inside a function? -- or are the two usages mutually exclusive?

Thanks very much for your response.

Yes, global variables can be accessed in a function - hence the name. By default, variables declared inside a function have a local scope - only accessible inside that same function.

I think reading the Variable Documentation will also clear things up a bit for you. Good luck :P

Link to comment
Share on other sites

Actually, I had read most of the section of variables, but it was this sentence that seemed to say they were the same:

By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name

The unless threw me.

Anyway, thanks for the clarification.

Link to comment
Share on other sites

Actually, I had read most of the section of variables, but it was this sentence that seemed to say they were the same:

The unless threw me.

Anyway, thanks for the clarification.

The unless means if you have declared a global variable $Count and set it to 5, then call a function which sets $Count to 3, the GLOBAL $Count will be changed to 3, you do not create a LOCAL $Count variable. Ofcourse you can do this by placing Local in front of the $Count in the function :P But I wouldn't recommend using the same var name in different scopes.
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...