Page 1 of 1

PicSwitch - Switch, Checkbox controls with picture

Posted: 08 Dec 2023, 08:59
by tranht17
Screenshot 2023-12-08 085737.png
Screenshot 2023-12-08 085737.png (25.88 KiB) Viewed 1161 times


PicSwitch.ahk

Code: Select all

;================================================================================
; PicSwitch - Switch, Checkbox controls with picture
; tranht17
; 2023/12/12
;================================================================================
Class PicSwitch Extends Gui.Text {
    Static __New() {
        Gui.Prototype.AddPicSwitch:=this.AddPicSwitch
    }
	Static AddPicSwitch(Options:="", sText:="", iValue:=0, SOptions:="") {
		wPic:=hPic:=36
		If SOptions && SOptions.Has("SWidth")
			wPic:=SOptions["SWidth"]
		If SOptions && SOptions.Has("SHeight")
			hPic:=SOptions["SHeight"]
		
		ctlTxt:=this.AddText("BackgroundTrans 0x200 " Options,sText)
        ctlPic:=this.AddPic("BackgroundTrans w" wPic " h" hPic " yp")
		
		ctlTxt.GetPos(&X, &Y)
		ctlPic.Move(X, Y)
		ctlTxt.Move(X+wPic+3, Y,,hPic)
		
		ctlEnabled:=ctlTxt.Enabled
		ctlVisible:=ctlTxt.Visible
		ctlPic.Enabled:=ctlEnabled
		ctlPic.Visible:=ctlVisible
		
		
        ctlTxt.base:=PicSwitch.Prototype
		ctlTxt.SPic:=ctlPic
		ctlTxt._Value:=iValue
		ctlTxt._Enabled:=ctlEnabled
		ctlTxt._Visible:=ctlVisible

		ctlTxt.OnEvent("click",ObjBindMethod(ctlTxt,"_ClickChangeValue"))
		ctlPic.OnEvent("click",ObjBindMethod(ctlTxt,"_ClickChangeValue"))
		
		ctlTxt.SOpt:=Map()
		If SOptions
			ctlTxt.SOpt:=SOptions
		
		If !ctlTxt.SOpt.Has("Value1Icon")
			ctlTxt.SOpt["Value1Icon"]:="*icon102 imageres.dll"
		If !ctlTxt.SOpt.Has("Value0Icon")
			ctlTxt.SOpt["Value0Icon"]:="*icon101 imageres.dll"
		If !ctlTxt.SOpt.Has("Value0DisabledIcon")
			ctlTxt.SOpt["Value0DisabledIcon"]:="*icon100 imageres.dll"
		If !ctlTxt.SOpt.Has("Value1DisabledIcon")
			ctlTxt.SOpt["Value1DisabledIcon"]:="*icon100 imageres.dll"
		If !ctlTxt.SOpt.Has("SWidth")
			ctlTxt.SOpt["SWidth"]:=wPic
		If !ctlTxt.SOpt.Has("SHeight")
			ctlTxt.SOpt["SHeight"]:=hPic
		ctlTxt.RefreshStatusIcon
        return ctlTxt
    }
	Type => "PicSwitch"
	Value {
        get => this._Value
        set {
			if (this._Value!=value) {
				this._Value:=value
				this.RefreshStatusIcon
			}
			Return value
		}
    }
	Enabled	{
		get => this._Enabled
        set {
			if (this._Enabled!=value) {
				super.Enabled:=value
				this.SPic.Enabled:=value
				this._Enabled:=value
				this.RefreshStatusIcon
			}
			Return value
		}
	}
	Visible	{
		get => this._Visible
        set {
			if (this._Visible!=value) {
				super.Visible:=value
				this.SPic.Visible:=value
				this._Visible:=value
			}
			Return value
		}
	}
	Move(X?, Y?, W?, H?) {
		this.SPic.Move(X?, Y?,, H?)
		this.SPic.GetPos(,,&wSPic)
		super.Move(IsSet(X)?X+wSPic+3:(X?), Y?, W?, H?)
	}
	_ClickChangeValue(*) {
		this._Value:=!this._Value
		this.RefreshStatusIcon
	}
	RefreshStatusIcon(*) {
		this.SPic.GetPos(&sX,, &sW, &sH)
		If sW!=this.SOpt["SWidth"] {
			this.SPic.Move(,, this.SOpt["SWidth"])
			super.Move(sX+this.SOpt["SWidth"]+3)
		}
		If sH!=this.SOpt["SHeight"] {
			this.Move(,,, this.SOpt["SHeight"])
		}
		t:=this._Value (this._Enabled?"":"Disabled")
		this.SPic.Value:=this.SOpt["Value" t "Icon"]
   }
}

PicSwitch_Sample.ahk

Code: Select all

#Include PicSwitch.ahk

g := Gui(,"PicSwitch")

sDisabled := g.AddPicSwitch("Disabled","Switch Disabled")
sOff := g.AddPicSwitch("yp cred","Switch Off")
sOn := g.AddPicSwitch("yp w100","Switch On",1)
sOn.OnEvent("click",Control_Click)
sOn.SPic.OnEvent("click",(*)=>Control_Click(sOn))

m:=Map()
m["Value0DisabledIcon"]:=m["Value1DisabledIcon"]:="*icon226 imageres.dll"
m["Value1Icon"]:="*icon234 imageres.dll"
m["Value0Icon"]:="*icon208 imageres.dll"

sDisabled2 := g.AddPicSwitch("xm Disabled","Switch Disabled",,m)
sOff2 := g.AddPicSwitch("yp","Switch Off",,m)
sOn2 := g.AddPicSwitch("yp w100","Switch On",1)
sOn2.SOpt:=m
sOn2.RefreshStatusIcon

m:=Map()
m["Value0DisabledIcon"]:=m["Value1DisabledIcon"]:="Icon\SW_Value0DisabledIcon.png"
m["Value1Icon"]:="Icon\SW_Value1Icon.png"
m["Value0Icon"]:="Icon\SW_Value0Icon.png"

sDisabled3 := g.AddPicSwitch("xm Disabled","Switch Disabled",,m)
sOff3 := g.AddPicSwitch("yp","Switch Off",,m)
sOn3 := g.AddPicSwitch("yp w100","Switch On",1,m)

m:=Map()
m["SWidth"]:=20
m["SHeight"]:=20
m["Value0DisabledIcon"]:=m["Value1DisabledIcon"]:="Icon\CB_Value0DisabledIcon.png"
m["Value1Icon"]:="Icon\CB_Value1Icon.png"
m["Value0Icon"]:="Icon\CB_Value0Icon.png"

sDisabled2 := g.AddPicSwitch("xm Disabled","Checkbox Disabled",,m)
sOff2 := g.AddPicSwitch("yp","Unchecked",,m)
sOn2 := g.AddPicSwitch("yp w100","Checked",1,m)


; sOn.Value:=0
; sOff.Visible:=False
; sDisabled.Enabled:=True

g.Show

Control_Click(CtrlObj, *) {
	CtrlObj.Text:="Switch " (CtrlObj.Value?"On":"Off")
}

Icon.zip
(10.18 KiB) Downloaded 56 times

Re: PicSwitch - Switch, Checkbox controls with picture

Posted: 08 Dec 2023, 13:37
by burque505
@tranht17, thanks very much for this. Works great, easy to modify.
Regards,
burque505

Re: PicSwitch - Switch, Checkbox controls with picture

Posted: 10 Dec 2023, 10:25
by Relayer
Hi,

Is the screenshot meant to show the output of running the sample script? When I run the sample script I only get the first two rows and the first item in each row does nothing when clicked on. Forgive my laziness to analyze the script and try to figure it out myself. BTW... very nice addition to my V2 library.

Relayer

Re: PicSwitch - Switch, Checkbox controls with picture

Posted: 10 Dec 2023, 20:22
by tranht17
I only get the first two rows
It's an icon created for testing, so there are a few parts that don't look good and I haven't had time to do it so I didn't post it.
If you like it, I'll post it later.
You can create your own icon set or search for icon sets online.
the first item in each row does nothing when clicked on
Because that control is disabled.
You should read more instructions of AKH v2: https://www.autohotkey.com/docs/v2/lib/Gui.htm#Disabled

Re: PicSwitch - Switch, Checkbox controls with picture

Posted: 12 Dec 2023, 03:27
by tranht17
Updated and added icons

Re: PicSwitch - Switch, Checkbox controls with picture

Posted: 19 Dec 2023, 08:18
by Vfpskin
Its a great script!
Thanks for sharing!


Regards.