Jump to content

Is it possible to not change window's positions when locked and minimized?


Recommended Posts

Hi,

I am referring to code being presented in this thread () that discusses about enabling windows to lock to relative positions when moving around. I am requesting help in these areas:

1. Is it possible for the secondary window to not have both its relative X and Y positions set to the primary window's coordinates, when minimized?

Example code:

Func showBottomForm($userID, $hWindow) ; Bottom form
    $g_hBottomForm = GUICreate("Bottom Form", $CONST_NORMAL_WIDTH, $CONST_NORMAL_HEIGHT, -1, WinGetPos($hWindow)[1] + WinGetPos($hWindow)[3], -1, $WS_EX_TOOLWINDOW)
    $hButton_Form = GUICtrlCreateButton("Bottom Form Button", 80, 10, 120, 30)
    GUISetState()
EndFunc     ;==>showBottomForm

What this means is that if I moved the secondary window around before minimizing the primary window and then re-maximize the primary window again, the secondary window's position could be set correctly.

But if the secondary window is not moved and then the primary window is minimized/re-maximized immediately, the secondary window's position gets locked to the X/Y coordinates of the primary window.

2. Is it possible for the secondary window to be shown together with the primary window when the primary window is re-maximized in the presence of other application windows?

What happens now is that the secondary window (based on above code) could not be seen if there are other windows shown.

But if every window is minimized (except my AutoIt app), then I can see the secondary window. What changes are needed?

I tried the following:

GUICreate("Bottom Form", $CONST_NORMAL_WIDTH, $CONST_NORMAL_HEIGHT, -1, WinGetPos($hWindow)[1] + WinGetPos($hWindow)[3], -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_CLIENTEDGE))

This didn't really cause much change, so I am kinda stumped now. All help appreciated.

Edited by poila
Link to comment
Share on other sites

  • Moderators

poila,

I am afraid I am having difficulty in understanding what you want to do in your first question. Are you asking if it can be arranged so that if the secondary window is moved then it retains the new relative position from then on? :huh:

As to the second question, try using WinActivate on the secondary when the primary is restored. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi,

I think pictures are clearer in explaining my first issue.

* Minimize before restore (windows are not moved):

bo8k.png

* Minimize after restore (windows not moved prior to restore):

3fsa.png

(Secondary window is shown hidden behind primary window)

* Window after restore (windows moved prior to restore):

vd0p.png

 

Code that I used to make secondary window have focus after restore:

#Region Restore Window Functions
Func _WM_EXTRACTOR($hWnd, $iMsg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0x0000FFFF)
    Local $hCtrl = $lParam
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Switch $iMsg
        Case $WM_SYSCOMMAND
            Switch $wParam
                Case $SC_RESTORE
                    ConsoleWrite("!Restore window" & @LF)
        WinActivate($g_hAdminForm) ; g_hAdminForm is the global secondary form
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_EXTRACTOR
#EndRegion

In the main func:

GUIRegisterMsg($WM_ENTERSIZEMOVE, "setrelpos")  ;   Get relative position through function
GUIRegisterMsg($WM_MOVE, "followme")        ;   Once relative position retrieved, perform window movement
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_EXTRACTOR") ;   Detect if the primary window is restored

Symptoms are not elevated after implementing the above code and changes, was something amiss that I need to modify to rectify issue of secondary hiding behind primary?

Link to comment
Share on other sites

  • Moderators

poila,

That is much clearer, thanks. :)

Please post all of your code - or at least all of the code you use to create and link the 2 GUIs. Just a few lines from here and there in the script does not help a lot. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 3 weeks later...

#Region Program Configuration Settings, Constants & Include Files
    AutoItSetOption("TrayIconHide", 0)
    AutoItSetOption("TrayIconDebug", 1)
    AutoItSetOption("GUIResizeMode", 802)
    AutoItSetOption("GUIOnEventMode", 1)
    #include-once
    #include <ButtonConstants.au3>
    #include <ComboConstants.au3>
    #include <Constants.au3>
    #include <Crypt.au3>
    #include <Date.au3>
    #include <DateTimeConstants.au3>
    #include <EditConstants.au3>
    #include <Excel.au3>
    #include <GUIComboBox.au3>
    #include <GUIConstantsEx.au3>
    #include <GUIListView.au3>
    #include <GuiMenu.au3>
    #include <GuiTab.au3>
    #include <GUIToolTip.au3>
    #include <InetConstants.au3>
    #include <Misc.au3>
    #include <MySQLFunctions.au3>
    #include <SendMessage.au3>
    #include <StaticConstants.au3>
    #include <StringSize.au3>
    #include <TabConstants.au3>
    #include <Timers.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>
    #include "AssocArrays.au3" ; Made by Nutster
    #include "CaptchaControl.au3" ; Made by trancexx
    #include "GUICtrlOnHover.au3" ; Made by MrCreatoR
    #include "GUIScrollbars_Ex.au3" ; Made by Melba23
    #include "MySQL.au3" ; Made by cdkid
    #include "Yinail.au3"
    Global $g_sFileServerName = "server.ini", $g_vSQLInstance, $g_vNormalSQLInstance, $errorTipMsg = "", $errorPopupGUI, $g_hAdminForm = 9999, $g_iFormMode = 0 ; 0 - One-to-one rank comparison, 1 - Rank bigger/smaller than comparison
    Global Const $SC_DRAGMOVE = 0xF012, $SQL_DATABASE = "ShowLogin", $CONST_ADMIN_VAL = 666, $CONST_USER_VAL = 9999, $CONST_NORMAL_WIDTH = 960, $CONST_NORMAL_HEIGHT = 120, $CONST_MAIN_HEIGHT = 580
    AutoItSetOption("MouseCoordMode", 1)
#EndRegion Program Configuration Settings, Constants & Include Files

main() ;    Where everything begins

#Region Settings Functions
Func connectToMySQL() ;     Establishes connection with mySQL Database
    Local $sServerName = FileRead($g_sFileServerName)
    $g_vNormalSQLInstance = _MySQLConnect($SERVER_USER, $SERVER_PASS, $SQL_DATABASE, $sServerName)
    $g_vSQLInstance = $g_vNormalSQLInstance
    If $g_vNormalSQLInstance = 0 Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>connectToMySQL

Func main() ;   Main Function
    If UBound(ProcessList(@ScriptName)) > 2 Or UBound(ProcessList("AutoIt3_x64.exe")) > 2 Or UBound(ProcessList("AutoIt3_x86.exe")) > 2 Then ;  Prevent multiple instances of the program from running
        popupGUI("Warning", "Only one instance of the program can be running at any one time!")
        Exit
    EndIf
    Local $hLoadingScreen = GUICreate("loadscreen", 190, 80, -1, -1, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_STATICEDGE + $WS_EX_TOOLWINDOW)
    GUISetBkColor(0x555555, $hLoadingScreen)
    $hLoadingLabel = GUICtrlCreateLabel("Connecting to server...", 10, 15, 170, 50, $SS_CENTER)
    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xcccccc)
    GUISetState(@SW_SHOW)
    GUISetState(@SW_DISABLE)
    If connectToMySQL() Then
        GUIDelete($hLoadingScreen)
        showLoginForm()             ;   To main login form
        _MysqlEnd($g_vSQLInstance)  ;   Ends Session
    Else
        GUIDelete($hLoadingScreen)
        popupGUI("Error", "Error connecting to Server OR Server is inactive")
    EndIf
EndFunc   ;==>main
#EndRegion Settings Functions

#Region Cosmetic Functions
    Func popupGUI($title, $string)
        If StringLen($string) < 100 Then
            Local $popupGUI = GUICreate("popupGUI", 390, 100, -1, -1, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_STATICEDGE + $WS_EX_TOOLWINDOW)
        Else
            Local $popupGUI = GUICreate("popupGUI", 390, 150, -1, -1, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_STATICEDGE + $WS_EX_TOOLWINDOW)
        EndIf
        GUISetBkColor(0x555555, $popupGUI)
        $titleLabel = GUICtrlCreateLabel($title, 20, 10, 260, 25)
        GUICtrlSetFont(-1, 11, 600, 0, "Tahoma")
        GUICtrlSetColor(-1, 0xeeeeee)
        $stringLabel = GUICtrlCreateLabel($string, 20, 35, 260, 115)
        GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
        GUICtrlSetColor(-1, 0xeeeeee)
        GUISetState(@SW_SHOW)
        GUISetState(@SW_DISABLE)
        Sleep(1000)
        If StringLen($string) > 50 Then Sleep(1000)
        If StringLen($string) > 100 Then Sleep(1000)
        GUIDelete($popupGUI)
    EndFunc   ;==>popupGUI

    Func errorPopup()
        $winState = WinGetState("errorPopupGUI")
        If BitAND($winState, 1) And BitAND($winState, 2) Then
            WinMove("errorPopupGUI", "", MouseGetPos()[0] + 25, MouseGetPos()[1] - 105)
        ElseIf $winState = 0 Then
            $errorPopupGUI = GUICreate("errorPopupGUI", 200, 80, MouseGetPos()[0] + 25, MouseGetPos()[1] - 105, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_STATICEDGE + $WS_EX_TOOLWINDOW)
            $errorLabel = GUICtrlCreateLabel($errorTipMsg, 10, 10, 190, 70)
            GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
            GUICtrlSetColor(-1, 0xcccccc)
            $hErrorPopupGUI = WinGetHandle("errorPopupGUI", "")
            GUISetBkColor(0x555555, $hErrorPopupGUI)
            GUISetState(@SW_SHOW)
            For $i = 15 To 255 Step 20
                Sleep(10)
                WinMove("errorPopupGUI", "", MouseGetPos()[0] + 25, MouseGetPos()[1] - 105)
            Next
        EndIf
    EndFunc   ;==>errorPopup

    Func expandGUIX($coordinate)
        Return $coordinate * 1.39
    EndFunc   ;==>expandGUIX

    Func expandGUIY($coordinate)
        Return $coordinate * 1.32
    EndFunc   ;==>expandGUIY
#EndRegion Cosmetic Functions

#Region Miscellaneous Functions
Func sendRecoveryEmail($email, $emailbody)
    Return _INetSmtpMailCom("smtp.gmail.com", "My Inventory", $RECOVERY_EMAIL_ACC, $email, "My Inventory Username Recovery", $emailbody, "", "", "", "Normal", $RECOVERY_EMAIL_ACC, $RECOVERY_EMAIL_PW, 465, 1)
EndFunc   ;==>sendRecoveryEmail

Func getString($type, $param = "", $param2 = "") ;  Gets a string of variables, type of which is defined by $type
    Local $mySQLstatement, $retString = ""
    Switch $type
        Case "Username" ;   Returns list of usernames separated by a pipe "|", include username as a parameter to exclude username from list
            $mySQLstatement = "SELECT * FROM MyLogin WHERE STATUS = 1"
            If $param <> "" Then
                $mySQLstatement = $mySQLstatement & " AND NOT ID = " & $param
            EndIf
    EndSwitch
    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)
    Switch $type
        Case "Username" ;   Returns list of usernames separated by a pipe "|", include username as a parameter to exclude username from list
            While Not $executeStatement.EOF
                $retString = $retString & $executeStatement.Fields("Username").value & "|"
                $executeStatement.Movenext
            WEnd
            $retString = StringTrimRight($retString, 1)
    EndSwitch
    Return $retString
EndFunc   ;==>getString

Func getUserPWSalt($userID)
    Local $mySQLstatement = "SELECT PWSalt FROM MyLogin WHERE ID = " & $userID
    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)

    If Not $executeStatement.EOF Then
        Return $executeStatement.Fields("PWSalt").Value
    Else
        Return 0
    EndIf
EndFunc   ;==>getUserPWSalt

Func genPw() ;  Generate new password
    Local $aSpace[3], $pw = "", $pwLength = 8
    For $i = 1 To $pwLength
        $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
        $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z
        $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9
        $pw &= $aSpace[Random(0, 2, 1)]
    Next
    Return $pw
EndFunc   ;==>genPw

Func genSalt() ;    Generate new password salt
    Local $aSpace[3], $salt = "", $saltLength = 16
    For $i = 1 To $saltLength
        $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
        $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z
        $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9
        $salt &= $aSpace[Random(0, 2, 1)]
    Next
    Return $salt
EndFunc   ;==>genSalt

Func hashPw($unhashedpassword) ;    Hashs password
    Local $hashedpassword
    _Crypt_Startup()
    $hashedpassword = _Crypt_HashData($unhashedpassword, $CALG_SHA1)
    _Crypt_Shutdown()
    Return $hashedpassword
EndFunc   ;==>hashPw

Func isValidEmail($email) ;     Checks if username entered is valid
    Return StringRegExp($email, "[a-zA-Z0-9_-]+\@gmail\.com{1}$")
EndFunc   ;==>isValidEmail

Func notifyPwError($password)
    $errorValue = identifyPwError($password)
    $retMsg = ""
    If $errorValue <> 0 Then
        $retMsg = "Your password is invalid because of the following reason(s): " & @CRLF
        If BitAND($errorValue, 1) Then $retMsg = $retMsg & "It is too short (Length required: 8-16 characters)." & @CRLF
        If BitAND($errorValue, 2) Then $retMsg = $retMsg & "It is too long (Length required: 8-16 characters)." & @CRLF
        If BitAND($errorValue, 4) Then $retMsg = $retMsg & "Password contains invalid characters (Allowed characters: Alphanumericals, dashes and underscores." & @CRLF
    EndIf
    Return $retMsg
EndFunc   ;==>notifyPwError

Func identifyPwError($password)
    $retVal = 0
    If StringRegExp($password, "^[.]{0,7}$") Then $retVal += 1
    If StringRegExp($password, "^[.]{17}$") Then $retVal += 2
    If StringRegExp($password, "^[a-zA-Z0-9_-]$") Then $retVal += 4
    Return $retVal
EndFunc   ;==>identifyPwError

Func isValidPw($password) ;     Checks if password entered is valid
    Return StringRegExp($password, "^[a-zA-Z0-9_-]{8,16}$")
EndFunc   ;==>isValidPw

Func isValidUser($username) ;   Checks if username entered is valid
    Return StringRegExp($username, "^[a-zA-Z0-9_-]{4,16}$")
EndFunc   ;==>isValidUser

Func isValidEntry($entry) ;     Checks if string entry is valid
    Return StringRegExp($entry, "^[a-zA-Z0-9_-]+[a-zA-Z0-9_-\s]*$")
EndFunc   ;==>isValidEntry

Func isValidLogin($userID, $password) ;     Checks credentials of username and password entered
    Local $userSalt = getUserPWSalt($userID)
    Local $hashedpassword = hashPw(hashPw($password & $userSalt) & $userSalt)
    Local $mySQLstatement = "SELECT * FROM MyLogin WHERE ID = " & $userID & " AND Password = '" & $hashedpassword & "' AND STATUS = 1"
    Local $userexists = 0
    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)
    If $executeStatement.EOF Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>isValidLogin

Func isNotBlank($a1 = ".", $a2 = ".", $a3 = ".", $a4 = ".", $a5 = ".", $a6 = ".", $a7 = ".") ;  Checks if fields (up to 7 fields) are empty. Returns true if not blank
    If $a1 = "" Or $a2 = "" Or $a3 = "" Or $a4 = "" Or $a5 = "" Or $a6 = "" Or $a7 = "" Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>isNotBlank

Func getID($type, $name) ;  Gets ID linked to name
    Local $mySQLstatement
    Switch $type
        Case "User"
            $mySQLstatement = "SELECT * FROM MyLogin WHERE Username = '" & $name & "'"
    EndSwitch
    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)
    If Not $executeStatement.EOF Then
        MsgBox(0, "Debug - UserID Value", $executeStatement.Fields("ID").value)
        Return $executeStatement.Fields("ID").value
    Else
        Return 0
    EndIf
EndFunc   ;==>getID

Func getName($type, $ID, $login = "") ; Gets name linked to ID
    Local $mySQLstatement
    Switch $type
        Case "User"
            $mySQLstatement = "SELECT * FROM MyLogin WHERE ID = " & $ID
            If $login <> "" Then $mySQLstatement = $mySQLstatement & " AND STATUS = 1"
        Case "Status"
            If $ID = 1 Then
                Return "Active"
            Else
                Return "Inactive"
            EndIf
    EndSwitch
    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)
    If $executeStatement.EOF Then
        Return 0
    Else
        Switch $type
            Case "User"
                Return $executeStatement.Fields("Username").value
        EndSwitch
    EndIf
EndFunc   ;==>getName

Func getFriendlyMonth($numericmonth, $shortform = 1) ;  Converts numeric month to worded month
    Local $retVal
    Switch $numericmonth
        Case 1
            $retVal = "January"
        Case 2
            $retVal = "February"
        Case 3
            $retVal = "March"
        Case 4
            $retVal = "April"
        Case 5
            $retVal = "May"
        Case 6
            $retVal = "June"
        Case 7
            $retVal = "July"
        Case 8
            $retVal = "August"
        Case 9
            $retVal = "September"
        Case 10
            $retVal = "October"
        Case 11
            $retVal = "November"
        Case 12
            $retVal = "December"
    EndSwitch
    If $shortform = 1 Then
        Return StringLeft($retVal, 3)
    Else
        Return $retVal
    EndIf
EndFunc   ;==>getFriendlyMonth

Func getFriendlyTime($date) ;   Converts numeric time to a view friendly time
    Local $retVal
    If StringMid($date, 9, 4) >= 1300 And StringMid($date, 9, 4) < 2400 Then
        $retVal = StringMid($date, 9, 2) - 12 & ":" & StringMid($date, 11, 2) & " PM"
    ElseIf StringMid($date, 9, 4) >= 1200 And StringMid($date, 9, 4) < 1300 Then
        $retVal = StringMid($date, 9, 2) & ":" & StringMid($date, 11, 2) & " PM"
    Else
        $retVal = StringMid($date, 9, 2) & ":" & StringMid($date, 11, 2) & " AM"
    EndIf
    Return $retVal
EndFunc   ;==>getFriendlyTime

Func getFriendlyDate($date, $format) ;  Converts numeric date to a view friendly date
    Switch $format
        Case 0 ;    For Last Login
            Return StringMid($date, 7, 2) & " " & getFriendlyMonth(StringMid($date, 5, 2)) & " " & StringLeft($date, 4) & " at " & StringMid($date, 9, 2) & ":" & StringMid($date, 11, 2) & "."
        Case 1 ;    For List View (DD/MM/YYYY HH:MM AM/PM)
            Return StringMid($date, 7, 2) & "/" & StringMid($date, 5, 2) & "/" & StringLeft($date, 4) & " " & getFriendlyTime($date)
        Case 2 ;    For Birthday List View (DD/MM/YYYY)
            Return StringMid($date, 7, 2) & "/" & StringMid($date, 5, 2) & "/" & StringLeft($date, 4)
        Case 3 ;    For setting value in GUICtrlCreateDate
            Return StringLeft($date, 4) & "-" & StringMid($date, 5, 2) & "-" & StringMid($date, 7, 2)
    EndSwitch
    StringMid($date, 7, 2) & " " & getFriendlyMonth(StringMid($date, 5, 2)) & " " & StringLeft($date, 4) & " at " & StringMid($date, 9, 2) & ":" & StringMid($date, 11, 2) & "." ;  Minute
EndFunc   ;==>getFriendlyDate

Func checkExists($type, $param1, $param2 = "") ;    Checks if something of specific type exists in DB
    Local $mySQLstatement
    Switch $type
        Case "Username"
            $mySQLstatement = "SELECT * FROM MyLogin WHERE Username = '" & $param1 & "' AND Status = 1"
        Case "Email"
            $mySQLstatement = "SELECT * FROM MyLogin WHERE Email = '" & $param1 & "' AND Status = 1"
        Case "Item"
            $mySQLstatement = "SELECT * FROM MyInventory WHERE UserID = '" & $param1 & "' AND InternalName = '" & $param2 & "' AND Status = 1"
    EndSwitch
    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)
    If $executeStatement.EOF Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>checkExists
#EndRegion Miscellaneous Functions

#Region Form/Function Processors
Func checkRegistration($username, $password, $password2, $email, $rank, $firstName = "", $lastName = "", $birthday = "") ;  Registration Form Checker
    Local $retVal = 0 ; Returns: 0 - Valid entry, 1 - Nothing entered, 2 - Passwords don't match, 3 - Invalid Entry, 4 - Nonexistent Username, 5 - Can't register onto db
    If isNotBlank($username, $password, $password2, $email, $rank) Then ;   Checks for blanks
        If $password == $password2 Then                                 ;   Checks if passwords match
            If isValidPw($password) And isValidUser($username) And isValidEmail($email) Then;   Checks for valid username, password and email
                If checkExists("Username", $username) Or checkExists("Email", $email) Then  ;   Checks for duplicate username/email entry
                    $retVal = 4
                Else
                    Local $newSalt = genSalt()
                    Local $hashedpassword = hashPw(hashPw($password & $newSalt) & $newSalt)
                    Local $mySQLstatement = "INSERT INTO MyLogin (Username,Password,PWSalt,Email,RankID,Status) VALUES ('" & $username & "','" & $hashedpassword & "','" & $newSalt & "','" & $email & "'," & $rank & ",1)"
                    If $firstName = "" And $lastName = "" And $birthday = "" Then
                        Local $mySQLstatement1 = "INSERT INTO myprofile (FirstName) VALUES ('" & $username & "')"
                    Else
                        If isValidEntry($firstName) And isValidEntry($lastName) Then
                            Local $mySQLstatement1 = "INSERT INTO myprofile (FirstName, LastName, Birthday) VALUES ('" & $firstName & "','" & $lastName & "','" & $birthday & "')"
                        Else
                            Return 3
                        EndIf
                    EndIf
                    $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)
                    $executeStatement1 = _Query($g_vSQLInstance, $mySQLstatement1)
                    If $executeStatement = 0 Or $executeStatement1 = 0 Then
                        $retVal = 5
                    Else
                        Local $mySQLstatement3 = "SELECT * FROM MyLogin WHERE Username = '" & $username & "' AND Status = 1" ;  Retrieves new user ID from database
                        $executeStatement3 = _Query($g_vSQLInstance, $mySQLstatement3)
                        $userID = $executeStatement3.Fields("ID").value
                        $retVal = 0
                    EndIf
                EndIf
            Else
                $retVal = 3
            EndIf
        Else
            $retVal = 2
        EndIf
    Else
        $retVal = 1
    EndIf
    Return $retVal
EndFunc   ;==>checkRegistration

Func checkRecoverPw($username) ;    Forget Password Form Checker
    Local $retVal = 0 ; Returns: 0 - Valid entry, 1 - Nothing entered, 2 - Invalid Entry, 3 - Nonexistent Username, 4 - Can't register onto db
    Local $password, $email, $emailbody

    If isNotBlank($username) Then ; Checks for blanks
        If isValidUser($username) Then ;    Checks for valid username
            If checkExists("Username", $username) = 0 Then ;    Checks for existing username
                $retVal = 3
            Else
                Local $mySQLstatement = "SELECT * FROM MyLogin WHERE Username = '" & $username & "' AND STATUS = 1"
                $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)

                $userID = $executeStatement.Fields("ID").value
                $username = $executeStatement.Fields("Username").value
                $email = $executeStatement.Fields("Email").value

                Local $newPW = genPw(), $newSalt = genSalt()
                Local $hashedpassword = hashPw(hashPw($newPW & $newSalt) & $newSalt)

                Local $mySQLstatement1 = "UPDATE MyLogin SET Password = '" & $hashedpassword & "', PWSalt = '" & $newSalt & "' WHERE ID = " & $userID & " AND Status = 1"
                $executeStatement1 = _Query($g_vSQLInstance, $mySQLstatement1)

                If $executeStatement = 0 Then
                Else
                    Local $processingScreen = GUICreate("Processing", 190, 80, -1, -1, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_STATICEDGE + $WS_EX_TOOLWINDOW)
                    GUISetBkColor(0x555555, $processingScreen)
                    $processingLabel = GUICtrlCreateLabel("Processing...", 10, 15, 170, 50, $SS_CENTER)
                    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
                    GUICtrlSetColor(-1, 0xcccccc)
                    ;WinSetTrans($processingScreen, "", 0)
                    GUISetState(@SW_HIDE)
                    GUISetState(@SW_SHOW)
                    GUISetState(@SW_DISABLE)

                    $emailbody = "Hello " & $username & "," & @CRLF & @CRLF & "You recently made a request to reset your password using the username '" & $username & "'. Your password has been reset to " & $newPW & "." & @CRLF & @CRLF & "Please log in with this new password to change your account details." & @CRLF & "The Admin Team"
                    sendRecoveryEmail($email, $emailbody)
                    If @error = 1 Then
                        GUICtrlSetData($processingLabel, "We encountered an error. Please contact your administrator")
                    Else
                        GUICtrlSetData($processingLabel, "Your username has been sent to your email!")
                    EndIf
                    Sleep(2000)
                    GUIDelete($processingScreen)
                    $retVal = 0
                EndIf
            EndIf
        Else
            $retVal = 2
        EndIf
    Else
        $retVal = 1
    EndIf

    Return $retVal
EndFunc   ;==>checkRecoverPw

Func checkRecoverUser($email) ; Forget Username Form Checker
    Local $retVal = 0 ; Returns: 0 - Valid entry, 1 - Nothing entered, 2 - Invalid Entry, 3 - Nonexistent Username, 4 - Can't register onto db
    Local $username, $emailbody

    If isNotBlank($email) Then ; Checks for blanks
        If isValidEmail($email) Then ; Checks for valid email
            If checkExists("Email", $email) = 0 Then ; Checks for existing email
                $retVal = 3
            Else
                Local $mySQLstatement = "SELECT * FROM MyLogin WHERE Email = '" & $email & "' AND STATUS = 1"
                $executeStatement = _Query($g_vSQLInstance, $mySQLstatement)

                $userID = $executeStatement.Fields("ID").value
                $username = $executeStatement.Fields("Username").value
                $emailbody = "Hello " & $username & "," & @CRLF & @CRLF & "You recently made a request for your username using the email address " & $email & "." & @CRLF & @CRLF & "The Company SWQA Team"
                If $executeStatement = 0 Then
                    $retVal = 4
                Else
                    Local $processingScreen = GUICreate("Processing", 190, 80, -1, -1, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_STATICEDGE + $WS_EX_TOOLWINDOW)
                    GUISetBkColor(0x555555, $processingScreen)
                    $processingLabel = GUICtrlCreateLabel("Processing...", 10, 15, 170, 50, $SS_CENTER)
                    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
                    GUICtrlSetColor(-1, 0xcccccc)
                    ;WinSetTrans($processingScreen, "", 0)
                    GUISetState(@SW_HIDE)
                    GUISetState(@SW_SHOW)
                    GUISetState(@SW_DISABLE)

                    sendRecoveryEmail($email, $emailbody)
                    If @error = 1 Then
                        GUICtrlSetData($processingLabel, "We encountered an error. Please contact your administrator")
                    Else
                        GUICtrlSetData($processingLabel, "Your username has been sent to your email!")
                    EndIf
                    Sleep(2000)
                    GUIDelete($processingScreen)
                    $retVal = 0
                EndIf
            EndIf
        Else
            $retVal = 2
        EndIf
    Else
        $retVal = 1
    EndIf

    Return $retVal
EndFunc   ;==>checkRecoverUser

Func checkUserLogin($username, $password) ;     Login Form Checker
    Local $retVal = 0 ; Returns: 0 - Valid entry, 1 - Nothing entered, 2 - Invalid Entry, 3 - Nonexistent Username, 4 - Can't register onto db
    If isNotBlank($username, $password) Then
        If isValidPw($password) And isValidUser($username) Then
            If isValidLogin(getID("User", $username), $password) Then
                $retVal = 0
            Else
                $retVal = 3
            EndIf
        Else
            $retVal = 2
        EndIf
    Else
        $retVal = 1
    EndIf
    Return $retVal
EndFunc   ;==>checkUserLogin
#EndRegion Form/Function Processors

#Region Forms
Func showForgetPwForm() ;   Forget Password Form
    Global $ForgetPwForm
    Local $formTitleLabel
    Local $submitButton
    Local $userLabel, $userInput
    Local $invalidUser, $missingInput, $nonexistentUser

    ;   Forget Password Form Canvas
    $ForgetPwForm = GUICreate("Forgot Password", 535, 190, -1, -1, $WS_POPUP)
    GUISetIcon(@ScriptDir & "\Image\Logo.ico", -1)

    ;   Form background
    $formBg = GUICtrlCreatePic("image\alt_bg_landscape.jpg", 0, 0, 550, 190)
    GUICtrlSetState($formBg, $GUI_DISABLE)

    ;   Form Title
    $formTitleLabel = GUICtrlCreateLabel("Enter your username to recover your password.", 17, 25, 500, 40, $SS_CENTER)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xeeeeee)

    ;   Enter username Label
    $userLabel = GUICtrlCreateLabel("Username", 30, 80, 105, 30)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xeeeeee)

    ;   Enter username Input
    $userInput = GUICtrlCreateInput("", 140, 75, 350, 35)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)

    ;=========================================================================================
    ;   Valid Username Tip
    $userTip = GUICtrlCreateLabel("√", 495, 83, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Username Tip
    $userXTip = GUICtrlCreateLabel("❌", 495, 83, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    _GUICtrl_OnHoverRegister($userXTip, 'errorPopup', "dErrorPopup", "", "", "", 1)
    ;=========================================================================================

    ;   Forgot User Button
    $forgetUserButton = GUICtrlCreateLabel("Forgot your Username?", 155, 135, 170, 20)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetFont(-1, 10, 600, 2, "Tahoma")

    ;   Missing username Tip
    $missingInput = GUICtrlCreateLabel("Please enter your username.", 145, 55, 200, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)

    ;   Invalid username Tip
    $invalidUser = GUICtrlCreateLabel("Please enter a valid username.", 145, 55, 200, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, "", 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)

    ;   Nonexistent username Tip
    $nonexistentUser = GUICtrlCreateLabel("The username you entered does not exist.", 145, 55, 250, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)

    ;   Submit Button
    $submitButton = GUICtrlCreateButton("Submit", 365, 125, 110, 45)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")

    ;   Exit Button
    $exitLabel = GUICtrlCreateLabel("X", WinGetPos($ForgetPwForm)[2] - 22, 2, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)

    GUICtrlSetState($missingInput, $GUI_HIDE) ; Hides Error Messages on GUI on start
    GUICtrlSetState($invalidUser, $GUI_HIDE)
    GUICtrlSetState($nonexistentUser, $GUI_HIDE)
    GUICtrlSetState($userInput, $GUI_FOCUS)
    GUICtrlSetData($userInput, "")
    GUICtrlSetState($userXTip, $GUI_HIDE)
    GUICtrlSetState($userTip, $GUI_HIDE)
    GUISetState(@SW_HIDE)
    GUISetState(@SW_SHOW)

    While 1
        $guiEvent = GUIGetMsg(1)
        Switch $guiEvent[0]
            Case $exitLabel
                MouseUp("left")
                ContinueCase
            Case $GUI_EVENT_CLOSE
                GUIDelete($ForgetPwForm)
                ExitLoop
            Case $GUI_EVENT_PRIMARYDOWN
                _SendMessage($ForgetPwForm, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
            Case $forgetUserButton
                GUIDelete($ForgetPwForm)
                showForgetUserForm()
                ExitLoop
            Case $submitButton
                GUISetState(@SW_DISABLE)
                $userValue = GUICtrlRead($userInput)
                Switch checkRecoverPw($userValue)
                    Case 0
                        GUIDelete($ForgetPwForm)
                        ExitLoop
                    Case 1
                        GUICtrlSetState($invalidUser, $GUI_HIDE)
                        GUICtrlSetState($nonexistentUser, $GUI_HIDE)
                        GUICtrlSetState($missingInput, $GUI_SHOW)
                        GUICtrlSetData($userInput, "")
                    Case 2
                        GUICtrlSetState($missingInput, $GUI_HIDE)
                        GUICtrlSetState($nonexistentUser, $GUI_HIDE)
                        GUICtrlSetState($invalidUser, $GUI_SHOW)
                        GUICtrlSetData($userInput, "")
                    Case 3
                        GUICtrlSetState($invalidUser, $GUI_HIDE)
                        GUICtrlSetState($missingInput, $GUI_HIDE)
                        GUICtrlSetState($nonexistentUser, $GUI_SHOW)
                        GUICtrlSetData($userInput, "")
                    Case 4
                        popupGUI("Error!", "Error registering! Please contact your administrator.")
                        GUIDelete($ForgetPwForm)
                        ExitLoop
                EndSwitch
                GUISetState(@SW_ENABLE)
            Case $userInput
                $userValue = GUICtrlRead($userInput)
                If isValidUser($userValue) Then                 ;   If username is valid
                    If checkExists("Username", $userValue) Then ;   If username already exists
                        GUICtrlSetState($userXTip, $GUI_HIDE)
                        GUICtrlSetState($userTip, $GUI_SHOW)
                    Else
                        $errorTipMsg = "The username does not exist."
                        GUICtrlSetState($userTip, $GUI_HIDE)
                        GUICtrlSetState($userXTip, $GUI_SHOW)
                    EndIf
                ElseIf $userValue = "" Then
                    GUICtrlSetState($userXTip, $GUI_HIDE)
                    GUICtrlSetState($userTip, $GUI_HIDE)
                Else                                            ;   If username is invalid
                    $errorTipMsg = "Please enter a valid alphanumeric username of 4-16 characters."
                    GUICtrlSetState($userTip, $GUI_HIDE)
                    GUICtrlSetState($userXTip, $GUI_SHOW)
                EndIf
        EndSwitch
    WEnd
EndFunc   ;==>showForgetPwForm

Func showForgetUserForm() ;     Forget Username Form
    Global $forgetUserForm
    Local $formTitleLabel
    Local $submitButton
    Local $emailLabel, $emailInput
    Local $invalidEmail, $missingInput, $nonexistentEmail

    ;   Forget User Form Canvas
    $forgetUserForm = GUICreate("Forgot Username", 490, 190, -1, -1, $WS_POPUP)
    GUISetIcon(@ScriptDir & "\Image\Logo.ico", -1)

    ;   Form background
    $formBg = GUICtrlCreatePic("image\alt_bg_landscape.jpg", 0, 0, 500, 190)
    GUICtrlSetState($formBg, $GUI_DISABLE)

    ;   Form Title
    $formTitleLabel = GUICtrlCreateLabel("Enter your email to recover your username.", 45, 25, 400, 40, $SS_CENTER)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xeeeeee)

    ;   Enter Email Label
    $emailLabel = GUICtrlCreateLabel("Email", 30, 80, 60, 30)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")

    ;   Enter Email Input
    $emailInput = GUICtrlCreateInput("", 100, 75, 350, 35)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 64)

    ;=========================================================================================
    ;   Valid Email Tip
    $emailTip = GUICtrlCreateLabel("√", 453, 83, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Email Tip
    $emailXTip = GUICtrlCreateLabel("❌", 453, 83, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    _GUICtrl_OnHoverRegister($emailXTip, 'errorPopup', "dErrorPopup", "", "", "", 1)
    ;=========================================================================================

    ;   Forgot Password Button
    $forgetpwButton = GUICtrlCreateLabel("Forgot your Password?", 115, 135, 170, 20)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xFFFFFF)
    GUICtrlSetFont(-1, 10, 600, 2, "Tahoma")

    ;   Missing Email Tip
    $missingInput = GUICtrlCreateLabel("Please enter your email.", 105, 55, 400, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)

    ;   Invalid Email Tip
    $invalidEmail = GUICtrlCreateLabel("Please enter a valid email.", 105, 55, 400, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, "", 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)

    ;   Nonexistent Email Tip
    $nonexistentEmail = GUICtrlCreateLabel("The email you entered does not exist.", 105, 55, 400, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)

    ;   Submit Button
    $submitButton = GUICtrlCreateButton("Submit", 325, 125, 110, 45)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")

    ;   Exit Button
    $exitLabel = GUICtrlCreateLabel("X", WinGetPos($forgetUserForm)[2] - 22, 2, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)

    GUICtrlSetState($missingInput, $GUI_HIDE) ; Hides Error Messages on GUI on start
    GUICtrlSetState($invalidEmail, $GUI_HIDE)
    GUICtrlSetState($nonexistentEmail, $GUI_HIDE)
    GUICtrlSetState($emailInput, $GUI_FOCUS)
    GUICtrlSetData($emailInput, "")
    GUICtrlSetState($emailXTip, $GUI_HIDE)
    GUICtrlSetState($emailTip, $GUI_HIDE)
    GUISetState(@SW_HIDE)
    GUISetState(@SW_SHOW)

    While 1
        $guiEvent = GUIGetMsg(1)

        Switch $guiEvent[0]
            Case $exitLabel
                MouseUp("left")
                ContinueCase
            Case $GUI_EVENT_CLOSE
                GUIDelete($forgetUserForm)
                ExitLoop

            Case $GUI_EVENT_PRIMARYDOWN
                _SendMessage($forgetUserForm, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)

            Case $forgetpwButton
                GUIDelete($forgetUserForm)
                showForgetPwForm()
                ExitLoop

            Case $submitButton
                GUISetState(@SW_DISABLE)
                $emailValue = GUICtrlRead($emailInput)

                Switch checkRecoverUser($emailValue)
                    Case 0
                        GUIDelete($forgetUserForm)
                        ExitLoop
                    Case 1
                        GUICtrlSetState($invalidEmail, $GUI_HIDE)
                        GUICtrlSetState($nonexistentEmail, $GUI_HIDE)
                        GUICtrlSetState($missingInput, $GUI_SHOW)

                        GUICtrlSetData($emailInput, "")
                    Case 2
                        GUICtrlSetState($missingInput, $GUI_HIDE)
                        GUICtrlSetState($nonexistentEmail, $GUI_HIDE)
                        GUICtrlSetState($invalidEmail, $GUI_SHOW)

                        GUICtrlSetData($emailInput, "")
                    Case 3
                        GUICtrlSetState($invalidEmail, $GUI_HIDE)
                        GUICtrlSetState($missingInput, $GUI_HIDE)
                        GUICtrlSetState($nonexistentEmail, $GUI_SHOW)

                        GUICtrlSetData($emailInput, "")
                    Case 4
                        GUICtrlSetState($invalidEmail, $GUI_HIDE)
                        GUICtrlSetState($nonexistentEmail, $GUI_HIDE)
                        GUICtrlSetState($missingInput, $GUI_HIDE)
                        GUIDelete($forgetUserForm)
                        ExitLoop
                EndSwitch
                GUISetState(@SW_ENABLE)

            Case $emailInput
                $emailValue = GUICtrlRead($emailInput)
                If isValidEmail($emailValue) Then               ;   If email is valid
                    If checkExists("Email", $emailValue) Then   ;   If email already exists
                        GUICtrlSetState($emailXTip, $GUI_HIDE)
                        GUICtrlSetState($emailTip, $GUI_SHOW)
                    Else
                        $errorTipMsg = "The email does not exist."
                        GUICtrlSetState($emailTip, $GUI_HIDE)
                        GUICtrlSetState($emailXTip, $GUI_SHOW)
                    EndIf
                ElseIf $emailValue = "" Then
                    GUICtrlSetState($emailXTip, $GUI_HIDE)
                    GUICtrlSetState($emailTip, $GUI_HIDE)
                Else                                            ;   If email is invalid
                    $errorTipMsg = "Please enter a valid email (i.e. John@gmail.com)."
                    GUICtrlSetState($emailTip, $GUI_HIDE)
                    GUICtrlSetState($emailXTip, $GUI_SHOW)
                EndIf
        EndSwitch
    WEnd
EndFunc   ;==>showForgetUserForm

Func showRegisterForm() ;   Registration Form
    Global $registerForm
    Local $formTitleLabel, $formLogo
    Local $registerButton
    Local $userLabel, $pwLabel, $pw2Label, $emailLabel
    Local $userInput, $pwInput, $pw2Input, $emailInput
    Local $invalidUser, $invalidPw, $nomatchPw, $invalidEmail, $invalidFirstname, $invalidLastname, $novalue

    ;   Register Form Canvas
    $registerForm = GUICreate("Registration", 560, 450, -1, -1, $WS_POPUP)
    GUISetIcon(@ScriptDir & "\Image\Logo.ico", -1)

    ;   Form background
    $formBg = GUICtrlCreatePic("image\alt_bg_landscape.jpg", 0, 0, 600, 480)
    GUICtrlSetState($formBg, $GUI_DISABLE)

    ;   Form Title
    $formTitleLabel = GUICtrlCreateLabel("Register New Account", 80, 40, 400, 40, $SS_CENTER)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 16, 800, 0, "Tahoma")

    ;   Username Label
    $userLabel = GUICtrlCreateLabel("Username", 40, 120, 105, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)

    ;   Password Label
    $pwLabel = GUICtrlCreateLabel("Password", 40, 170, 105, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")

    ;   Confirm Password Label
    $pwLabel = GUICtrlCreateLabel("Re-enter Password", 40, 215, 105, 75)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")

    ;   Email Label
    $emailLabel = GUICtrlCreateLabel("Email", 40, 270, 105, 25)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")

    ;   Captcha Label
    $CaptchaLabel = GUICtrlCreateLabel("Enter Captcha", 40, 315, 105, 75)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")

    ;   Username Input
    $userInput = GUICtrlCreateInput("", 165, 120, 350, 35)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)

    ;   Password Input
    $pwInput = GUICtrlCreateInput("", 165, 170, 350, 35, $ES_PASSWORD)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)

    ;   Confirm Password Input
    $pw2Input = GUICtrlCreateInput("", 165, 220, 350, 35, $ES_PASSWORD)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)

    ;   Email Input
    $emailInput = GUICtrlCreateInput("", 165, 270, 350, 35)
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 64)

    ;################################################################################################################################################
    ;   START OF ERROR TIPS
    Local $hErrorTip = _GUIToolTip_Create(0, BitOR($_TT_ghTTDefaultStyle, $TTS_BALLOON)); balloon style tooltip

    ;   Valid Username Tip
    $userTip = GUICtrlCreateLabel("√", 520, 128, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Username Tip
    $userXTip = GUICtrlCreateLabel("❌", 520, 128, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    $hUserXTip = GUICtrlGetHandle($userXTip)
    _GUIToolTip_AddTool($hErrorTip, 0, "userXTip", $hUserXTip)

    ;   Valid Password Tip
    $pwTip = GUICtrlCreateLabel("√", 520, 178, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Password Tip
    $pwXTip = GUICtrlCreateLabel("❌", 520, 178, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    $hPwXTip = GUICtrlGetHandle($pwXTip)
    _GUIToolTip_AddTool($hErrorTip, 0, "Please enter an alphanumeric password of 8 - 16 characters. Your password may include underscores, and dashes.", $hPwXTip)

    ;   Valid Password Tip
    $pw2Tip = GUICtrlCreateLabel("√", 520, 228, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Password Tip
    $pw2XTip = GUICtrlCreateLabel("❌", 520, 228, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    $hPw2XTip = GUICtrlGetHandle($pw2XTip)
    _GUIToolTip_AddTool($hErrorTip, 0, "pw2XTip", $hPw2XTip)

    ;   Valid Email Tip
    $emailTip = GUICtrlCreateLabel("√", 520, 278, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Email Tip
    $emailXTip = GUICtrlCreateLabel("❌", 520, 278, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    $hEmailXTip = GUICtrlGetHandle($emailXTip)
    _GUIToolTip_AddTool($hErrorTip, 0, "emailXTip", $hEmailXTip)

    ;   Valid Captcha Tip
    $captchaTip = GUICtrlCreateLabel("√", 520, 323, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x00ee00)
    ;   Invalid Captcha Tip
    $captchaXTip = GUICtrlCreateLabel("❌", 520, 323, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xee0000)
    $hCaptchaXTip = GUICtrlGetHandle($captchaXTip)
    _GUIToolTip_AddTool($hErrorTip, 0, "captchaXTip", $hCaptchaXTip)

    ;   END OF ERROR TIPS
    ;################################################################################################################################################
    ;   Register Button
    $registerButton = GUICtrlCreateButton("Register", 225, 380, 110, 45)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")

    ;   Exit Button
    $exitLabel = GUICtrlCreateLabel("X", WinGetPos($registerForm)[2] - 22, 2, 20, 20)
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    ;################################################################################################################################################
    ;   START OF CAPTCHA
    $hCaptchaGUI = $registerForm
    $hCaptchaPic = GUICtrlCreatePic("", 348, 320, 120, 55)
    $hButtonNew = GUICtrlCreateButton("New", 475, 330, 40, 35)
    $hButton = $registerButton
    $hInput = GUICtrlCreateInput("", 165, 320, 170, 35)

    If @OSVersion = "WIN_2012" Then
        GUICtrlSetState($hCaptchaPic, $GUI_DISABLE)
        GUICtrlSetState($hButtonNew, $GUI_DISABLE)
        GUICtrlSetState($hInput, $GUI_DISABLE)
    EndIf

    GUICtrlSetState(-1, 256)

    ;   Enable OpenGL
    If Not _EnableOpenGL(GUICtrlGetHandle($hCaptchaPic), $hDeviceContext, $hRenderingContext) Then
        MsgBox(48, "Error", "Error initializing usage of OpenGL functions" & @CRLF & "Error code: " & @error)
        Exit
    EndIf

    ;   OpenGL preparing and positioning
    _glClear(BitOR($GL_COLOR_BUFFER_BIT, $GL_DEPTH_BUFFER_BIT)) ; initially cleaning buffers in case something is left there
    _glViewport(0, 0, 120, 55) ; position the view

    ;   Random Font Generator
    $iFontUbound = _GetNumberOfFonts()
    $sFont = RegEnumVal("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", Random(1, $iFontUbound, 1))
    $hFontList = _CaptchaCreateOpenGLFont(16, 400, 256 + 2 ^ Random(0, 3, 1), $sFont)

    GUIRegisterMsg(133, "_Preserve") ; WM_NCPAINT

    ;   Initial generation of captcha text
    _NewText()
    ;   END OF CAPTCHA
    ;################################################################################################################################################
    GUICtrlSetData($userInput, "")
    GUICtrlSetData($emailInput, "")
    GUICtrlSetState($userInput, $GUI_FOCUS)
    ;==============================================================================
    GUICtrlSetState($userXTip, $GUI_HIDE)
    GUICtrlSetState($userTip, $GUI_HIDE)
    GUICtrlSetState($pwXTip, $GUI_HIDE)
    GUICtrlSetState($pwTip, $GUI_HIDE)
    GUICtrlSetState($pw2XTip, $GUI_HIDE)
    GUICtrlSetState($pw2Tip, $GUI_HIDE)
    GUICtrlSetState($emailXTip, $GUI_HIDE)
    GUICtrlSetState($emailTip, $GUI_HIDE)
    GUICtrlSetState($captchaXTip, $GUI_HIDE)
    GUICtrlSetState($captchaTip, $GUI_HIDE)
    ;==============================================================================
    GUISetState(@SW_HIDE)
    GUISetState(@SW_SHOW)

    While 1
        _GLDraw()
        $guiEvent = GUIGetMsg(1)
        Switch $guiEvent[0]
            Case $exitLabel
                MouseUp("left")
                ContinueCase
            Case $GUI_EVENT_CLOSE
                _GUIToolTip_Destroy($hErrorTip)
                GUIDelete($registerForm)
                ExitLoop
            Case $GUI_EVENT_PRIMARYDOWN
                _SendMessage($registerForm, $WM_SYSCOMMAND, $SC_DRAGMOVE, 0)
            Case $hButtonNew
                _NewText()
            Case $registerButton
                If @OSVersion = "WIN_2012" Then
                    $checkOS = 1
                Else
                    $checkOS = _CheckInput()
                EndIf
                If $checkOS Then
                    $userValue = GUICtrlRead($userInput)
                    $pwValue = GUICtrlRead($pwInput)
                    $pw2Value = GUICtrlRead($pw2Input)
                    $emailValue = GUICtrlRead($emailInput)

                    Switch checkRegistration($userValue, $pwValue, $pw2Value, $emailValue, 1)
                        Case 0
                            popupGUI("Success", "The user " & $userValue & " was successfully created.")
                            GUIDelete($registerForm)
                            ExitLoop
                        Case 1
                            GUICtrlSetData($pwInput, "")
                            GUICtrlSetData($pw2Input, "")
                        Case 2
                            GUICtrlSetData($pwInput, "")
                            GUICtrlSetData($pw2Input, "")
                        Case 3
                            GUICtrlSetData($pwInput, "")
                            GUICtrlSetData($pw2Input, "")
                        Case 4
                            GUICtrlSetData($userInput, "")
                            GUICtrlSetData($emailInput, "")
                        Case 5
                            popupGUI("Error", "We encountered an error when creating the user. Please contact your administrator.")
                            GUIDelete($registerForm)
                    EndSwitch
                EndIf
            Case $userInput
                If isValidUser(GUICtrlRead($userInput)) Then                ;   If username is valid
                    If checkExists("Username", GUICtrlRead($userInput)) Then;   If username already exists
                        GUICtrlSetState($userTip, $GUI_HIDE)
                        GUICtrlSetState($userXTip, $GUI_SHOW)
                        $errorTipMsg = "The username has already been taken. Please try another username."
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hUserXTip, $errorTipMsg)
                    Else
                        GUICtrlSetState($userXTip, $GUI_HIDE)
                        GUICtrlSetState($userTip, $GUI_SHOW)
                    EndIf
                ElseIf GUICtrlRead($userInput) = "" Then
                    GUICtrlSetState($userXTip, $GUI_HIDE)
                    GUICtrlSetState($userTip, $GUI_HIDE)
                Else                                                        ;   If username is invalid
                    $errorTipMsg = "Please enter an alphanumeric username of 4-16 characters. Your username may include underscores and dashes."
                    _GUIToolTip_UpdateTipText($hErrorTip, 0, $hUserXTip, $errorTipMsg)
                    GUICtrlSetState($userTip, $GUI_HIDE)
                    GUICtrlSetState($userXTip, $GUI_SHOW)
                EndIf
            Case $pwInput
                If isValidPw(GUICtrlRead($pwInput)) Then                                                            ;   If passwords are valid
                    If GUICtrlRead($pwInput) <> GUICtrlRead($pw2Input) And isValidPw(GUICtrlRead($pw2Input)) Then   ;   If passwords are different
                        $errorTipMsg = "Your passwords do not match!"
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPwXTip, $errorTipMsg)
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPw2XTip, $errorTipMsg)
                        GUICtrlSetState($pwTip, $GUI_HIDE)
                        GUICtrlSetState($pwXTip, $GUI_SHOW)
                        GUICtrlSetState($pw2Tip, $GUI_HIDE)
                        GUICtrlSetState($pw2XTip, $GUI_SHOW)
                    Else                                                                                            ;   If passwords are the same
                        If GUICtrlRead($pwInput) = GUICtrlRead($pw2Input) Then
                            GUICtrlSetState($pwXTip, $GUI_HIDE)
                            GUICtrlSetState($pwTip, $GUI_SHOW)
                            GUICtrlSetState($pw2XTip, $GUI_HIDE)
                            GUICtrlSetState($pw2Tip, $GUI_SHOW)
                        Else
                            GUICtrlSetState($pwXTip, $GUI_HIDE)
                            GUICtrlSetState($pwTip, $GUI_SHOW)
                        EndIf
                    EndIf
                Else
                    If GUICtrlRead($pw2Input) <> "" Then
                        $errorTipMsg = "Please enter an alphanumeric password of 8 - 16 characters. Your password may include underscores, dashes or @."
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPw2XTip, $errorTipMsg)
                        GUICtrlSetState($pw2Tip, $GUI_HIDE)
                        GUICtrlSetState($pw2XTip, $GUI_SHOW)
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPwXTip, $errorTipMsg)
                        GUICtrlSetState($pwTip, $GUI_HIDE)
                        GUICtrlSetState($pwXTip, $GUI_SHOW)
                    Else
                        GUICtrlSetState($pw2Tip, $GUI_HIDE)
                        GUICtrlSetState($pw2XTip, $GUI_HIDE)
                        $errorTipMsg = "Please enter an alphanumeric password of 8 - 16 characters. Your password may include underscores, dashes or @."
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPw2XTip, $errorTipMsg)
                        GUICtrlSetState($pwTip, $GUI_HIDE)
                        GUICtrlSetState($pwXTip, $GUI_SHOW)
                    EndIf
                EndIf
            Case $pw2Input
                If isValidPw(GUICtrlRead($pw2Input)) Then                                                       ;   If passwords are valid
                    If GUICtrlRead($pwInput) <> GUICtrlRead($pw2Input) And isValidPw(GUICtrlRead($pwInput)) Then;   If passwords are different
                        $errorTipMsg = "Your passwords do not match!"
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPwXTip, $errorTipMsg)
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPw2XTip, $errorTipMsg)
                        GUICtrlSetState($pwTip, $GUI_HIDE)
                        GUICtrlSetState($pwXTip, $GUI_SHOW)
                        GUICtrlSetState($pw2Tip, $GUI_HIDE)
                        GUICtrlSetState($pw2XTip, $GUI_SHOW)
                    Else                                                                                        ;   If passwords are the same
                        If GUICtrlRead($pwInput) = GUICtrlRead($pw2Input) Then
                            GUICtrlSetState($pwXTip, $GUI_HIDE)
                            GUICtrlSetState($pwTip, $GUI_SHOW)
                            GUICtrlSetState($pw2XTip, $GUI_HIDE)
                            GUICtrlSetState($pw2Tip, $GUI_SHOW)
                        Else
                            GUICtrlSetState($pw2XTip, $GUI_HIDE)
                            GUICtrlSetState($pw2Tip, $GUI_SHOW)
                        EndIf
                    EndIf
                Else
                    If GUICtrlRead($pwInput) <> "" Then
                        $errorTipMsg = "Please enter an alphanumeric password of 8 - 16 characters. Your password may include underscores, dashes or @."
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPwXTip, $errorTipMsg)
                        GUICtrlSetState($pwTip, $GUI_HIDE)
                        GUICtrlSetState($pwXTip, $GUI_SHOW)
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPw2XTip, $errorTipMsg)
                        GUICtrlSetState($pw2Tip, $GUI_HIDE)
                        GUICtrlSetState($pw2XTip, $GUI_SHOW)
                    Else
                        GUICtrlSetState($pwTip, $GUI_HIDE)
                        GUICtrlSetState($pwXTip, $GUI_HIDE)
                        $errorTipMsg = "Please enter an alphanumeric password of 8 - 16 characters. Your password may include underscores, dashes or @."
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hPw2XTip, $errorTipMsg)
                        GUICtrlSetState($pw2Tip, $GUI_HIDE)
                        GUICtrlSetState($pw2XTip, $GUI_SHOW)
                    EndIf
                EndIf
            Case $emailInput
                If isValidEmail(GUICtrlRead($emailInput)) Then              ;   If email is valid
                    If checkExists("Email", GUICtrlRead($emailInput)) Then  ;   If email exists
                        $errorTipMsg = "The email you have entered already exists!"
                        _GUIToolTip_UpdateTipText($hErrorTip, 0, $hEmailXTip, $errorTipMsg)
                        GUICtrlSetState($emailTip, $GUI_HIDE)
                        GUICtrlSetState($emailXTip, $GUI_SHOW)
                    Else
                        GUICtrlSetState($emailXTip, $GUI_HIDE)
                        GUICtrlSetState($emailTip, $GUI_SHOW)
                    EndIf
                ElseIf GUICtrlRead($emailInput) = "" Then                   ;   If email = ""
                    GUICtrlSetState($emailXTip, $GUI_HIDE)
                    GUICtrlSetState($emailTip, $GUI_HIDE)
                Else                                                        ;   If email is invalid
                    $errorTipMsg = "The email you have entered is invalid. It must be in the format of 'text@gmail.com'"
                    _GUIToolTip_UpdateTipText($hErrorTip, 0, $hEmailXTip, $errorTipMsg)
                    GUICtrlSetState($emailTip, $GUI_HIDE)
                    GUICtrlSetState($emailXTip, $GUI_SHOW)
                EndIf
            Case $hInput
                If _CheckInput() Then                   ;   Correct captcha
                    GUICtrlSetState($captchaXTip, $GUI_HIDE)
                    GUICtrlSetState($captchaTip, $GUI_SHOW)
                ElseIf GUICtrlRead($hInput) = "" Then   ;   No captcha
                    GUICtrlSetState($captchaXTip, $GUI_HIDE)
                    GUICtrlSetState($captchaTip, $GUI_HIDE)
                Else                                    ;   Wrong captcha
                    $errorTipMsg = "The captcha entered is incorrect. Please note that the captcha is cAsE-sEnSiTiVe."
                    _GUIToolTip_UpdateTipText($hErrorTip, 0, $hCaptchaXTip, $errorTipMsg)
                    GUICtrlSetState($captchaTip, $GUI_HIDE)
                    GUICtrlSetState($captchaXTip, $GUI_SHOW)
                EndIf
        EndSwitch
    WEnd
EndFunc   ;==>showRegisterForm

Func showLoginForm() ;  Login form
    Global $loginForm = GUICreate("Login Form", 600, 330, -1, -1, $WS_POPUP)                                        ;   Login Form Canvas
    GUISetIcon(@ScriptDir & "\Image\Logo.ico", -1)
    $loginFormPic = GUICtrlCreatePic("image\bg.jpg", 0, 0, 600, 340)                                                ;   Login Form background
    GUICtrlSetState($loginFormPic, $GUI_DISABLE)
    Local $formTitleLabel = GUICtrlCreateLabel("Show Login", 150, 42, 300, 40, $SS_CENTER)                          ;   Title: "Show Login"
    GUICtrlSetFont(-1, 18, 800, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x22cc22)
    Local $userLabel = GUICtrlCreateLabel("Username", 40, 115, 125, 35)                                             ;   Username Label
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFFFFFF)
    Local $pwLabel = GUICtrlCreateLabel("Password", 40, 165, 125, 35)                                               ;   Password Label
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFFFFFF)
    Global $userInput = GUICtrlCreateInput("", 160, 110, 350, 35)                                                   ;   Username Input
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)
    Global $pwInput = GUICtrlCreateInput("", 160, 160, 350, 35, $ES_PASSWORD)                                       ;   Password Input
    GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
    GUICtrlSetLimit(-1, 32)
    Global $errorTipLabel = GUICtrlCreateLabel("*****", 165, 85, 300, 25) ; Errorenous Input Tip
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
    GUICtrlSetColor(-1, 0xFF0000)
    Local $loginButton = GUICtrlCreateButton("Login", 400, 230, 110, 45)                                            ;   Login Button
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
    GUICtrlSetOnEvent($loginButton, "loginButton")
    Local $registerButton = GUICtrlCreateLabel("Register", 425, 205, 70, 30)                                        ;   Register Button
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 11, 600, "", "Tahoma")
    GUICtrlSetColor(-1, 0x22cc22)
    GUICtrlSetOnEvent($registerButton, "showRegisterForm")
    Local $forgetUserButton = GUICtrlCreateLabel("Forgot your Username?", 165, 205, 170, 20)                        ;   Forgot Username Button
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 10, 600, 2, "Tahoma")
    GUICtrlSetOnEvent($forgetUserButton, "showForgetUserForm")
    Local $forgetpwButton = GUICtrlCreateLabel("Forgot your Password?", 165, 230, 170, 20)                          ;   Forgot Password Button
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 10, 600, 2, "Tahoma")
    GUICtrlSetOnEvent($forgetpwButton, "showForgetPwForm")
    Local $copyrightLabel = GUICtrlCreateLabel("Copyright © 2014            Inc. All rights reserved. Ver. " & $PROGRAM_VERSION, 15, 305, 420, 35);    Bottom Label
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetFont(-1, 10, 700, 0, "Tahoma")
    Local $myLabel = GUICtrlCreateLabel("My Company", 135, 305, 420, 35)                                                ;   My Label
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0x22cc22)
    GUICtrlSetFont(-1, 10, 700, 0, "Tahoma")
    Local $exitLabel = GUICtrlCreateLabel("X", WinGetPos($loginForm)[2] - 22, 2, 20, 20)                            ;   Exit Button
    GUICtrlSetFont(-1, 14, 1000, 0, "Tahoma")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xeeeeee)
    GUICtrlSetOnEvent($exitLabel, "exitProgram") ; Prompt exit when Exit Button clicked
    GUISetOnEvent($GUI_EVENT_CLOSE, "exitProgram") ; Prompt exit when user presses Esc
    GUICtrlSetState($errorTipLabel, $GUI_HIDE) ; Hide Errorenous Input Tip upon login form entry
    GUICtrlSetState($userInput, $GUI_FOCUS)
    GUISetState(@SW_SHOW)
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>showLoginForm

Func loginButton()
    Switch checkUserLogin(GUICtrlRead($userInput), GUICtrlRead($pwInput))
        Case 0 ; If credentials have been verified
            GUISetState(@SW_HIDE, $loginForm)
            If checkFirstLogin() == 0 Then
                updateFirstLogin()
            EndIf
            showPlaceholderForm(getID("User", GUICtrlRead($userInput)))
            GUICtrlSetData($pwInput, "")
            GUICtrlSetData($errorTipLabel, "*****")
            GUICtrlSetState($errorTipLabel, $GUI_HIDE)
            GUISetState(@SW_HIDE, $loginForm)
        Case 1 ; If nothing is entered
            GUICtrlSetData($errorTipLabel, "Please enter both your username and password.")
            GUICtrlSetState($errorTipLabel, $GUI_SHOW)
        Case 2 ; If invalid username or password is entered
            GUICtrlSetData($errorTipLabel, "Invalid Username or Password.")
            GUICtrlSetState($errorTipLabel, $GUI_SHOW)
        Case 3 ; If incorrect username or password is entered
            GUICtrlSetData($errorTipLabel, "Incorrect Username or Password.")
            GUICtrlSetState($errorTipLabel, $GUI_SHOW)
    EndSwitch
EndFunc    ;==>loginButton

Func exitProgram()
    If MsgBox($MB_TOPMOST + 68, "Exit and Close", "Would you like to quit the application?") = 6 Then
        Exit
    EndIf
EndFunc    ;==>exitProgram

Func showPlaceholderForm($userID) ;     Placeholder Form
    Local $sFormID = "00_01" ; SAMPLE Form Number
    initializeBeginTable($sFormID, $userID) ; First user who enters this form becomes owner of this form.
    If checkExistingUser($userID, $sFormID) = "" Then
        createRightsTable($sFormID)
        If getUsersCount($sFormID) = 0 Then
            updateRightsTable($userID, $sFormID, $CONST_ADMIN_VAL)
        Else
            updateRightsTable($userID, $sFormID, $CONST_USER_VAL)
        EndIf
    EndIf
    Global $hPlaceholderForm = GUICreate("Placeholder", $CONST_NORMAL_WIDTH, $CONST_MAIN_HEIGHT, -1, -1, -1, $WS_EX_CLIENTEDGE) ;   Placeholder Form Canvas (Original style: $WS_POPUP)
    Local $hPlaceholderLabel = GUICtrlCreateLabel("Landing Page - Esc To Close", 60, 50, 250, 25)       ;   Label Label
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, $COLOR_RED)
    GUICtrlSetFont(-1, 12, 500, 0, "Tahoma")
    Local $hButton_User = GUICtrlCreateButton("User", 60, 100, 80, 30)  ;   SAMPLE Button - Visible to all users (23)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
    showControl_yn($userID, $sFormID, $hButton_User)
    Local $hButton_Admin = GUICtrlCreateButton("Admin", 60, 150, 80, 30);   SAMPLE Button - Visible to only administrators (24)
    GUICtrlSetFont(-1, 14, 800, 0, "Tahoma")
    showControl_yn($userID, $sFormID, $hButton_Admin)
    Local $hCombobox_Exclude = GUICtrlCreateCombo("MutEx", 60, 200, 120, 30, $CBS_DROPDOWNLIST) ; SAMPLE Combo Box - Mutually-exclusive selection of combobox items based on user's rank
    showControl_yn($userID, $sFormID, $hCombobox_Exclude)
    populateCombobox(getUserRank($userID, $sFormID), $hCombobox_Exclude, $sFormID)
    GUISetState(@SW_SHOW) ; @SW_SHOW
    If getFormOwner($sFormID) = $userID Then
        showAdminForm($userID, $hPlaceholderForm) ; SAMPLE Form - Visible to only administrators of landed page
    EndIf
    Global $aRelPos[2]                                              ;   Array for relative position of Admin Form
    GUIRegisterMsg($WM_ENTERSIZEMOVE, "setrelpos")                  ;   Get relative position through function
    GUIRegisterMsg($WM_MOVE, "followme")                            ;   Once relative position retrieved, perform window movement
    GUIRegisterMsg($WM_SYSCOMMAND, "_WM_EXTRACTOR")                 ;   Detect if the primary window is restored
    GUISetOnEvent($GUI_EVENT_CLOSE, "closePlaceholderForm")
    GUISwitch($hPlaceholderForm)
EndFunc   ;==>showPlaceholderForm

Func closePlaceholderForm()
    GUIDelete($hPlaceholderForm)
    GUIDelete($g_hAdminForm)
    GUISetState(@SW_SHOW, $loginForm)
    GUISetState(@SW_ENABLE, $loginForm)
EndFunc
#EndRegion Forms

#Region Admin Form Functions
Func showAdminForm($userID, $hWindow) ; Admin form, always at bottom
    $g_hAdminForm = GUICreate("Admin Form", $CONST_NORMAL_WIDTH, $CONST_NORMAL_HEIGHT, -1, WinGetPos($hWindow)[1] + WinGetPos($hWindow)[3], BitOR($WS_POPUPWINDOW, $WS_CAPTION) , $WS_EX_TOOLWINDOW)
    $hButton_Form = GUICtrlCreateButton("Admin Form Button", 80, 10, 120, 30)
    GUISetState()
EndFunc     ;==>showAdminForm
#EndRegion

#Region First-Time Login Functions
Func checkFirstLogin()
    Local $sTempExecStatement = _Query($g_vSQLInstance, "SELECT islogin FROM islogin")
    Return $sTempExecStatement.Fields("islogin").Value
EndFunc     ;==>checkFirstLogin

Func updateFirstLogin()
    Local $sTempExecStatement = _Query($g_vSQLInstance, "UPDATE islogin SET islogin = 1")
EndFunc     ;==>updateFirstLogin
#EndRegion First-Time Login Functions

#Region begin Table Functions
Func initializeBeginTable($formID, $userID)
    Local $sTempExecStatement1 = _Query($g_vSQLInstance, "CREATE TABLE IF NOT EXISTS begin (formID varchar(45) CHARACTER SET latin1 NOT NULL, userID bigint(20) unsigned NOT NULL, PRIMARY KEY (userID), UNIQUE KEY userID (userID))")
    If _Query($g_vSQLInstance, "SELECT * FROM begin").EOF Then
        Local $sTempExecStatement2 = _Query($g_vSQLInstance, "INSERT INTO begin (formID, userID) VALUES ('" & $formID & "', " & $userID & ")")
    EndIf
EndFunc     ;==>initializeBeginTable

Func getFormOwner($formID)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "SELECT userID FROM begin WHERE formID = '" & $formID & "'")
    Return $sTempExecStatement.Fields("userID").Value
EndFunc     ;==>getFormOwner
#EndRegion begin Table Functions

#Region Rights Table Functions
Func createRightsTable($formID)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "CREATE TABLE IF NOT EXISTS " & $formID & "_rights (userID bigint(20) unsigned NOT NULL, userRank bigint(20) unsigned NOT NULL, PRIMARY KEY (userID), UNIQUE KEY userID (userID))")
EndFunc     ;==>createRightsTable

Func updateRightsTable($userID, $formID, $userRank = $CONST_USER_VAL)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "INSERT INTO " & $formID & "_rights (userID, userRank) VALUES (" & $userID & ", " & $userRank & ")")
EndFunc     ;==>updateRightsTable

Func checkExistingUser($userID, $formID)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "SELECT userID FROM " & $formID & "_rights WHERE userID = " & $userID)
    Return $sTempExecStatement.Fields("userID").Value
EndFunc     ;==>checkExistingUser

Func getUsersCount($formID)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "SELECT userID FROM " & $formID & "_rights")
    Local $iRecordCount = 0
    While Not $sTempExecStatement.EOF
        $iRecordCount = $iRecordCount + 1
        $sTempExecStatement.MoveNext
    WEnd
    Return $iRecordCount
EndFunc     ;==>getUsersCount

Func getUserRank($userID, $formID)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "SELECT userRank FROM " & $formID & "_rights WHERE userID = " & $userID)
    Return $sTempExecStatement.Fields("userRank").Value
EndFunc     ;==>getUserRank

Func showControl_yn($userID, $formID, $hControl)
    If getUserRank($userID, $formID) <= getCtrlRank($hControl) Then
        If $g_iFormMode = 0 Then
            GUICtrlSetState($hControl, $GUI_SHOW)
        EndIf
    ElseIf getUserRank($userID, $formID) = getCtrlRank($hControl) Then
        GUICtrlSetState($hControl, $GUI_SHOW)
    Else
        GUICtrlSetState($hControl, $GUI_DISABLE) ; 2nd parameter should be $GUI_HIDE
    EndIf
EndFunc     ;==>showControl

; Using sample data table instead of actual table schema
Func getCtrlRank($ctrlID)
    Local $sTempExecStatement = _Query($g_vSQLInstance, "SELECT ctrlRank FROM sample_ctrl_data WHERE ctrlID = " & $ctrlID)
    Return $sTempExecStatement.Fields("ctrlRank").Value
EndFunc     ;==>getCtrlRank
#EndRegion Rights Table Functions

#Region ComboBox Functions
Func populateCombobox($rankID, $hCombobox, $formID)
    If $g_iFormMode = 0 Then
        $cmp = ">"
    EndIf
    Local $sTempStatement = "SELECT * FROM sample_combobox_data WHERE minRank " & $cmp & "= " & $rankID
    Local $sTempValue = ""
    Local $sTempExecStatement = _Query($g_vSQLInstance, $sTempStatement)
    While Not $sTempExecStatement.EOF
        $sTempValue = $sTempValue & $sTempExecStatement.Fields("dataItem").Value & "|"
        $sTempExecStatement.MoveNext
    WEnd
    GUICtrlSetData($hCombobox, $sTempValue)
EndFunc     ;==>populateCombobox
#EndRegion

#Region Window Movement Functions
Func followme($hW, $iM, $wp, $lp)
    If $hW <> $hPlaceholderForm Then Return
    Local $xypos = WinGetPos($hPlaceholderForm) ; use WinGetPos rather than the values in $lP
    WinMove($g_hAdminForm, "", $xypos[0] - $aRelPos[0], $xypos[1] - $aRelPos[1])
EndFunc     ;==>followme
Func SetRelPos($hW, $iM, $wp, $lp) ; When the primary window starts moving, we want to know the relative position of the secondary window.
    If $hW <> $hPlaceholderForm Then Return
    Local $gpp = WinGetPos($hPlaceholderForm)
    Local $gsp = WinGetPos($g_hAdminForm)
    If $gsp <> "" Then
        $aRelPos[0] = $gpp[0] - $gsp[0]
        $aRelPos[1] = $gpp[1] - $gsp[1]
    EndIf
EndFunc     ;==>SetRelPos
#EndRegion Window Movement Functions

#Region Restore Window Functions
Func _WM_EXTRACTOR($hWnd, $iMsg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0x0000FFFF)
    Local $hCtrl = $lParam
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Switch $iMsg
        Case $WM_SYSCOMMAND
            Switch $wParam
                Case $SC_RESTORE
                    ConsoleWrite("!Restore window" & @LF)
                    WinActivate($g_hAdminForm)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_EXTRACTOR
#EndRegion

Edited by poila
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

×
×
  • Create New...