Send menu item to other function?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kunkel321
Posts: 1149
Joined: 30 Nov 2015, 21:19

Send menu item to other function?

Post by kunkel321 » 14 May 2024, 12:20

Given this code, the menu displays as expected, but the MsgBox is always blank when I click a menu item. I want it to show the selected menu item... What am I doing wrong??

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
PrinterToolMenu()
PrinterToolMenu(*)
{
	Switches := "
	(
	/e display printing preferences
	/ge enum per machine printer connections
	/k print test page to specified printer
	/o display printer queue view
	/p display printer properties
	/s display server properties
	/y set printer as the default
	/Xg get printer settings
	/? help this message
	)"

	dfm := Menu()
	Loop Parse, Switches, "`n"
		dfm.Add(A_Loopfield, (*) => chosenSwitch( A_Loopfield ))
	dfm.Show()
}

chosenSwitch(var)
{	MsgBox var
	; Loop Parse, printerlist, "`n" 
	; {	thisRadioVal := df["Radio" . a_index].value
	; 	If thisRadioVal != 0
	; 	newDefault := a_loopfield
	; }
	; global mySwitch := StrSplit(item, " ")[1]
	; RunWait("C:\Windows\System32\RUNDLL32.exe PRINTUI.DLL, PrintUIEntry  " mySwitch "   /n `"" newDefault "`"") 
}
ste(phen|ve) kunkel

User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Send menu item to other function?

Post by boiler » 14 May 2024, 12:37

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
PrinterToolMenu()
PrinterToolMenu(*)
{
	Switches := "
	(
	/e display printing preferences
	/ge enum per machine printer connections
	/k print test page to specified printer
	/o display printer queue view
	/p display printer properties
	/s display server properties
	/y set printer as the default
	/Xg get printer settings
	/? help this message
	)"

	dfm := Menu()
	Loop Parse, Switches, "`n"
		dfm.Add(A_Loopfield, chosenSwitch.Bind(A_Loopfield ))
	dfm.Show()
}

chosenSwitch(var, *)
{	MsgBox var
	; Loop Parse, printerlist, "`n" a
	; {	thisRadioVal := df["Radio" . a_index].value
	; 	If thisRadioVal != 0
	; 	newDefault := a_loopfield
	; }
	; global mySwitch := StrSplit(item, " ")[1]
	; RunWait("C:\Windows\System32\RUNDLL32.exe PRINTUI.DLL, PrintUIEntry  " mySwitch "   /n `"" newDefault "`"") 
}

User avatar
kunkel321
Posts: 1149
Joined: 30 Nov 2015, 21:19

Re: Send menu item to other function?

Post by kunkel321 » 14 May 2024, 13:24

Thank you, Sir.
ste(phen|ve) kunkel

User avatar
kunkel321
Posts: 1149
Joined: 30 Nov 2015, 21:19

Re: Send menu item to other function?

Post by kunkel321 » 14 May 2024, 13:34

Strange... The code that you (Boiler) just posted works, but when it's with the rest of the code, I get
Error: Too many parameters passed to function.

Specifically: chosenSwitch

065: Loop Parse Switches, "
"
066: dfm.Add(A_Loopfield, chosenSwitch.Bind(A_Loopfield ))
▶ 067: dfm.Show()
068: }
071: {
Here is the code:

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

;===============================================================================
;   Get/Set my default printer
; A tool to allow user to check and/or change default printer.
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=118596&p=526363#p526363
; By Kunkel321 with help from Garry. Partly auto-converted from v1, partly rewritten.
;===============================================================================
+!p:: ; Shift+Alt+P
PrinterTool(*)
{	; TraySetIcon("shell32.dll","107") ; Icon of a printer with a green checkmark.
	
	guiColor := "F0F8FF" ; "F0F8FF" is light blue
	pfontColor := "003366" ; "003366" is dark blue
	Global df := ""

	; ===== Get default printer name. ======
	defaultPrinter := RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device")

	; ==== Get list of installed printers ======
	Global printerlist := ""
	Loop Reg, "HKCU\Software\Microsoft\Windows NT\CurrentVersion\devices"
		printerlist := printerlist . "" . A_loopRegName . "`n"
	printerlist := SubStr(printerlist, 1, strlen(printerlist) - 2)

	df := Gui()
	df.Title := "Default Printer Changer"
	df.OnEvent("Close", ButtonCancel)
	df.OnEvent("Escape", ButtonCancel)
	df.BackColor := guiColor
	df.SetFont("s12 bold c" . pFontColor)
	df.Add("Text", , "Set A Default Printer ...")
	df.SetFont("s11")
	Loop Parse, printerlist, "`n"
		If InStr(defaultPrinter, A_LoopField)
			df.AddRadio("checked vRadio" . a_index, a_loopfield)
		Else
			df.AddRadio("vRadio" . a_index, a_loopfield)
	df.AddButton("default", "Set Printer").OnEvent("Click", ButtonSet)
	df.AddButton("x+10", "Print &Queue").OnEvent("Click", ButtonQue)
	df.AddButton("x+10", "Control Panel").OnEvent("Click", ButtonCtrlPanel)
	df.AddButton("x+10", "Cancel").OnEvent("Click", ButtonCancel)
	df.Show()
	df.OnEvent("ContextMenu", PrinterToolMenu)

}

PrinterToolMenu(*)
{
	Switches := "
	(
	/e display printing preferences
	/ge enum per machine printer connections
	/k print test page to specified printer
	/o display printer queue view
	/p display printer properties
	/s display server properties
	/y set printer as the default
	/Xg get printer settings
	/? help this message
	)"

	dfm := Menu()
	Loop Parse, Switches, "`n"
		dfm.Add(A_Loopfield, chosenSwitch.Bind(A_Loopfield ))
	dfm.Show()
}

chosenSwitch(var)
{	MsgBox var
	; Loop Parse, printerlist, "`n" 
	; {	thisRadioVal := df["Radio" . a_index].value
	; 	If thisRadioVal != 0
	; 	newDefault := a_loopfield
	; }
	; global mySwitch := StrSplit(item, " ")[1]
	; RunWait("C:\Windows\System32\RUNDLL32.exe PRINTUI.DLL, PrintUIEntry  " mySwitch "   /n `"" newDefault "`"") ; sets the printer
}

ButtonSet(*)
{ Loop Parse, printerlist, "`n" {
	thisRadioVal := df["Radio" . a_index].value
	If thisRadioVal != 0
		newDefault := a_loopfield
}
; ==== Set new default printer =====
RunWait("C:\Windows\System32\RUNDLL32.exe PRINTUI.DLL, PrintUIEntry /y /n `"" newDefault "`"") ; sets the printer
df.Destroy()
}

ButtonQue(*)
{ viewThis := ""
	Loop Parse, printerlist, "`n" {
		thisRadioVal := df["Radio" . a_index].value
		If thisRadioVal != 0
			viewThis := a_loopfield
	}
	; ==== View print queue for selected =====
	RunWait("rundll32 printui.dll, PrintUIEntry /o /n `"" viewThis "`"")  ;- display printer queue view -User Garry
	df.Destroy()
}

ButtonCtrlPanel(*)
{ Run("control printers")
	df.Destroy()
	printerlist := ""
}  

ButtonCancel(*)
{ df.Destroy()
	printerlist := ""
}
Any idea why it is different, when used with the rest of the code?
ste(phen|ve) kunkel

RussF
Posts: 1298
Joined: 05 Aug 2021, 06:36

Re: Send menu item to other function?

Post by RussF » 14 May 2024, 14:40

You forgot the asterisk.

Code: Select all

chosenSwitch(var,*)
Russ

User avatar
kunkel321
Posts: 1149
Joined: 30 Nov 2015, 21:19

Re: Send menu item to other function?

Post by kunkel321 » 14 May 2024, 15:01

Thanks Russ!
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v2)”