Jump to content

Help with GUIs


Recommended Posts

I have made a GUI that will launch utilities with Admin rights. It's not pretty and I know I am probably reinventing the wheel. I just wanted to see if I could do it. The GUI works as expected EXCEPT for when clicking on the icon for Set Default Printer. When clicking the first GUI minimizes and a second GUI is created. It contains a dropdown list of the printers installed and it also has a button to set the default printer once the selection is made. The second GUI creates, the dropdown list is populated, I can look at the list and make a selection. However the button does not function (currently it is set to a msgbox) and the GUI cannot be closed. It is as if all commands from this GUI are being ignored. I am currently using GUIonEvent mode, which in my opinion makes the code confusing and cluttered. So, I wouldn't be opposed to switching to the GUIgetMsg method if that will fix my problem. Any help would be greatly appreciated. Attached are the icons and PrintUDF that I am using.

#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.3.0.0
    Author:      erik7426
    
    Script Function:
    Open utilities with Admin privileges.
    
#ce ----------------------------------------------------------------------------

#include <Misc.au3>
#include <PrinterUDF.au3>
#include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GuiComboBox.au3>

Opt("OnExitFunc", "endscript")
Opt("GUIOnEventMode", 1)

$TempFolder = _TempFile()

SplashTextOn("", "Uncompressing...", 200, 50, Default, Default, 33, "", 10, 600)

$AccessibilityIcon = $TempFolder & "\Accessibility.ico"
$AddHardwareIcon = $TempFolder & "\AddHardware.ico"
$AddNewPrinterIcon = $TempFolder & "\AddNewPrinter.ico"
$AddRemoveProgIcon = $TempFolder & "\AddRemoveProgs.ico"
$CMDPromptIcon = $TempFolder & "\CmdPrompt.ico"
$CompMgmtIcon = $TempFolder & "\ComputerMgmt.ico"
$ControlPanelIcon = $TempFolder & "\ControlPanel.ico"
$DateTimeIcon = $TempFolder & "\DateTime.ico"
$DeviceManagerIcon = $TempFolder & "\DeviceMgr.ico"
$DisplayPropertiesIcon = $TempFolder & "\DisplaySettings.ico"
$InternetPropertiesIcon = $TempFolder & "\InternetProperties.ico"
$JavaControlPanelIcon = $TempFolder & "\Java.ico"
$NetworkOptionsIcon = $TempFolder & "\NetworkConnections.ico"
$ODBCSettingsIcon = $TempFolder & "\ODBC.ico"
$TeleModemOptionsIcon = $TempFolder & "\PhoneModem.ico"
$PowerMgmtIcon = $TempFolder & "\PowerCfg.ico"
$RegionalSettingsIcon = $TempFolder & "\RegionalSettings.ico"
$ServicesMgmtIcon = $TempFolder & "\Services.ico"
$SetDefaultPrinterIcon = $TempFolder & "\SetDefaultPrint.ico"
$MultimediaPropertiesIcon = $TempFolder & "\SoundAudioDevices.ico"
$SystemPropertiesIcon = $TempFolder & "\SysProperties.ico"

DirCreate($TempFolder)
FileInstall("Accessibility.ico", $AccessibilityIcon, 1)
FileInstall("AddHardware.ico", $AddHardwareIcon, 1)
FileInstall("AddNewPrinter.ico", $AddNewPrinterIcon, 1)
FileInstall("AddRemoveProgs.ico", $AddRemoveProgIcon, 1)
FileInstall("CmdPrompt.ico", $CMDPromptIcon, 1)
FileInstall("ComputerMgmt.ico", $CompMgmtIcon, 1)
FileInstall("ControlPanel.ico", $ControlPanelIcon, 1)
FileInstall("DateTime.ico", $DateTimeIcon, 1)
FileInstall("DeviceMgr.ico", $DeviceManagerIcon, 1)
FileInstall("DisplaySettings.ico", $DisplayPropertiesIcon, 1)
FileInstall("InternetProperties.ico", $InternetPropertiesIcon, 1)
FileInstall("Java.ico", $JavaControlPanelIcon, 1)
FileInstall("NetworkConnections.ico", $NetworkOptionsIcon, 1)
FileInstall("ODBC.ico", $ODBCSettingsIcon, 1)
FileInstall("PhoneModem.ico", $TeleModemOptionsIcon, 1)
FileInstall("PowerCfg.ico", $PowerMgmtIcon, 1)
FileInstall("RegionalSettings.ico", $RegionalSettingsIcon, 1)
FileInstall("Services.ico", $ServicesMgmtIcon, 1)
FileInstall("SetDefaultPrint.ico", $SetDefaultPrinterIcon, 1)
FileInstall("SoundAudioDevices.ico", $MultimediaPropertiesIcon, 1)
FileInstall("SysProperties.ico", $SystemPropertiesIcon, 1)

$ControlPanel = "rundll32.exe shell32.dll,Control_RunDLL"
$AccessibilityOptions = "rundll32.exe shell32.dll,Control_RunDLL access.cpl"
$AddNewHardware = "rundll32.exe shell32.dll,Control_RunDLL hdwwiz.cpl"
$AddNewPrinter = "rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter"
$AddRemovePrograms = "rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,0"
$AddRemoveWindowsComponent = "rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl,,2"
$CMDPrompt = "cmd.exe /K cd C:\"
$CompMgmt = "cmd /k start compmgmt.msc"
$DateTime = "rundll32.exe shell32.dll,Control_RunDLL timedate.cpl"
$DeviceManager = "cmd /k start devmgmt.msc"
$DisplayProperties = "rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0"
$InternetProperties = "rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl"
$JavaControlPanel = "rundll32.exe shell32.dll,Control_RunDLL jpicpl32.cpl"
$MultimediaProperties = "rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,0"
$NetworkOptions = "rundll32.exe shell32.dll,Control_RunDLL ncpa.cpl"
$ODBCSettings = "rundll32.exe shell32.dll,Control_RunDLL odbccp32.cpl"
$PowerMgmt = "rundll32.exe shell32.dll,Control_RunDLL powercfg.cpl"
$RegionalSettings = "rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,0"
$ServicesMgmt = "cmd /k start services.msc"
$SystemProperties = "rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,0"
$TeleModemOptions = "rundll32.exe shell32.dll,Control_RunDLL telephon.cpl"

$AvailablePrinters = StringSplit(StringTrimRight(StringReplace(GetPrinterList(), @CRLF, "|"), 1), "|")

$Form1 = GUICreate("Trover Admin Utilites", 899, 370, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close", $Form1)
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize", $Form1)
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize", $Form1)
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore", $Form1)
$_AccessIcon = GUICtrlCreateIcon($AccessibilityIcon, 0, 32, 24, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_AccessIconclick")
$_AddWinCompIcon = GUICtrlCreateIcon($AddRemoveProgIcon, 0, 32, 248, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_AddWinCompIconclick")
$_DateIcon = GUICtrlCreateIcon($DateTimeIcon, 0, 296, 136, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_DateIconclick")
$_AddHardIcon = GUICtrlCreateIcon($AddHardwareIcon, 0, 32, 80, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_AddHardIconclick")
$_CMDIcon = GUICtrlCreateIcon($CMDPromptIcon, 0, 32, 304, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_CMDIconclick")
$_DeviceMgrIcon = GUICtrlCreateIcon($DeviceManagerIcon, 0, 296, 192, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_DeviceMgrIconclick")
$_AddPrinterIcon = GUICtrlCreateIcon($AddNewPrinterIcon, 0, 32, 136, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_AddPrinterIconclick")
$_CompMgmtIcon = GUICtrlCreateIcon($CompMgmtIcon, 0, 296, 24, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_CompMgmtIconclick")
$_DisplayIcon = GUICtrlCreateIcon($DisplayPropertiesIcon, 0, 296, 248, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_DisplayIconclick")
$_AddProgsIcon = GUICtrlCreateIcon($AddRemoveProgIcon, 0, 32, 192, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_AddProgsIconclick")
$_ControlPanelIcon = GUICtrlCreateIcon($ControlPanelIcon, 0, 296, 80, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_ControlPanelIconclick")
$_InetPropsIcon = GUICtrlCreateIcon($InternetPropertiesIcon, 0, 296, 304, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_InetPropsIconclick")
$_SoundIcon = GUICtrlCreateIcon($MultimediaPropertiesIcon, 0, 696, 80, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_SoundIconclick")
$_DefaultPrintIcon = GUICtrlCreateIcon($SetDefaultPrinterIcon, 0, 696, 24, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_DefaultPrintIconclick")
$_ServicesIcon = GUICtrlCreateIcon($ServicesMgmtIcon, 0, 496, 304, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_ServicesIconclick")
$_JavaPanelIcon = GUICtrlCreateIcon($JavaControlPanelIcon, 0, 496, 24, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_JavaPanelIconclick")
$_NetworkIcon = GUICtrlCreateIcon($NetworkOptionsIcon, 0, 496, 80, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_NetworkIconclick")
$_ODBCIcon = GUICtrlCreateIcon($ODBCSettingsIcon, 0, 496, 136, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_ODBCIconclick")
$_PowerIcon = GUICtrlCreateIcon($PowerMgmtIcon, 0, 496, 192, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_PowerIconclick")
$_RegionalIcon = GUICtrlCreateIcon($RegionalSettingsIcon, 0, 496, 248, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_RegionalIconclick")
$_SysPropsIcon = GUICtrlCreateIcon($SystemPropertiesIcon, 0, 696, 136, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_SysPropsIconclick")
$_ModemIcon = GUICtrlCreateIcon($TeleModemOptionsIcon, 0, 696, 192, 32, 32, BitOR($SS_NOTIFY, $WS_GROUP))
GUICtrlSetOnEvent(-1, "_ModemIconclick")
$Label1 = GUICtrlCreateLabel("Accessibility Options", 72, 32, 100, 17)
$Label2 = GUICtrlCreateLabel("Add New Hardware", 72, 88, 97, 17)
$Label3 = GUICtrlCreateLabel("Add New Printer", 72, 144, 81, 17)
$Label4 = GUICtrlCreateLabel("Add Remove Programs", 72, 200, 113, 17)
$Label5 = GUICtrlCreateLabel("Add Remove Windows Components", 72, 256, 175, 17)
$Label6 = GUICtrlCreateLabel("CMD Prompt", 72, 312, 64, 17)
$Label7 = GUICtrlCreateLabel("Computer Management", 336, 32, 114, 17)
$Label8 = GUICtrlCreateLabel("Control Panel", 336, 88, 67, 17)
$Label9 = GUICtrlCreateLabel("Date/Time Properties", 336, 144, 105, 17)
$Label10 = GUICtrlCreateLabel("Device Manager", 336, 200, 83, 17)
$Label11 = GUICtrlCreateLabel("Display Settings", 336, 256, 79, 17)
$Label12 = GUICtrlCreateLabel("Internet Properties", 336, 312, 90, 17)
$Label13 = GUICtrlCreateLabel("Java Control Panel", 536, 32, 93, 17)
$Label14 = GUICtrlCreateLabel("Network Connections", 536, 88, 106, 17)
$Label15 = GUICtrlCreateLabel("ODBC Connections", 536, 144, 96, 17)
$Label16 = GUICtrlCreateLabel("Power Settings", 536, 200, 75, 17)
$Label17 = GUICtrlCreateLabel("Regional Settings", 536, 256, 87, 17)
$Label18 = GUICtrlCreateLabel("Services", 536, 312, 45, 17)
$Label19 = GUICtrlCreateLabel("Set Default Printer", 736, 32, 90, 17)
$Label20 = GUICtrlCreateLabel("Sound/Audio Devices", 736, 88, 109, 17)
$Label21 = GUICtrlCreateLabel("System Properties", 736, 144, 88, 17)
$Label22 = GUICtrlCreateLabel("Telephone/Modem Options", 736, 200, 134, 17)
$Group1 = GUICtrlCreateGroup("", 16, 8, 241, 345)
$Group2 = GUICtrlCreateGroup("", 280, 8, 177, 345)
$Group3 = GUICtrlCreateGroup("", 480, 8, 177, 345)
$Group4 = GUICtrlCreateGroup("", 680, 8, 201, 345)

SplashOff()
Sleep(500)
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func _AccessIconclick()
    myLaunch($AccessibilityOptions)
EndFunc  ;==>_AccessIconclick

Func _AddHardIconclick()
    myLaunch($AddNewHardware)
EndFunc  ;==>_AddHardIconclick

Func _AddPrinterIconclick()
    myLaunch($AddNewPrinter)
EndFunc  ;==>_AddPrinterIconclick

Func _AddProgsIconclick()
    myLaunch($AddRemovePrograms)
EndFunc  ;==>_AddProgsIconclick

Func _AddWinCompIconclick()
    myLaunch($AddRemoveWindowsComponent)
EndFunc  ;==>_AddWinCompIconclick

Func _CMDIconclick()
    myLaunch($CMDPrompt)
EndFunc  ;==>_CMDIconclick

Func _CompMgmtIconclick()
    myLaunch($CompMgmt, "", @SW_HIDE)
EndFunc  ;==>_CompMgmtIconclick

Func _ControlPanelIconclick()
    myLaunch($ControlPanel)
EndFunc  ;==>_ControlPanelIconclick

Func _DateIconclick()
    myLaunch($DateTime)
EndFunc  ;==>_DateIconclick

Func _DefaultPrintIconclick()
    If IsArray($AvailablePrinters) Then
        Sleep(50)
    Else
        MsgBox(0, "Error", "There are no printers currently installed")
        Return
    EndIf
    GUISetState(@SW_MINIMIZE, $Form1)
    $PickPrinter = GUICreate("Assign Default Printer", 399, 85, 193, 125)
    GUISetOnEvent($GUI_EVENT_CLOSE, "PickPrinterClose", $PickPrinter)

    $PrintList = GUICtrlCreateCombo("", 16, 32, 273, 25, $CBS_DROPDOWNLIST)
    $PrintDropDown = GUICtrlCreateLabel("Select Printer", 16, 8, 83, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    For $pCount = 1 To $AvailablePrinters[0]
        _GUICtrlComboBox_AddString($PrintList, $AvailablePrinters[$pCount])
    Next

    $MakeDefault = GUICtrlCreateButton("Make Default", 304, 30, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "MakeDefaultClick")
    GUISetState(@SW_SHOW, $PickPrinter)

    While 1
        Sleep(100)
    WEnd

EndFunc  ;==>_DefaultPrintIconclick

Func _DeviceMgrIconclick()
    myLaunch($DeviceManager, "", @SW_HIDE)
EndFunc  ;==>_DeviceMgrIconclick

Func _DisplayIconclick()
    myLaunch($DisplayProperties)
EndFunc  ;==>_DisplayIconclick

Func _InetPropsIconclick()
    myLaunch($InternetProperties)
EndFunc  ;==>_InetPropsIconclick

Func _JavaPanelIconclick()
    $JavaPath = @ProgramFilesDir & "\Java"
    $JavaEXE = "javacpl.exe"
    $FoundJava = FilePath($JavaPath, $JavaEXE)

    myLaunch($FoundJava & "\" & $JavaEXE)
EndFunc  ;==>_JavaPanelIconclick

Func _ModemIconclick()
    myLaunch($TeleModemOptions)
EndFunc  ;==>_ModemIconclick

Func _NetworkIconclick()
    myLaunch($NetworkOptions)
EndFunc  ;==>_NetworkIconclick

Func _ODBCIconclick()
    myLaunch($ODBCSettings)
EndFunc  ;==>_ODBCIconclick

Func _PowerIconclick()
    myLaunch($PowerMgmt)
EndFunc  ;==>_PowerIconclick

Func _RegionalIconclick()
    myLaunch($RegionalSettings)
EndFunc  ;==>_RegionalIconclick

Func _ServicesIconclick()
    myLaunch($ServicesMgmt, "", @SW_HIDE)
EndFunc  ;==>_ServicesIconclick

Func _SoundIconclick()
    myLaunch($MultimediaProperties)
EndFunc  ;==>_SoundIconclick

Func _SysPropsIconclick()
    myLaunch($SystemProperties)
EndFunc  ;==>_SysPropsIconclick

Func Form1Close()
    Exit
EndFunc  ;==>Form1Close

Func Form1Maximize()
    Sleep(50)
EndFunc  ;==>Form1Maximize

Func Form1Minimize()
    GUISetState(@SW_MINIMIZE)
EndFunc  ;==>Form1Minimize

Func Form1Restore()
    GUISetState(@SW_RESTORE)
EndFunc  ;==>Form1Restore

Func MakeDefaultClick()
    $ReadSelection = _GUICtrlComboBox_GetCurSel($PrintList)
    _GUICtrlComboBox_GetLBText($PrintList, $ReadSelection, $SelectedPrinter)
    MsgBox(0, "", $SelectedPrinter)
;PrinterSetAsDefault($SelectedPrinter)

EndFunc  ;==>MakeDefaultClick

Func PickPrinterClose()
    GUIDelete($PickPrinter)
    GUISetState(@SW_RESTORE, $Form1)
EndFunc  ;==>PickPrinterClose

Func myLaunch($myCommand, $myWorkingDir = "", $myFlags = @SW_SHOW)
    If $RunAsSet = 1 Then
        RunAs($UserName, $rights, $Password, 0, $myCommand, $myWorkingDir, $myFlags)
    Else
        Run($myCommand, $myWorkingDir, $myFlags)
    EndIf
EndFunc  ;==>myLaunch

Func FilePath($path, $file)
    Local $s, $pf
    If FileExists($path) Then
        $attrib = FileGetAttrib($path)
        If Not @error Then
            If StringInStr($attrib, "D") Then
                $s = FileFindFirstFile($path & "\" & $file)
                FileClose($s)
                If $s <> -1 Then Return $path
                $s = FileFindFirstFile($path & "\*.*")
                While 1
                    $f = FileFindNextFile($s)
                    If @error Then ExitLoop
                    If Not ($f = "." Or $f = "..") Then
                        $pf = $path & "\" & $f
                        $fp = FilePath($pf, $file)
                        If $fp <> "0" Then Return $fp
                    EndIf
                WEnd
                FileClose($s)
            EndIf
        EndIf
    EndIf
EndFunc  ;==>FilePath

Func endscript()
    DirRemove($TempFolder, 1)
EndFunc  ;==>endscript

RunAs_util.zip

PrinterUDF.au3

Link to comment
Share on other sites

I think you need a way to exit the while loop in the second GUI (Default printer function). I would use something like

Gobal $GLOBAL_SECONDLOOP = True
While $GLOBAL_SECONDLOOP
   Sleep(100)
WEnd

In your "PickPrinterClose" Function you need to set $GLOBAL_SECONDLOOP=False.

Just a theory...

Bob

Edited by YellowLab

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

I think you need a way to exit the while loop in the second GUI (Default printer function). I would use something like

Gobal $GLOBAL_SECONDLOOP = True
While $GLOBAL_SECONDLOOP
   Sleep(100)
WEnd

In your "PickPrinterClose" Function you need to set $GLOBAL_SECONDLOOP=False.

Just a theory...

Bob

This did not change the way the second GUI is functioning. I may have forgot to mention in my first post that I am able to minimize and restore the second GUI, but the Close and my button do not function.

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...