11# ColorButton.ahk
2- An extended method for changing a button's background color.
2+ An extended method for changing a button's background, text and border color.
33
44[ English] ( #colorbuttonahk ) | [ 中文] ( #colorbuttonahk-1 ) | [ Srpski] ( #colorbuttonahk-2 )
55
6- ![ 20240530-1454-20 5059500 ] ( https://github.com/nperovic/ColorButton.ahk/assets/122501303/c951d745-160a-4fe1-912d-42d27d97481a )
6+ ![ ColorButton Demo ] ( https://github.com/nperovic/ColorButton.ahk/assets/122501303/9e5c91f9-7982-4b8f-8330-fafa989a0fb8 )
77
88
99## Features
10- - Easily change a button's background color.
10+ - Easily change a button's background, text and border color.
1111- Automatically set the text colour to white or black depends on the background colour.
1212- Compatible with [ AutoHotkey v2.1-alpha.9] ( https://github.com/AutoHotkey/AutoHotkeyDocs/tree/alpha ) or later. ** (Update: v2.0 is now supported too.)**
1313- Learn more about the ahk v2.1-alpha: [ Click here] ( https://github.com/AutoHotkey/AutoHotkeyDocs/tree/alpha )
@@ -29,39 +29,57 @@ An extended method for changing a button's background color.
2929### Parameters
3030``` scala
3131/**
32- * @param {integer} btnBgColor Button's background color. (RGB)
33- * @param {integer} [colorBehindBtn] The color of the button's surrounding area. If omitted, if will be the same as `myGui.BackColor`. **(Usually let it be transparent looks better.)**
34- * @param {integer} [roundedCorner] Specifies the rounded corner preference for the button. If omitted, :
35- * > For Windows 11: Enabled. (value: 9)
36- * > For Windows 10: Disabled.
37- * @param {Integer} [showFocusedBorder=true] Highlight the border of the button when it's focused.
32+ * Configures a button's appearance.
33+ * @param {number} bgColor - Button's background color (RGB).
34+ * @param {number} [txColor] - Button text color (RGB). If omitted, the text colour will be automatically set to white or black depends on the background colour.
35+ * @param {boolean} [showBorder=1]
36+ * - `1` : Highlight when focused.
37+ * - `0` : No border displayed.
38+ * - `-1`: Border always visible.
39+ * @param {number} [borderColor=0xFFFFFF] - Button border color (RGB).
40+ * @param {number} [roundedCorner] - Rounded corner preference for the button. If omitted,
41+ * - For Windows 11: Enabled (value: 9).
42+ * - For Windows 10: Disabled.
3843 */
39- SetBackColor (btnBgColor, colorBehindBtn ? , roundedCorner ? , showFocusedBorder := true )
44+ SetColor (bgColor, txColor ? , showBorder := 1 , borderColor := 0xFFFFFF , roundedCorner ? )
4045```
41- ## ` BtnObj.SetBackColor(btnBgColor, colorBehindBtn ?, roundedCorner?, showFocusedBorder := true ) `
46+ ## ` BtnObj.SetColor(bgColor, txColor ?, showBorder := 1, borderColor := 0xFFFFFF, roundedCorner? ) `
4247``` php
4348#Requires AutoHotkey v2
4449#Include <ColorButton >
4550
46- myGui := Gui()
47- myGui.SetFont("cWhite s20", "Segoe UI")
48- myGui.BackColor := 0x2c2c2c
49-
50- /** @type {_BtnColor} */
51- btn := myGui.AddButton("w300", "Rounded Button")
52- btn.SetBackColor(0xaa2031,, 9)
53-
5451/** @type {_BtnColor} */
55- btn2 := myGui.AddButton("wp", "No Focused Outline")
56- btn2.SetBackColor(0xffd155,, 9, false)
57-
58- /** @type {_BtnColor} */
59- btn3 := myGui.AddButton("wp", "Rectangle Button")
60- btn3.SetBackColor("0x7755ff",, 0)
61-
62- /** @type {_BtnColor} */
63- btn4 := myGui.AddButton("wp", "No Focused Outline")
64- btn4.SetBackColor("0x55ffd4", , 0, 0)
52+ btn := btn2 := btn3 := btn4 := unset
53+
54+ btn := myGui.AddButton("xm w300", "Rounded Button")
55+ btn.SetColor("0xaa2031", "FFFFCC",, "fff5cc", 9)
56+ btn.OnEvent("Click", btnClicked)
57+
58+ btnClicked(btn, *) {
59+ static toggle := 0
60+ static textColor := btn.TextColor
61+ static backColor := btn.BackColor
62+
63+ if (toggle^=1) {
64+ btn.TextColor := backColor
65+ btn.backColor := textColor
66+ } else {
67+ btn.TextColor := TextColor
68+ btn.backColor := backColor
69+ }
70+ }
71+
72+ btn2 := myGui.AddButton("yp wp", "Border Always Visible")
73+ btn2.SetColor(myGui.BackColor, "fff5cc")
74+ btn2.BorderColor := btn2.TextColor
75+ btn2.ShowBorder := -1
76+
77+ btn3 := myGui.AddButton("xm wp", "Rectangle Button")
78+ btn3.SetColor("4e479a", "c6c1f7")
79+ btn3.RoundedCorner := 0
80+
81+ btn4 := myGui.AddButton("yp wp", "No Focused Outline")
82+ btn4.SetBackColor("008080",, 0, 0,, "AFEEEE")
6583
6684myGui.Show("w280 AutoSize")
6785```
@@ -72,10 +90,10 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
7290---
7391
7492# ColorButton.ahk
75- 本程式庫為內建類別 ` Gui.Button ` 擴充了一個更改按鈕背景顏色的方法 。
93+ 本程式庫為內建類別 ` Gui.Button ` 擴充了一個更改按鈕背景背景、文字和邊框顏色的方法 。
7694
7795## 特點
78- - 輕鬆更改按鈕的背景顏色 。
96+ - 輕鬆更改按鈕的背景、文字和邊框顏色 。
7997- 自動根據背景色彩的深淺設定文字色彩 (深色背景+白色文字或淺色背景+黑色文字)。
8098- 支援 AutoHotkey v2.0 或更高版本。
8199- 想知道更多關於 [ ahk v2.1-alpha] ( https://github.com/AutoHotkey/AutoHotkeyDocs/tree/alpha ) 的資訊,請按[ 這裡] ( https://github.com/AutoHotkey/AutoHotkeyDocs/tree/alpha ) 。
@@ -95,39 +113,57 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
95113### 參數
96114``` scala
97115/**
98- * @param {integer} btnBgColor 按鈕的背景顏色。(RGB)
99- * @param {integer} [colorBehindBtn] 按鈕周圍區域的顏色。如果省略,將與 `myGui.BackColor` 相同。**(通常設為透明看起來會更好。)**
100- * @param {integer} [roundedCorner] 指定按鈕的圓角偏好。如果省略,:
101- * > Windows 11: 啟用。(值:9)
102- * > Windows 10: 禁用。
103- * @param {Integer} [showFocusedBorder=true] 當按鈕獲得焦點時突出顯示邊框。
116+ * 設定按鈕的外觀。
117+ * @param {number} bgColor - 按鈕的背景顏色(RGB)。
118+ * @param {number} [txColor] - 按鈕文字顏色(RGB)。如果未指定,文字顏色將根據背景顏色自動設置為白色或黑色。
119+ * @param {boolean} [showBorder=1]
120+ * - `1` : 聚焦時高亮顯示。
121+ * - `0` : 無邊框顯示。
122+ * - `-1`: 邊框始終可見。
123+ * @param {number} [borderColor=0xFFFFFF] - 按鈕邊框顏色(RGB)。
124+ * @param {number} [roundedCorner] - 按鈕的圓角偏好。如果未指定:
125+ * - 對於 Windows 11:啟用(值:9)。
126+ * - 對於 Windows 10:禁用。
104127 */
105- SetBackColor (btnBgColor, colorBehindBtn ? , roundedCorner ? , showFocusedBorder := true )
128+ SetColor (bgColor, txColor ? , showBorder := 1 , borderColor := 0xFFFFFF , roundedCorner ? )
106129```
107- ## ` BtnObj.SetBackColor(btnBgColor, colorBehindBtn ?, roundedCorner?, showFocusedBorder := true ) `
130+ ## ` BtnObj.SetColor(bgColor, txColor ?, showBorder := 1, borderColor := 0xFFFFFF, roundedCorner? ) `
108131``` php
109132#Requires AutoHotkey v2
110133#Include <ColorButton >
111134
112- myGui := Gui()
113- myGui.SetFont("cWhite s20", "Microsoft Yahei UI")
114- myGui.BackColor := 0x2c2c2c
115-
116135/** @type {_BtnColor} */
117- btn := myGui.AddButton("w300", "圓角按鈕")
118- btn.SetBackColor(0xaa2031,, 9)
119-
120- /** @type {_BtnColor} */
121- btn2 := myGui.AddButton("wp", "聚焦時不顯示邊框")
122- btn2.SetBackColor(0xffd155,, 9, false)
123-
124- /** @type {_BtnColor} */
125- btn3 := myGui.AddButton("wp", "正方形按鈕")
126- btn3.SetBackColor("0x7755ff",, 0)
127-
128- /** @type {_BtnColor} */
129- btn4 := myGui.AddButton("wp", "聚焦時不顯示邊框")
130- btn4.SetBackColor("0x55ffd4", , 0, 0)
136+ btn := btn2 := btn3 := btn4 := unset
137+
138+ btn := myGui.AddButton("xm w300", "Rounded Button")
139+ btn.SetColor("0xaa2031", "FFFFCC",, "fff5cc", 9)
140+ btn.OnEvent("Click", btnClicked)
141+
142+ btnClicked(btn, *) {
143+ static toggle := 0
144+ static textColor := btn.TextColor
145+ static backColor := btn.BackColor
146+
147+ if (toggle^=1) {
148+ btn.TextColor := backColor
149+ btn.backColor := textColor
150+ } else {
151+ btn.TextColor := TextColor
152+ btn.backColor := backColor
153+ }
154+ }
155+
156+ btn2 := myGui.AddButton("yp wp", "Border Always Visible")
157+ btn2.SetColor(myGui.BackColor, "fff5cc")
158+ btn2.BorderColor := btn2.TextColor
159+ btn2.ShowBorder := -1
160+
161+ btn3 := myGui.AddButton("xm wp", "Rectangle Button")
162+ btn3.SetColor("4e479a", "c6c1f7")
163+ btn3.RoundedCorner := 0
164+
165+ btn4 := myGui.AddButton("yp wp", "No Focused Outline")
166+ btn4.SetBackColor("008080",, 0, 0,, "AFEEEE")
131167
132168myGui.Show("w280 AutoSize")
133169```
0 commit comments