AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Select Printer and set default Printer (winNT Win2000 WinXP)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
littlebut0



Joined: 22 Dec 2005
Posts: 33
Location: Belgium

PostPosted: Thu Nov 23, 2006 1:29 am    Post subject: Select Printer and set default Printer (winNT Win2000 WinXP) Reply with quote

Here is a little script which enables you to select the desired printer when you want to print from within your scripts. I've written two versions of the script, one with a quite simple UI and one with a more fancy UI (based on corrupts script to integrate bitmaps and icons in buttons). For the second script to work properly, you need to download the images that belong to the script and extract them in the same directory as your script directory.You can download the images from this location: http://users.skynet.be/amnestylanden/downloads/images.zip

Here is the first script (you don't need to download anything for this script to work properly)
Code:

;====================begin of initialisation section =======================
regread,defaultPrinter,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device
stringsplit,defaultName,defaultPrinter,`,
defaultName := defaultName1
loop,HKCU,Software\Microsoft\Windows NT\CurrentVersion\devices
   {
   printer%A_index% := A_loopRegName
   regread,settings%A_index%
   NumPrinters := A_index
   printer := Printer%A_index%
   if (printer = defaultName)
   defaultNum := A_index
   }
;=====================end of initialization section ==========================





;=============================begin main section =================================================
fileselectfile,FileToPrint,,%A_MyDocuments%\,Select document to print
if FileToPrint<>
gosub print
exitapp
;=============================end of main section ================================================



print:
gosub selectprinters
winwait,Select Printer
winwaitclose,Select Printer
if print
{
runwait,print "%FileToPrint%"
;=================================restore registry=================================
if not defaultprint
   {
   PrintName := printer%defaultNum%
   PrintSet := settings%defaultNum%
   RegWrite,Reg_sz,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device,%PrintName%`,%PrintSet%
   }
;=============================end of restore registry=================================
}
return



selectprinters:
if (NumPrinters = 0)
   {
   msgbox No printers installed on this computer. This program will exit.
   exitapp
   }
Gui, Font, S14 CDefault, Verdana
Gui, Add, Text, x176 y17 w140 h20, Select Printer
Gui, Font, S8 CDefault, Verdana
Gui, Add, ListView, x6 y67 w460 h290 glistviewEvent NoSortHdr, Default|Installed Printers
Gui, Add, Text, x6 y47 w350 h20, Doubleclick the printer you want to use
Gui, Add, Button, x246 y367 w100 h30 gsetasdefault, Set as Default
Gui, Add, Button, x366 y367 w100 h30, Cancel
Gui, Show, x398 y110 h415 w482, Select Printer
loop,% NumPrinters
   {
   printer := Printer%A_index%
   if (A_index = defaultNum)
      {
      LV_add("","default",printer)
      LV_modify(A_index,"Select")
      }
   else LV_add("","",printer)
   }
Return


listviewEvent:
if (A_GuiEvent <> "DoubleClick")
return
PrintName := printer%A_EventInfo%
PrintSet := settings%A_EventInfo%
; set the selected printer temporarily as the default one (Default one will be restored afterwards again)
RegWrite,Reg_sz,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device,%PrintName%`,%PrintSet%
if (A_EventInfo = defaultNum)
defaultprint = 1
else defaultprint = 0
print = 1
Gui,destroy
return


setasdefault:
numFocused := LV_GetCount("selected")
if (numFocused>1)
   {
   msgbox You can only set one printer as the default one
   LV_modify(0,"-Select")
   LV_modify(defaultNum,"select")
   return
   }
if (numfocused = 0)
return
LV_Modify(0,"","")
rowNumber := LV_GetNext(0,"Focused")
LV_Modify(rownumber,"","default")
PrintName := printer%rowNumber%
PrintSet := settings%rowNumber%
; set the selected printer temporarily as the default one (Default one will be restored afterwards again)
RegWrite,Reg_sz,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device,%PrintName%`,%PrintSet%
defaultNum := rowNumber
return

GuiClose:
ButtonCancel:
print = 0
Gui,destroy
return



The second script is a bit more complicate. For this script to work properly, you must download the images from the URL provided above)
Code:

DetectHiddenWindows, On
; Set constants
BS_BITMAP := 128
IMAGE_BITMAP := 0
BS_ICON := 64
IMAGE_ICON := 1
previous = 0
print = 1
loop,HKCU,Software\Microsoft\Windows NT\CurrentVersion\devices
{
printer%A_index% := A_loopRegName
regread,settings%A_index%
NumPrinters := A_index
}

loop,% NumPrinters
{
printer := Printer%A_index%
settings := settings%A_index%
}
regread,defaultPrinter,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device
stringsplit,defaultName,defaultPrinter,`,
defaultName := defaultName1

loop, % NumPrinters
{
if (printer%A_index% = defaultName)
   {
   defaultNum := A_index
   break
   }
}

;===========================end of initialization section ========================================





;=============================begin main section =================================================
fileselectfile,FileToPrint,,%A_MyDocuments%\,Select document to print
if FileToPrint<>
gosub print
exitapp
;=============================end of main section ================================================



print:
gosub selectprinters
winwait,Select Printer
winwaitclose,Select Printer
if print
{
runwait,print "%FileToPrint%"
;=================================restore registry=================================
if not defaultprint
   {
   PrintName := printer%defaultNum%
   PrintSet := settings%defaultNum%
   RegWrite,Reg_sz,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device,%PrintName%`,%PrintSet%
   }
;=============================end of restore registry=================================
}
return


selectprinters:
if (NumPrinters = O)
   {   
   msgbox No Printers Installed: This Program will exit
   exitapp
   }
if (NumPrinters > 16)
   {
   msgbox This application supports max. 16 printers
   exitapp
   }
gui,font,s14
Gui, Add,Text, x216 y5, Select printer
Gui,font,s8
ybutton := -25
ytext := -5
loop, 8
{
ybutton += 60
ytext += 60
setdefault =
printerName := printer%A_index%
if (A_index = defaultNum)
setdefault = checked1 disabled1
Gui,add,checkbox,x50 y%ytext% vch%a_index% %setdefault%  gsetAsDefault
Gui, Add, Button, x116 y%ybutton% w50 h50 gPrinterButton vB%A_index% +%BS_ICON%
Gui, add, text, x+20 vtext%A_index% y%ytext%,%PrinterName%
}
if (NumPrinters < 8)
{
tohide := 8-NumPrinters
loop % tohide
   {
   hidenr := 9-A_index
   guicontrol,hide,ch%hidenr%
   guicontrol,hide,B%hidenr%
   guicontrol,hide,text%hidenr%
   }
}
if (NumPrinters>8)
Gui, add,Button, x210 y540 h20 w60 vNext gbuttonnext +%BS_BITMAP%
gui,add, Button, x300 y540 h20 w60 gButtonCancel +%BS_BITMAP%
gui,add, Button, x+30 h20 w60 gButtonPrint default +%BS_BITMAP%
if (NumPrinters <=8)
hgroup1 := (60*NumPrinters)
else
{
hgroup1 := 480
hgroup2 := (NumPrinters-8)*60
gui,add, groupbox,x15 y27 w85 h%hgroup2% vgroup2 ,Set as Default
guicontrol,hide,group2
}
gui,add, groupbox,x15 y27 w85 h%hgroup1% vgroup1,Set as Default
Gui, Show,+hide h600, Select Printer
;msgbox
; Wait for the window to be created

WinWait, Select Printer

; Get the handle to the window
WinHandle := WinExist("Select Printer")

; Add the images to the buttons - substitute the path(s) to your image(s)
SetButtonGraphic(WinHandle, 2, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 4, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 6, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 8, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 10, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 12, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 14, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
SetButtonGraphic(WinHandle, 16, A_ScriptDir "\printer.ico", "50", "50", IMAGE_ICON)
if (NumPrinters>8)
{
SetButtonGraphic(WinHandle, "17", A_ScriptDir "\btn_next.bmp", "20", "60", IMAGE_BITMAP)
SetButtonGraphic(WinHandle, "18", A_ScriptDir "\btn_cancel.bmp", "20", "60", IMAGE_BITMAP)
SetButtonGraphic(WinHandle, "19", A_ScriptDir "\print2.bmp", "20", "60", IMAGE_BITMAP)
}
else
{
SetButtonGraphic(WinHandle, "17", A_ScriptDir "\btn_cancel.bmp", "20", "60", IMAGE_BITMAP)
SetButtonGraphic(WinHandle, "18", A_ScriptDir "\print2.bmp", "20", "60", IMAGE_BITMAP)
}
; Show the window
Gui, Show,w500 xCentered ycentered, Select Printer
Return




SetButtonGraphic(WindowHWND, CtrlInstance, ImgPath, bHeight=32, bWidth=32, ImgType=0)
{
; WindowHWND = window handle
; CtrlInstance = Button Number (same as the # in ClassNN in Window Spy)
; ImgPath = Path to the image to be displayed
; ImgType = Either BS_BITMAP (default) or BS_ICON are supported (0 or 1)
; bHeight = Image height (default = 32)
; bWidth = Image width (default = 32)

; Set Constants
NULL=
LR_LOADFROMFILE := 16
BM_SETIMAGE := 247

; Find the handle to the Button based on the windle handle and button number
Loop, %CtrlInstance%
{
  CtrlHandle := DllCall("FindWindowExA", "Uint", WindowHWND, "Uint", CtrlHandle, "str", "Button", "str", NULL, "Uint")
  If CtrlHandle = 0
    Return
}

; Load the image from the file and retrieve the image handle
ImgHwnd%CtrlInstance% := DllCall("LoadImage", "UInt", NULL, "Str", ImgPath, "UInt", ImgType, "Int", bWidth, "Int", bHeight, "UInt", LR_LOADFROMFILE, "UInt")

; Assign the image to the button
DllCall("SendMessage", "UInt", CtrlHandle, "UInt", BM_SETIMAGE, "UInt", ImgType,  "UInt", ImgHwnd%CtrlInstance%)

; Return the handle to the image
retImg := ImgHwnd%CtrlInstance%
Return, %retImg%
}


buttonNext:
if previous
{
loop, 8
   {
   guicontrol,,ch%A_index%,0
   guicontrol,show,ch%A_index%
   guicontrol,show,B%A_index%
   printerName := printer%A_index%
   guicontrol,show,text%A_index%
   guicontrol,,text%A_index%,%printerName%
   guicontrol,enable,ch%A_index%
   }
guicontrol,hide,group2
guicontrol,show,group1
guicontrol,disable,ch%defaultNum%
guicontrol,,ch%defaultnum%,1
previous = 0
SetButtonGraphic(WinHandle, "17", A_ScriptDir "\btn_next.bmp", "20", "60", IMAGE_BITMAP)
return
}
guicontrol,hide,group1
guicontrol,show,group2
rest := NumPrinters - 8
loop, % rest
{
num := 8+A_index
printerName := printer%num%
guicontrol,,text%A_index%,%printerName%
}
rest2 := 8-rest
loop, % rest2
{
num := A_index+rest
guicontrol,hide,B%num%
guicontrol,hide,text%num%
guicontrol,hide,ch%num%
guicontrol,enable,ch%defaultNum%
guicontrol,,ch%defaultnum%,0
if (defaultNum>8)
{
defaultch := defaultNum-8
guicontrol,disable,ch%defaultch%
guicontrol,,ch%defaultch%,1
}
SetButtonGraphic(WinHandle, "17", A_ScriptDir "\btn_previous.bmp", "20", "60", IMAGE_BITMAP)
previous = 1
}
return


GuiClose:
ButtonCancel:
print = 0
gui,destroy
return

PrinterButton:
controlgetfocus,focused,Select Printer
stringreplace,focused,focused,Button,,
Selected := round(focused/2)
if previous
selected += 8
PrintName := printer%selected%
PrintSet := settings%selected%
; set the selected printer temporarily as the default one (Default one will be restored afterwards again)
RegWrite,Reg_sz,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device,%PrintName%`,%PrintSet%
print = 1
defaultprint = 0
gui,destroy
return

setAsDefault:
controlgetfocus,focused,Select Printer
defaultch := defaultNum
if (defaultNum > 8)
defaultch := defaultNum-8
guicontrol,enable,ch%defaultch%
guicontrol,,ch%defaultch%,0
stringreplace,focused,focused,Button,,
defaultNum := round((focused+1)/2)
guicontrol,,ch%defaultNum%,1
guicontrol,disable,ch%defaultNum%
if previous
defaultNum += 8
PrintName := printer%defaultNum%
PrintSet := settings%defaultNum%
RegWrite,Reg_sz,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device,%PrintName%`,%PrintSet%
return

buttonprint:
print = 1
defaultprint = 1
gui,destroy
return
Back to top
View user's profile Send private message
adamrgolf



Joined: 28 Dec 2006
Posts: 362

PostPosted: Wed Sep 19, 2007 6:47 am    Post subject: Reply with quote

it would be cool to expand this (i assume pretty easily) to also have a button to remove selected printer from this pop-up.
Back to top
View user's profile Send private message
controlfreak



Joined: 13 Apr 2007
Posts: 7
Location: Unterschleißheim, BY, DE

PostPosted: Sat Feb 09, 2008 10:17 am    Post subject: Alternative Reply with quote

Hi there,

I have written a similar, less complicated and easily expandable script. For those who want to enjoy printer-switching by just a few lines, you can find it here:

http://www.autohotkey.com/forum/viewtopic.php?p=115849

Have fun!
Christian Sentis
_________________
On-on!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group