是一个 自定义复选按钮控件的实现,用于通过 AutoHotkey 提供的图形用户界面 (GUI) 生成 动态分组的自定义单选按钮 (radio buttons)。每个按钮具有自定义的外观样式(颜色、缩放比例、字体等),并支持分组选择。
所需依赖库下载:
; #Include <My Altered Gdip Lib>
#SingleInstance, Force
Gdip_Startup()
Gui, 1:New, +AlwaysOnTop HwndGuiHwnd
Gui, Color, % Background := "333333"
;######################################################################################################################################################################
scale := 1
Y_Offset := 1
X_Offset := 5
Gui, -DPIScale
Admin := New GlowRadio_1( { ButtonRestColor: "0xFF116600" , RestColor: "0xFF339922" , ActiveColor: "0xFF99ef77" , Scalefactor: scale , State: 1 , Y: y := 10 , Text: "Admin" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
User := New GlowRadio_1( { ButtonRestColor: "0xFF116600" , RestColor: "0xFF339922" , ActiveColor: "0xFF99ef77" , Scalefactor: scale , State: 0 , Y: y += 5 + Admin.H , Text: "User" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
Other := New GlowRadio_1( { ButtonRestColor: "0xFF116600" , RestColor: "0xFF339922" , ActiveColor: "0xFF99ef77" , Scalefactor: scale , State: 0 , Y: y += 5 + User.H , Text: "Other" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
;######################################################################################################################################################################
scale := 1.5
Y_Offset := 1
X_Offset := 2
OtherGroup := []
OtherGroup[ 1 ] := New GlowRadio_1( { ButtonRestColor: "0xFF52565a" , RestColor: "0xFF92969a" , ActiveColor: "0xFFd2d6da" , GroupName: "Group2" , Scalefactor: scale , State: 1 , X: 130 , Y: y := 10 , Text: "Admin" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
OtherGroup[ 2 ] := New GlowRadio_1( { ButtonRestColor: "0xFF52565a" , RestColor: "0xFF92969a" , ActiveColor: "0xFFd2d6da" , Scalefactor: scale , State: 0 , X: 130 , Y: y += 5 + Admin.H , Text: "User" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
OtherGroup[ 3 ] := New GlowRadio_1( { ButtonRestColor: "0xFF52565a" , RestColor: "0xFF92969a" , ActiveColor: "0xFFd2d6da" , Scalefactor: scale , State: 0 , X: 130 , Y: y += 5 + User.H , Text: "Other" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
;######################################################################################################################################################################
scale := 2
Y_Offset := 1
X_Offset := 2
AnotherGroup := []
AnotherGroup[ 1 ] := New GlowRadio_1( { ButtonRestColor: "0xFF660000" , RestColor: "0xFF992222" , ActiveColor: "0xFFff4444" , GroupName: "Group3" , Scalefactor: scale , State: 1 , Y: y := 100 , Text: "Admin" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
AnotherGroup[ 2 ] := New GlowRadio_1( { ButtonRestColor: "0xFF660000" , RestColor: "0xFF992222" , ActiveColor: "0xFFff4444" , Scalefactor: scale , State: 0 , Y: y += 5 + Admin.H , Text: "Waldo" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
AnotherGroup[ 3 ] := New GlowRadio_1( { ButtonRestColor: "0xFF660000" , RestColor: "0xFF992222" , ActiveColor: "0xFFff4444" , Scalefactor: scale , State: 0 , Y: y += 5 + User.H , Text: "Other" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
;######################################################################################################################################################################
scale := 3
Y_Offset := 1
X_Offset := 1
OneLastGroup := []
OneLastGroup[ 1 ] := New GlowRadio_1( { GroupName: "Group4" , Scalefactor: scale , State: 0 , X: 130 , Y: y := 100 , Text: "Admin" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
OneLastGroup[ 2 ] := New GlowRadio_1( { Scalefactor: scale , State: 0 , X: 130 , Y: y += 5 + Admin.H , Text: "User" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
OneLastGroup[ 3 ] := New GlowRadio_1( { Scalefactor: scale , State: 1 , X: 130 , Y: y += 5 + User.H , Text: "Other" , BackgroundColor: "0xFF" Background , Y_Offset: Y_Offset , X_Offset: X_Offset } )
Gui, +DPIScale
;######################################################################################################################################################################
Gui, Add, Edit, xm y+30 w300 r1
Gui, Show,,Custom Radio Controls
return
GuiClose:
GuiContextMenu:
*ESC::ExitApp
;*********************************************************************************************************************************
;How to get the output from the radios
RALT::
ToolTip, % "Selected item from Group 1: " Admin.GetSelected()
. "`nThe text on the selected item is: " GlowRadio_1.Groups[ "Group1" ].Members[ Admin.GetSelected() ].Text
. "`n`n`nSelected item from group 2: " OtherGroup[ 1 ].GetSelected()
. "`n`nThe State of ""OneLastGroup[ 2 ]"" is: " OneLastGroup[ 2 ].State " [ " OneLastGroup[ 2 ].GetState() " ] "
return
;*********************************************************************************************************************************
;***********************************************************************************************************************************************************************************************************
;***********************************************************************************************************************************************************************************************************
;***********************************************************************************************************************************************************************************************************
;***********************************************************************************************************************************************************************************************************
;Custom Radio Button Class
class GlowRadio_1 {
;Written By: Hellbent
;Date: Aprial 6th , 2022
;Creates new instances of gui Radio controls and groups
;New Instance Example: MyRadio := New GlowRadio_1( { Scalefactor: 1 , State: 1 , Y: 10 , Text: "Admin" , BackgroundColor: "0xFF22262A" , Y_Offset: 1 , X_Offset: 5 } )
;Get Selected Item From Group Example: MyRadio..GetSelected() ;(Returns the position of the selected radio in it's group )
;Get The State Of A Radio Example: MyRadio.State ;Or: MyRadio.GetState()
static init := GlowRadio_1._SetDefaults()
_SetDefaults(){
This.W := 11
This.H := 11
This.Groups := {}
This.Index := 0
This.Settings := {}
This.Settings.X := 10
This.Settings.Y := 10
This.Settings.W := 120
This.Settings.H := 20
This.Settings.Text := ""
This.Settings.ActiveColor := "0xFF67D5D6"
This.Settings.ButtonRestColor := "0xFF004444"
This.Settings.RestColor := "0xFF009999"
This.Settings.State := 0
This.Settings.Font := "Arial"
This.Settings.FontSize := 14
This.Settings.FontOptions := " vCenter Left "
This.Settings.BackgroundColor := "0xFF22262a"
This.Settings.GroupName := "Group1"
This.Settings.Owner := "1"
This.Settings.GroupIndex := 1
This.Settings.GroupSelected := 0
This.Settings.ScaleFactor := 1
This.Settings.Y_Offset := 5
This.Settings.X_Offset := 1
}
__New( obj := "" ){
This._CreateRadioObject( obj )
This._CreateControl()
if( This.State ){
Loop, % GlowRadio_1.Groups[ This.GroupName ].Members.Length() {
GlowRadio_1.Groups[ This.GroupName ].Members[ A_Index ].GroupSelected := GlowRadio_1.Groups[ This.GroupName ].Selected
GlowRadio_1.Groups[ This.GroupName ].Members[ A_Index ].State := 0
}
This.State := 1
}
Loop, % GlowRadio_1.Groups[ This.GroupName ].Members.Length()
GlowRadio_1.Groups[ This.GroupName ].Members[ A_Index ].Draw()
}
_CreateRadioObject( obj := "" ){
local k , v
for k , v in GlowRadio_1.Settings
This[ k ] := v
if( IsObject( obj ) ){
for k , v in obj
if( GlowRadio_1.Settings.HasKey( k ) )
This[ k ] := v
if( obj.HasKey( "GroupName" ) )
GlowRadio_1.Settings.GroupName := obj.GroupName
}
}
_CreateControl(){
local hwnd , bd
Gui, % This.Owner ":Add", Picture, % "x" This.X * This.ScaleFactor " y" This.Y * This.ScaleFactor " w" This.W * This.ScaleFactor " h" This.H * This.ScaleFactor " hwndhwnd 0xE"
This.Hwnd := hwnd
++GlowRadio_1.Index
If( !isObject( GlowRadio_1.Groups[ This.GroupName ] ) ){
GlowRadio_1.Groups[ This.GroupName ] := {}
This.Index := GlowRadio_1.Groups[ This.GroupName ].Index := 1
GlowRadio_1.Groups[ This.GroupName ].Members := []
( This.State ) ? ( This.GroupSelected := GlowRadio_1.Groups[ This.GroupName ].Selected := This.Index ) : ( This.GroupSelected := GlowRadio_1.Groups[ This.GroupName ].Selected := 0 )
}else{
This.Index := ++GlowRadio_1.Groups[ This.GroupName ].Index
( This.State ) ? ( This.GroupSelected := GlowRadio_1.Groups[ This.GroupName ].Selected := This.Index ) : ( This.GroupSelected := GlowRadio_1.Groups[ This.GroupName ].Selected := GlowRadio_1.Groups[ This.GroupName ].Selected )
}
GlowRadio_1.Groups[ This.GroupName ].Members[ This.Index ] := This
bd := This._SwitchStates.Bind( This )
GuiControl, % This.Owner ":+G" , % Hwnd , % bd
GlowRadio_1.Groups[ This.GroupName ].Selected := This.GroupSelected
GuiControl, % This.Owner ":Focus", % This.Hwnd
GlowRadio_1.Groups[ This.GroupName ].LastSelected := This.GroupSelected
}
_SwitchStates(){
local cc := GlowRadio_1.Groups[ This.GroupName ] , ls := cc.Selected
if( This.Index = GlowRadio_1.Groups[ This.GroupName ].LastSelected )
return
cc.Selected := This.Index
Loop, % cc.Members.Length() {
cc.Members[ A_Index ].GroupSelected := cc.Selected
cc.Members[ A_Index ].State := 0
}
This.State := 1
cc.Members[ GlowRadio_1.Groups[ This.GroupName ].LastSelected ].Draw()
This.Draw()
GlowRadio_1.Groups[ This.GroupName ].LastSelected := This.Index
GuiControl, % This.Owner ":Focus", % This.Hwnd
}
GetState(){
return This.State
}
GetSelected(){
return This.GroupSelected
}
Draw(){
;Bitmap Created Using: HB Bitmap Maker
local ly := 0
ScaleFactor := This.ScaleFactor
pBitmap := Gdip_CreateBitmap( This.W * ScaleFactor , This.H * ScaleFactor ) , G := Gdip_GraphicsFromImage( pBitmap ) , Gdip_SetSmoothingMode( G , 2 )
Brush := Gdip_BrushCreateSolid( This.BackgroundColor ) , Gdip_FillRectangle( G , Brush , -10 * ScaleFactor , -10 * ScaleFactor , 140 * ScaleFactor , 210 * ScaleFactor ) , Gdip_DeleteBrush( Brush )
;Outer ring
Pen := Gdip_CreatePen( ( This.State ) ? ( This.ActiveColor ) : ( This.ButtonRestColor ) , 1 ) , Gdip_DrawRectangle( G , Pen , 2 * This.ScaleFactor , ( ( This.H / 2 ) - ( GlowRadio_1.H / 2 ) + 1 ) * This.ScaleFactor , GlowRadio_1.W * This.ScaleFactor , GlowRadio_1.H * This.ScaleFactor ) , Gdip_DeletePen( Pen )
;inner square
Brush := Gdip_BrushCreateSolid( ( This.ButtonRestColor ) ) , Gdip_FillRectangle( G , Brush , 4 * ScaleFactor , ( ( This.H / 2 ) - ( GlowRadio_1.H / 2 ) + 3 ) * This.ScaleFactor , 7 * ScaleFactor , 7 * ScaleFactor ) , Gdip_DeleteBrush( Brush )
Brush := Gdip_BrushCreateSolid( ( This.State ) ? ( This.ActiveColor ) : ( This.ButtonRestColor ) ) , Gdip_FillRectangle( G , Brush , 5 * ScaleFactor , ( ( This.H / 2 ) - ( GlowRadio_1.H / 2 ) + 4 ) * This.ScaleFactor , 5 * ScaleFactor , 5 * ScaleFactor ) , Gdip_DeleteBrush( Brush )
;bottom layer of text
if( This.State )
Brush := Gdip_BrushCreateSolid( "0xFF000000" ) , Gdip_TextToGraphics( G , This.Text , "s" This.FontSize * ScaleFactor " " This.FontOptions " c" Brush " x" floor( ( GlowRadio_1.W + 5 + 1 + This.X_Offset ) * ScaleFactor ) " y" ly := Floor( ( ( 2 + This.Y_Offset ) * ScaleFactor ) ) , This.Font , Floor( This.W * ScaleFactor ) , Floor( This.H * ScaleFactor ) ) , Gdip_DeleteBrush( Brush )
else
Brush := Gdip_BrushCreateSolid( "0x000000" ) , Gdip_TextToGraphics( G , This.Text , "s" This.FontSize * ScaleFactor " " This.FontOptions " c" Brush " x" Floor( ( GlowRadio_1.W + 5 + 2 + This.X_Offset ) * ScaleFactor ) " y" ly := Floor( ( ( 2 + This.Y_Offset ) * ScaleFactor ) ) , This.Font , Floor( This.W * ScaleFactor ) , floor( This.H * ScaleFactor ) ) , Gdip_DeleteBrush( Brush )
;top layer of text
Brush := Gdip_BrushCreateSolid( ( This.State ) ? ( This.ActiveColor ) : ( This.RestColor ) ) , Gdip_TextToGraphics( G , This.Text , "s" This.FontSize * ScaleFactor " " This.FontOptions " c" Brush " x" floor( ( GlowRadio_1.W + 5 + This.X_Offset ) * ScaleFactor ) " y" Floor( ( ly - 1 + This.Y_Offset ) ) , This.Font , Floor( ( This.W - GlowRadio_1.W ) * ScaleFactor ) , Floor( This.H * ScaleFactor ) ) , Gdip_DeleteBrush( Brush )
Gdip_DeleteGraphics( G )
hBitmap := Gdip_CreateHBITMAPFromBitmap( pBitmap )
SetImage( This.Hwnd , hBitmap )
Gdip_DisposeImage( pBitmap )
DeleteObject( hBitmap )
}
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。

评论(0)