Sign in to follow this  
Followers 0
ScottL

Printer Selector GUI

10 posts in this topic

Hi-

I work in an environment with a lot of different network printers and everyone has different set mapped to their PC. I wanted to create a GUI that reads through the windows registry and creates a autosizing GUI with up to 10 different possible printer choices, if there were that many mapped to the local machine. Then, using radio buttons, users can choose a specific printer, hit OK, and the script will write the new printer choice to the windows registry.

I have finished the code, and it works well, but I was hoping to get some general advice on optimizing it. There are a lot of lines that I think I might be able to condense, but I'm not enitrely sure how.

Any thoughts would be appreciated.

Thanks

CODE
#include <File.au3>

#include <Array.au3>

#include <GUIConstants.au3>

Global $printers

Global $printercount

Global $regprinters

Global $currentprinter

Global $windir

Global $regwriteprinters

$regprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices"

$regwriteprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"

$currentprinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\","Device")

If FileExists("C:\WINNT") Then

$windir = "C:\WINNT"

Else

$windir = "C:\WINDOWS"

EndIf

$printers = $windir & "\" & "AUIPrinters.txt"

_FileCreate($printers)

FileOpen($printers, 2)

For $i= 1 to 10

$var = RegEnumVal($regprinters, $i)

If @error <> 0 then ExitLoop

FileWrite($printers, $var & @CRLF)

Next

FileClose($printers)

dim $PrinterList

_FileReadToArray($printers,$PrinterList)

$numberlines = _FileCountLines($printers)

$emptyindex = _ArrayMinIndex($PrinterList,0,0)

$emptyvalue = _ArrayMin($PrinterList,0,0)

If $emptyvalue = "" Then

_ArrayDelete($PrinterList, $emptyindex)

$numberlines = $emptyindex - 1

EndIf

$printercount = $numberlines - 1

_FileCreate($printers)

_FileWriteFromArray($printers,$PrinterList,1)

Dim $PrinterListArray

_FileReadToArray($printers,$PrinterListArray)

_FileCreate($printers)

FileOpen($printers, 2)

For $p = 1 to $PrinterListArray[0]

$printreg = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices\",$PrinterListArray[$p])

FileWrite($printers, $PrinterListArray[$p] & "," & $printreg & @CRLF)

Next

FileClose($printers)

dim $PrinterListAdj

_FileReadToArray($printers,$PrinterListAdj)

$numberlines = _FileCountLines($printers)

$emptyindex = _ArrayMinIndex($PrinterListAdj,0,0)

$emptyvalue = _ArrayMin($PrinterListAdj,0,0)

If $emptyvalue = "" Then

_ArrayDelete($PrinterListAdj, $emptyindex)

$numberlines = $emptyindex - 1

EndIf

_FileCreate($printers)

_FileWriteFromArray($printers,$PrinterListAdj,1)

$numberlines = _FileCountLines($printers)

global $printercount1

global $printercount2

global $printercount3

global $printercount4

global $printercount5

global $printercount6

global $printercount7

global $printercount8

global $printercount9

global $printercount10

Global $groupheight

Global $guiheight

Global $buttontop

$groupheight = ($numberlines + 1) * 30

$guiheight = $groupheight + 50

$buttontop = $groupheight + 20

If $numberlines >= 1 Then

$printercount1 = "Y"

EndIf

If $numberlines >= 2 Then

$printercount2 = "Y"

EndIf

If $numberlines >= 3 Then

$printercount3 = "Y"

EndIf

If $numberlines >= 4 Then

$printercount4 = "Y"

EndIf

If $numberlines >= 5 Then

$printercount5 = "Y"

EndIf

If $numberlines >= 6 Then

$printercount6 = "Y"

EndIf

If $numberlines >= 7 Then

$printercount7 = "Y"

EndIf

If $numberlines >= 8 Then

$printercount8 = "Y"

EndIf

If $numberlines >= 9 Then

$printercount9 = "Y"

EndIf

If $numberlines >= 10 Then

$printercount10 = "Y"

EndIf

Opt("GUIOnEventMode", 1)

GUICreate("Printer Selector", 400, $guiheight)

$font = "Verdana"

GUISetFont (10, 400, 0, $font)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GUISetFont (10, 400, 0, $font)

GUICtrlCreateGroup("Available Printers",10, 10, 380, $groupheight)

If $printercount1 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio1 = GUICtrlCreateRadio ($PrinterList[1], 20, 30, 350, 20)

If $currentprinter = $PrinterListAdj[1] Then

GUICtrlSetState ($radio1,$GUI_CHECKED)

EndIf

EndIf

If $printercount2 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio2 = GUICtrlCreateRadio ($PrinterList[2], 20, 60, 350, 20)

If $currentprinter = $PrinterListAdj[2] Then

GUICtrlSetState ($radio2,$GUI_CHECKED)

EndIf

EndIf

If $printercount3 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio3 = GUICtrlCreateRadio ($PrinterList[3], 20, 90, 350, 20)

If $currentprinter = $PrinterListAdj[3] Then

GUICtrlSetState ($radio3,$GUI_CHECKED)

EndIf

EndIf

If $printercount4 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio4 = GUICtrlCreateRadio ($PrinterList[4], 20, 120, 350, 20)

If $currentprinter = $PrinterListAdj[4] Then

GUICtrlSetState ($radio4,$GUI_CHECKED)

EndIf

EndIf

If $printercount5 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio5 = GUICtrlCreateRadio ($PrinterList[5], 20, 150, 350, 20)

If $currentprinter = $PrinterListAdj[5] Then

GUICtrlSetState ($radio5,$GUI_CHECKED)

EndIf

EndIf

If $printercount6 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio6 = GUICtrlCreateRadio ($PrinterList[6], 20, 180, 350, 20)

If $currentprinter = $PrinterListAdj[6] Then

GUICtrlSetState ($radio6,$GUI_CHECKED)

EndIf

EndIf

If $printercount7 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio7 = GUICtrlCreateRadio ($PrinterList[7], 20, 210, 350, 20)

If $currentprinter = $PrinterListAdj[7] Then

GUICtrlSetState ($radio7,$GUI_CHECKED)

EndIf

EndIf

If $printercount8 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio8 = GUICtrlCreateRadio ($PrinterList[8], 20, 240, 350, 20)

If $currentprinter = $PrinterListAdj[8] Then

GUICtrlSetState ($radio8,$GUI_CHECKED)

EndIf

EndIf

If $printercount9 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio9 = GUICtrlCreateRadio ($PrinterList[9], 20, 270, 350, 20)

If $currentprinter = $PrinterListAdj[9] Then

GUICtrlSetState ($radio9,$GUI_CHECKED)

EndIf

EndIf

If $printercount10 = "Y" Then

GUISetFont (10, 400, 0, $font)

$radio10 = GUICtrlCreateRadio ($PrinterList[10], 20, 300, 350, 20)

If $currentprinter = $PrinterListAdj[10] Then

GUICtrlSetState ($radio10,$GUI_CHECKED)

EndIf

EndIf

GUISetFont (10, 400, 0, $font)

$okbutton = GUICtrlCreateButton("OK", 10, $buttontop, 50, 25)

GUICtrlSetOnEvent($okbutton, "OKButton")

GUISetState ()

While 1

Sleep(1000)

WEnd

Func OKButton()

If GUICtrlRead($radio1)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[1])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[1], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio2)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[2])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[2], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio3)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[3])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[3], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio4)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[4])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[4], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio5)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[5])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[5], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio6)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[6])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[6], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio7)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[7])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[7], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio8)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[8])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[8], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio9)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[9])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[9], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

ElseIf GUICtrlRead($radio10)=1 Then

RegWrite($regwriteprinters, "Device", "REG_SZ", $PrinterListAdj[10])

$handle = GUICreate("Printer Set", 600, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Your printer has been set to " & $PrinterList[10], 20, 10)

GUISetState(@SW_SHOW)

Sleep(2000)

GUISetState(@SW_HIDE)

EndIf

$handle = GUICreate("Please Wait", 250, 50)

GUISetFont (10, 400, 0, $font)

GUICtrlCreateLabel("Exiting ...", 20, 10)

GUISetState(@SW_SHOW)

Sleep(3000)

GUISetState(@SW_HIDE)

Exit

EndFunc

Func CLOSEClicked()

$handle = GUICreate("Please Wait", 250, 50)

GUISetFont (10, 400, "", $font)

GUICtrlCreateLabel("Exiting ...", 20, 10)

GUISetState(@SW_SHOW)

Sleep(3000)

GUISetState(@SW_HIDE)

Exit

EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Instead of radios I will use listview. Look at my signature NetPrinter, it may give you some ideas.

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Instead of radios I will use listview. Look at my signature NetPrinter, it may give you some ideas.

That's a really nice little app! I like the simplicity of the interface.

I'll take a look at the listview possibility. I like the radio buttons because I can control how many populate, as well as the size of the GUI based on the number of printers installed. Maybe the same thing can be done with listview, but I've never looked at it.

Thanks for the advice!

Share this post


Link to post
Share on other sites

Instead of radios I will use listview. Look at my signature NetPrinter, it may give you some ideas.

Thanks for posting the NetPrinter script! This saved me from re-inventing the wheel! :)

Share this post


Link to post
Share on other sites

Vaner had created a "system tray" print control script with similar functionality a while back that may help reduce some of your script size enumerating your printer list.

It can be found -> here.


- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

Vaner had created a "system tray" print control script with similar functionality a while back that may help reduce some of your script size enumerating your printer list.

It can be found -> here.

Cool! thanks for the link!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Did some cleanup of this code so that it is more dynamic (no 10 printers limit).

#include <Array.au3> 
#include <GUIConstants.au3>

Global $printers
Global $printercount
Global $regprinters
Global $currentprinter
Global $regwriteprinters
Global $printer_list[1]
Global $printer_list_ext[1]
global $printer_radio_array[1]
global $imprimante

$regprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices"
$regwriteprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
$currentprinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\","Device")

dim $i = 1
dim $erreur_reg = false
while not $erreur_reg
    $imprimante = RegEnumVal($regprinters, $i)
    $erreur_reg = @error
    if not $erreur_reg then
        _ArrayAdd($printer_list,$imprimante)
        _ArrayAdd($printer_list_ext, $imprimante & "," & RegRead($regprinters,$imprimante))
    endif
    $i = $i + 1
wend
_ArrayDelete($printer_list,0) 
_ArrayDelete($printer_list_ext,0) 

if ubound($printer_list) >= 2 then ;if more that 2 printers available, we show the dialog
    dim $groupheight = (ubound($printer_list) + 1) * 30
    dim $guiheight = $groupheight + 50
    dim $buttontop = $groupheight + 20
    Opt("GUIOnEventMode", 1)
    GUICreate("Choose default printer", 400, $guiheight)
    dim $font = "Verdana"
    GUISetFont (10, 400, 0, $font)
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUISetFont (10, 400, 0, $font)
    GUICtrlCreateGroup("Available printers:",10, 10, 380, $groupheight)
    dim $position_vertical = 0
    For $i=0 to ubound($printer_list)-1 step 1
        GUISetFont (10, 400, 0, $font)
        $position_vertical = $position_vertical + 30
        $radio = GUICtrlCreateRadio ($printer_list[$i], 20, $position_vertical, 350, 20)
        _ArrayAdd($printer_radio_array,$radio)
        If $currentprinter = $printer_list_ext[$i] Then
            GUICtrlSetState ($radio,$GUI_CHECKED)
        endif
    next
    _ArrayDelete($printer_radio_array,0)
    GUISetFont (10, 400, 0, $font)
    $okbutton = GUICtrlCreateButton("OK", 10, $buttontop, 50, 25)
    GUICtrlSetOnEvent($okbutton, "OKButton")
    GUISetState ()
    While 1
        Sleep(1000)
    WEnd
endif

Func OKButton()
    for $i=0 to ubound($printer_radio_array)-1 step 1
        If GUICtrlRead($printer_radio_array[$i])=1 Then
            RegWrite($regwriteprinters, "Device", "REG_SZ", $printer_list_ext[$i])
        endif
    next
    exit
endfunc

Func CLOSEClicked()
    Exit
EndFunc
Edited by mfecteau

Share this post


Link to post
Share on other sites

This is superb code, which is extremely useful in some applications that I am writing. I have one beginner's question:

In the code in the preceding message, is there any way to make the Enter key perform the same function as the OK button? It is slightly easier for a user to choose a printer and then press Enter instead of going to the OK button. Obviously, this is a minor issue, but I would be very grateful to learn if there is a way to make Enter and OK do the same thing in this GUI control.

Share this post


Link to post
Share on other sites

emendelson,

Look at GUISetAccelerators in the Help file - it lets you link a key combination to a control, rather like a HotKey except it only works when your GUI is active and so does not interfere with other running apps. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

Exactly what I needed to know. Thank you!

Share this post


Link to post
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
Sign in to follow this  
Followers 0