CreateImageButton() - 2024-01-01

Post your working scripts, libraries and tools.
just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: 2.0-beta.1: CreateImageButton() - 2021-08-03 alpha.1

Post by just me » 08 Jan 2022, 06:33

@jNizM: I have a GDI version of ImageButton for simple flat buttons. Interestingly, good old GDI DrawText() is able to draw the text correctly. But I could not force GDI+ GdipDrawString() to do it.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: 2.0-beta.1: CreateImageButton() - 2021-08-03 alpha.1

Post by jNizM » 10 Jan 2022, 03:18

Interesting. Thank you for your test. So with "simple flat buttons" it is not possible for different colors like hover and pressed?
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: 2.0-beta.1: CreateImageButton() - 2021-08-03 alpha.1

Post by just me » 11 Jan 2022, 06:11

@jNizM: Yes, but it's v1.1 and most probably not 'foolproof':

Code: Select all

#NoEnv
SetBatchLines, -1

Gui, Font, s14, Segoe MDL2 Assets
Gui, Add, Button, w200 hwndHBTN, % " " Chr(0xE80F) . "   Home"
FCB_Create(HBTN, ["White", "Red", "Red"], ["Gray", "White", "White"])
Gui, Show, , Test
Return

GuiEscape:
GuiClose:
ExitApp
; ================================================================================================================================
; Function:       Creates flat colored push buttons with up to 6 colors for the different button states.
; Syntax:         FCB_Create(Hwnd, BkgColors[, TxtColors])
; Parameters:     
;     Hwnd        -  The handle (HWND) to the button.
;     BkgColors   -  An array of up to 6 color values used to draw the background of the button.
;                    The first element is mandatory. Its value will be used for all other omitted or empty elements.
;     TxtColors   -  An array of up to 6 color values used to draw the text of the button.
;                    If the first element is omitted it will be set to the default text color.
;                    The value of the first element will be used for all other omitted or empty elements.
;                    Default: "" -> use the default text color for all states.
; Return values:
;     Returns True on success; otherwise False.
; Remarks:
;     All color values can be passed either as AHK HTML color name (e.g. "Navy") or RGB integer value (e.g. 0xRRGGBB).
;
;     MSDN defines 6 states (msdn.microsoft.com/en-us/library/windows/desktop/bb775975(v=vs.85).aspx):
;        PBS_NORMAL    = 1
;	      PBS_HOT       = 2
;	      PBS_PRESSED   = 3
;	      PBS_DISABLED  = 4
;	      PBS_DEFAULTED = 5
;	      PBS_STYLUSHOT = 6
;     On Win Vista and 7 the colors specified for states 5 and 6 are used to animate the current default button.
; ================================================================================================================================
FCB_Create(Hwnd, BkgColors, TxtColors := "") {
   ; System colors
   Static DefTxtColor := DllCall("GetSysColor", "Int", 18, "UInt") ; default text color
        , DisBkgColor := ""                                        ; disabled background color
        , DisTxtColor := DllCall("GetSysColor", "Int", 17, "UInt") ; disabled text color
   ; Stock objects
   Static DCB := DllCall("GetStockObject", "Int", 18, "UPtr") ; DC_BRUSH
        , DCP := DllCall("GetStockObject", "Int", 19, "UPtr") ; DC_PEN
   ; Messages
   Static WM_GETFONT := 0x31
   ; Styles
   Static BS_DEFPUSHBUTTON := 0x01, BS_LEFT := 0x0100, BS_RIGHT := 0x0200, BS_CENTER := 0x0300, BS_TOP := 0x0400
        , BS_BOTTOM := 0x0800, BS_VCENTER := 0x0C00, BS_MULTILINE := 0x2000, WS_DISABLED := 0x08000000
        , BS_CHECKBOX := 0x02, BS_RADIOBUTTON := 0x04, BS_GROUPBOX := 0x07, BS_AUTORADIOBUTTON := 0x09
        , RCBUTTONS := BS_CHECKBOX | BS_RADIOBUTTON | BS_AUTORADIOBUTTON
   ; DrawText format flags
   Static DT_LEFT := 0x00, DT_TOP := 0x00, DT_CENTER := 0x01, DT_RIGHT := 0x02, DT_VCENTER := 0x04
        , DT_BOTTOM := 0x08, DT_WORDBREAK := 0x10, DT_SINGLELINE := 0x20, DT_NOCLIP := 0x0100, DT_CALCRECT := 0x0400
        , DT_END_ELLIPSIS := 0x8000, DT_PATH_ELLIPSIS := 0x4000
   ; Image list flags
   Static ILC_COLOR32 := 0x20
   ; =============================================================================================================================
   ; Get the backgound color for disabled buttons on first call
   If (DisBkgColor = "") {
      LGB := DllCall("GetStockObject", "Int",  1, "UPtr") ; LTGRAY_BRUSH
      VarSetCapacity(LB, 16, 0) ; LOGBRUSH
      DllCall("GetObject", "Ptr", LGB, "Int", A_PtrSize = 8 ? 16 : 12, "Ptr", &LB)
      DisBkgColor := NumGet(LB, 4, "UInt")
      DllCall("DeleteObject", "Ptr", LGB)
   }
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Check the control parameter
   If !DllCall("IsWindow", "Ptr", Hwnd, "UPtr")
      Return False
   ; Get and check control's class and styles
   WinGetClass, BtnClass, ahk_id %Hwnd%
   ControlGet, BtnStyle, Style, , , ahk_id %Hwnd%
   If (BtnClass != "Button") || ((BtnStyle & 0xF ^ BS_GROUPBOX) = 0) || ((BtnStyle & RCBUTTONS) > 1)
      Return False
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Check and convert the color parameters
   ; Background colors
   If !(IsObject(BkgColors)) || (BkgColors[1] = "")
      Return False
   If !(BkgArray := FCB_GetColors(BkgColors, 0))
      Return False
   ; Text colors
   If !(IsObject(TxtColors)) || (TxtColors.MaxIndex() = "")
      TxtArray := [DefTxtColor, DefTxtColor, DefTxtColor, DefTxtColor, DefTxtColor, DefTxtColor]
   Else If !(TxtArray := FCB_GetColors(TxtColors, DefTxtColor))
      Return False
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Get the button's properties.
   HFN := DllCall("SendMessage", "Ptr", Hwnd, "UInt", 0x31, "Ptr", 0, "Ptr", 0, "UPtr") ; WM_GETFONT
   ControlGetText, BtnCaption, , ahk_id %Hwnd%
   VarSetCapacity(CtlRect, 16, 0)
   DllCall("GetClientRect", "Ptr", Hwnd, "Ptr", &CtlRect)
   CtlW := NumGet(CtlRect, 8, "Int")
   CtlH := NumGet(CtlRect, 12, "Int")
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Create a compatible memory DC and a compatible bitmap for drawing
   HDC := DllCall("GetDC", "Ptr", Hwnd, "UPtr")
   MDC := DllCall("CreateCompatibleDC", "Ptr", HDC, "UPtr")
   HBM := DllCall("CreateCompatibleBitmap", "Ptr", HDC, "Int", CtlW, "Int", CtlH, "UPtr")
   HBM := DllCall("CopyImage", "Ptr", HBM, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x2008, "UPtr")
   DllCall("SelectObject", "Ptr", MDC, "Ptr", DCB)
   DllCall("SelectObject", "Ptr", MDC, "Ptr", DCP)
   DllCall("SelectObject", "Ptr", MDC, "Ptr", HFN)
   DllCall("ReleaseDC", "Ptr", Hwnd, "Ptr", HDC)
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Calculate the text rectangle
   CenterBtn := BtnStyle & BS_CENTER
   DT_ALIGN := CenterBtn = BS_CENTER ? DT_CENTER
             : CenterBtn = BS_RIGHT  ? DT_RIGHT
             : CenterBtn = BS_LEFT   ? DT_LEFT
                                     : DT_CENTER
   DT_ALIGN |= (BtnStyle & BS_MULTILINE) ? DT_WORDBREAK : DT_END_ELLIPSIS
   VC := BtnStyle & BS_VCENTER
   If (VC = BS_VCENTER) || (VC = BS_BOTTOM) || (VC = 0) {
      VarSetCapacity(TxtRect, 16, 0)
      NumPut(NumGet(CtlRect, 0, "Int64"), TxtRect, 0, "Int64")
      NumPut(NumGet(CtlRect, 8, "Int64"), TxtRect, 8, "Int64")
      TxtT := NumGet(TxtRect, 4, "Int")
      TxtB := NumGet(TxtRect, 12, "Int")
      DllCall("DrawText", "Ptr", MDC, "Str", BtnCaption, "Int", -1, "Ptr", &TxtRect, "UInt", DT_ALIGN | DT_CALCRECT)
      D := TxtB - NumGet(TxtRect, 12, "Int")
      TxtT += (VC = BS_BOTTOM) ? D : D // 2
      NumPut(TxtT, CtlRect, 4, "Int")
      NumPut(TxtB, CtlRect, 12, "Int")
   }
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Create the bitmaps for each button state and add them to an image list
   HIL := DllCall("ImageList_Create", "UInt", CtlW, "UInt", CtlH, "UInt", ILC_COLOR32, "Int", 6, "Int", 0, "UPtr")
   Loop, 6 {
      OBM := DllCall("SelectObject", "Ptr", MDC, "Ptr", HBM)
      BtnDisabled := BtnStyle & WS_DISABLED
      BkgColor := BtnDisabled ? DisBkgColor : BkgArray[A_Index]
      TxtColor := BtnDisabled ? DisTxtColor : TxtArray[A_Index]
      DllCall("SetDCBrushColor", "Ptr", MDC, "UInt", BkgColor)
      DllCall("SetDCPenColor", "Ptr", MDC, "UInt", BkgColor)
      DllCall("Rectangle", "Ptr", MDC, "Int", 0, "Int", 0, "Int", CtlW, "Int", CtlH)
      DllCall("SetBkMode", "Ptr", MDC, "Int", 1)
      DllCall("SetTextColor", "Ptr", MDC, "UInt", TxtColor)
      DllCall("DrawText", "Ptr", MDC, "Str", BtnCaption, "Int", -1, "Ptr", &CtlRect, "UInt", DT_ALIGN)
      DllCall("SelectObject", "Ptr", MDC, "Ptr", OBM)
      DllCall("ImageList_Add", "Ptr", HIL, "Ptr", HBM, "Ptr", 0)
      If (BtnDisabled)
         Break
   }
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Create a BUTTON_IMAGELIST structure
   VarSetCapacity(BIL, 20 + A_PtrSize, 0)
   NumPut(HIL, BIL, 0, "Ptr")
   Numput(4, BIL, A_PtrSize + 16, "UInt") ; BUTTON_IMAGELIST_ALIGN_CENTER
   ; Assign the ImageList to the button
   Control, Style, +0x0080, , ahk_id %Hwnd% ; BS_BITMAP
   DllCall("SendMessage", "Ptr", Hwnd, "UInt", 0x1602, "Ptr", 0, "Ptr", 0) ; BCM_SETIMAGELIST
   DllCall("SendMessage", "Ptr", Hwnd, "UInt", 0x1602, "Ptr", 0, "Ptr", &BIL)
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; Free the GDI objects
   DllCall("DeleteDC", "Ptr", MDC)
   DllCall("DeleteObject", "Ptr", HBM)
   ; -----------------------------------------------------------------------------------------------------------------------------
   ; All done
   Return True
}
; ================================================================================================================================
; Internally called function to convert the colors to BGR
; ================================================================================================================================
FCB_GetColors(ColorArray, DefColor) {
   ; HTML colors (BGR)
   Static HTML := {BLACK:   0x000000, GRAY: 0x808080, SILVER: 0xC0C0C0, WHITE: 0xFFFFFF, MAROON: 0x000080, PURPLE: 0x800080
                 , FUCHSIA: 0xFF00FF, RED:  0x0000FF, GREEN:  0x008000, OLIVE: 0x008080, YELLOW: 0x00FFFF, LIME:   0x00FF00
                 , NAVY:    0x800000, TEAL: 0x808000, AQUA:   0xFFFF00, BLUE:  0xFF0000}
   Colors := []
   Loop, 6 {
      CurrColor := ColorArray[A_Index]
      If (CurrColor = "")
         CurrColor := A_Index = 1 ? DefColor : Colors[1]
      Else If (HTML.HasKey(CurrColor))
         CurrColor := HTML[CurrColor]
      Else
         CurrColor := ((CurrColor & 0xFF0000) >> 16) | (CurrColor & 0x00FF00) | ((CurrColor & 0x0000FF) << 16)
      If (CurrColor = "")
         Return False
      Colors[A_Index] := CurrColor
   }
   Return Colors
}

just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CreateImageButton() - 2023-02-03

Post by just me » 03 Feb 2023, 07:51

Update on 2023-02-03: v1.0.00

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: CreateImageButton() - 2023-02-03

Post by Smile_ » 18 Mar 2023, 12:28

2023-03-18_182650.png
2023-03-18_182650.png (34.56 KiB) Viewed 3115 times

While trying to run @jNizM example.

just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CreateImageButton() - 2023-02-03

Post by just me » 19 Mar 2023, 03:19

@Smile_: For the new version you have to pass a reference to a Gui.Button object, not only its HWND.

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: CreateImageButton() - 2023-02-03

Post by Smile_ » 19 Mar 2023, 06:30

Perfect thanks :)

mfedorov
Posts: 5
Joined: 10 Jul 2023, 00:32

Re: CreateImageButton() - 2023-02-03

Post by mfedorov » 10 Jul 2023, 00:43

mfedorov wrote:
10 Jul 2023, 00:35
amm, run example - saw 2 empty msgbox with text Button1 and button 2 and some error: Throw. How to run? Ty.
Ok i get it work like this: CreateImageButton(Btn11, 0, IBStyles["info"]*)

mfedorov
Posts: 5
Joined: 10 Jul 2023, 00:32

Re: CreateImageButton() - 2023-02-03

Post by mfedorov » 10 Jul 2023, 01:06

Could you please show an example how to set an image on a button?

just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CreateImageButton() - 2023-07-10

Post by just me » 10 Jul 2023, 04:04

The sample script already shows an example for Btn5. I've updated it today. It's now compatible with the current version of CreateImageButton().

The image files must be in A_ScriptDir respectively A_WorkingDir if no path is specified. Alternatively you can pass a handle to a bitmap (HBITMAP) instead of the name.

mfedorov
Posts: 5
Joined: 10 Jul 2023, 00:32

Re: CreateImageButton() - 2023-07-10

Post by mfedorov » 10 Jul 2023, 12:49

just me wrote:
10 Jul 2023, 04:04
The sample script already shows an example for Btn5. I've updated it today. It's now compatible with the current version of CreateImageButton().

The image files must be in A_ScriptDir respectively A_WorkingDir if no path is specified. Alternatively you can pass a handle to a bitmap (HBITMAP) instead of the name.
Thanks a lot! Amazing work!

nt-_-ts
Posts: 12
Joined: 27 Apr 2022, 08:02

Re: CreateImageButton() - 2023-07-10

Post by nt-_-ts » 07 Aug 2023, 06:50

Amazing work.
Attachments
Screenshot_1.png
Screenshot_1.png (78.54 KiB) Viewed 2640 times

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: 2.0-beta.1: CreateImageButton() - 2021-08-03 alpha.1

Post by sashaatx » 18 Nov 2023, 06:58

jNizM wrote:
03 Aug 2021, 06:47
Bootstrap Buttons
Image

Code: Select all

; ===============================================================================================================================

IBStyles := Map()

IBStyles["info"]                   := [[0x80C6E9F4,,, 0, 0x8046B8DA, 1], [0x8086D0E7,,, 0, 0x8046B8DA, 1], [0x8046B8DA,,, 0, 0x8046B8DA, 1], [0xFFF0F0F0,,, 0, 0x8046B8DA, 1]]
IBStyles["success"]                := [[0x80C6E6C6,,, 0, 0x805CB85C, 1], [0x8091CF91,,, 0, 0x805CB85C, 1], [0x805CB85C,,, 0, 0x805CB85C, 1], [0xFFF0F0F0,,, 0, 0x805CB85C, 1]]
IBStyles["warning"]                := [[0x80FCEFDC,,, 0, 0x80F0AD4E, 1], [0x80F6CE95,,, 0, 0x80F0AD4E, 1], [0x80F0AD4E,,, 0, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 0, 0x80F0AD4E, 1]]
IBStyles["critical"]               := [[0x80F0B9B8,,, 0, 0x80D43F3A, 1], [0x80E27C79,,, 0, 0x80D43F3A, 1], [0x80D43F3A,,, 0, 0x80D43F3A, 1], [0xFFF0F0F0,,, 0, 0x80D43F3A, 1]]

IBStyles["info-outline"]           := [[0xFFF0F0F0,,, 0, 0x8046B8DA, 1], [0x80C6E9F4,,, 0, 0x8046B8DA, 1], [0x8086D0E7,,, 0, 0x8046B8DA, 1], [0xFFF0F0F0,,, 0, 0x8046B8DA, 1]]
IBStyles["success-outline"]        := [[0xFFF0F0F0,,, 0, 0x805CB85C, 1], [0x80C6E6C6,,, 0, 0x805CB85C, 1], [0x8091CF91,,, 0, 0x805CB85C, 1], [0xFFF0F0F0,,, 0, 0x805CB85C, 1]]
IBStyles["warning-outline"]        := [[0xFFF0F0F0,,, 0, 0x80F0AD4E, 1], [0x80FCEFDC,,, 0, 0x80F0AD4E, 1], [0x80F6CE95,,, 0, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 0, 0x80F0AD4E, 1]]
IBStyles["critical-outline"]       := [[0xFFF0F0F0,,, 0, 0x80D43F3A, 1], [0x80F0B9B8,,, 0, 0x80D43F3A, 1], [0x80E27C79,,, 0, 0x80D43F3A, 1], [0xFFF0F0F0,,, 0, 0x80D43F3A, 1]]

IBStyles["info-round"]             := [[0x80C6E9F4,,, 8, 0x8046B8DA, 1], [0x8086D0E7,,, 8, 0x8046B8DA, 1], [0x8046B8DA,,, 8, 0x8046B8DA, 1], [0xFFF0F0F0,,, 8, 0x8046B8DA, 1]]
IBStyles["success-round"]          := [[0x80C6E6C6,,, 8, 0x805CB85C, 1], [0x8091CF91,,, 8, 0x805CB85C, 1], [0x805CB85C,,, 8, 0x805CB85C, 1], [0xFFF0F0F0,,, 8, 0x805CB85C, 1]]
IBStyles["warning-round"]          := [[0x80FCEFDC,,, 8, 0x80F0AD4E, 1], [0x80F6CE95,,, 8, 0x80F0AD4E, 1], [0x80F0AD4E,,, 8, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 8, 0x80F0AD4E, 1]]
IBStyles["critical-round"]         := [[0x80F0B9B8,,, 8, 0x80D43F3A, 1], [0x80E27C79,,, 8, 0x80D43F3A, 1], [0x80D43F3A,,, 8, 0x80D43F3A, 1], [0xFFF0F0F0,,, 8, 0x80D43F3A, 1]]

IBStyles["info-outline-round"]     := [[0xFFF0F0F0,,, 8, 0x8046B8DA, 1], [0x80C6E9F4,,, 8, 0x8046B8DA, 1], [0x8086D0E7,,, 8, 0x8046B8DA, 1], [0xFFF0F0F0,,, 8, 0x8046B8DA, 1]]
IBStyles["success-outline-round"]  := [[0xFFF0F0F0,,, 8, 0x805CB85C, 1], [0x80C6E6C6,,, 8, 0x805CB85C, 1], [0x8091CF91,,, 8, 0x805CB85C, 1], [0xFFF0F0F0,,, 8, 0x805CB85C, 1]]
IBStyles["warning-outline-round"]  := [[0xFFF0F0F0,,, 8, 0x80F0AD4E, 1], [0x80FCEFDC,,, 8, 0x80F0AD4E, 1], [0x80F6CE95,,, 8, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 8, 0x80F0AD4E, 1]]
IBStyles["critical-outline-round"] := [[0xFFF0F0F0,,, 8, 0x80D43F3A, 1], [0x80F0B9B8,,, 8, 0x80D43F3A, 1], [0x80E27C79,,, 8, 0x80D43F3A, 1], [0xFFF0F0F0,,, 8, 0x80D43F3A, 1]]

; ===============================================================================================================================

UseGDIP()

MyGui := Gui(, "Bootstrap Buttons")
MyGui.MarginX := 20
MyGui.MarginY := 20
MyGui.SetFont("s11", "Segoe UI")
CreateImageButton("SetDefGuiColor", 0xFFF0F0F0)

; -----------------------------------------------------------------------------

Btn11 := MyGui.AddButton("xm ym w80 h24", "Info")
CreateImageButton(Btn11.Hwnd, 0, IBStyles["info"]*)

Btn12 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn12.Hwnd, 0, IBStyles["success"]*)

Btn13 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn13.Hwnd, 0, IBStyles["warning"]*)

Btn14 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn14.Hwnd, 0, IBStyles["critical"]*)

; -----------------------------------------------------------------------------

Btn21 := MyGui.AddButton("x+20 yp w80 h24", "Info")
CreateImageButton(Btn21.Hwnd, 0, IBStyles["info-outline"]*)

Btn22 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn22.Hwnd, 0, IBStyles["success-outline"]*)

Btn23 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn23.Hwnd, 0, IBStyles["warning-outline"]*)

Btn24 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn24.Hwnd, 0, IBStyles["critical-outline"]*)

; -----------------------------------------------------------------------------

Btn31 := MyGui.AddButton("xm y+20 w80 h24", "Info")
CreateImageButton(Btn31.Hwnd, 0, IBStyles["info-round"]*)

Btn32 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn32.Hwnd, 0, IBStyles["success-round"]*)

Btn33 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn33.Hwnd, 0, IBStyles["warning-round"]*)

Btn34 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn34.Hwnd, 0, IBStyles["critical-round"]*)

; -----------------------------------------------------------------------------

Btn41 := MyGui.AddButton("x+20 yp w80 h24", "Info")
CreateImageButton(Btn41.Hwnd, 0, IBStyles["info-outline-round"]*)

Btn42 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn42.Hwnd, 0, IBStyles["success-outline-round"]*)

Btn43 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn43.Hwnd, 0, IBStyles["warning-outline-round"]*)

Btn44 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn44.Hwnd, 0, IBStyles["critical-outline-round"]*)

; -----------------------------------------------------------------------------

Btn51 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn51.Hwnd, 0, IBStyles["info"]*)

Btn52 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn52.Hwnd, 0, IBStyles["success"]*)

Btn53 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn53.Hwnd, 0, IBStyles["warning"]*)

Btn54 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn54.Hwnd, 0, IBStyles["critical"]*)

; -----------------------------------------------------------------------------

Btn61 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn61.Hwnd, 0, IBStyles["info-outline"]*)

Btn62 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn62.Hwnd, 0, IBStyles["success-outline"]*)

Btn63 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn63.Hwnd, 0, IBStyles["warning-outline"]*)

Btn64 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn64.Hwnd, 0, IBStyles["critical-outline"]*)

; -----------------------------------------------------------------------------

Btn71 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn71.Hwnd, 0, IBStyles["info-round"]*)

Btn72 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn72.Hwnd, 0, IBStyles["success-round"]*)

Btn73 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn73.Hwnd, 0, IBStyles["warning-round"]*)

Btn74 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn74.Hwnd, 0, IBStyles["critical-round"]*)

; -----------------------------------------------------------------------------

Btn81 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn81.Hwnd, 0, IBStyles["info-outline-round"]*)

Btn82 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn82.Hwnd, 0, IBStyles["success-outline-round"]*)

Btn83 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn83.Hwnd, 0, IBStyles["warning-outline-round"]*)

Btn84 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn84.Hwnd, 0, IBStyles["critical-outline-round"]*)

; -----------------------------------------------------------------------------

MyGui.Show()

; ===============================================================================================================================

#Include CreateImageButton.ahk
#Include UseGDIP.ahk
small tweak to fix error, remove .hwnd from function call. might have already been reposted with fix but didnt see it.

Code: Select all

; ===============================================================================================================================

IBStyles := Map()

IBStyles["info"]                   := [[0x80C6E9F4,,, 0, 0x8046B8DA, 1], [0x8086D0E7,,, 0, 0x8046B8DA, 1], [0x8046B8DA,,, 0, 0x8046B8DA, 1], [0xFFF0F0F0,,, 0, 0x8046B8DA, 1]]
IBStyles["success"]                := [[0x80C6E6C6,,, 0, 0x805CB85C, 1], [0x8091CF91,,, 0, 0x805CB85C, 1], [0x805CB85C,,, 0, 0x805CB85C, 1], [0xFFF0F0F0,,, 0, 0x805CB85C, 1]]
IBStyles["warning"]                := [[0x80FCEFDC,,, 0, 0x80F0AD4E, 1], [0x80F6CE95,,, 0, 0x80F0AD4E, 1], [0x80F0AD4E,,, 0, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 0, 0x80F0AD4E, 1]]
IBStyles["critical"]               := [[0x80F0B9B8,,, 0, 0x80D43F3A, 1], [0x80E27C79,,, 0, 0x80D43F3A, 1], [0x80D43F3A,,, 0, 0x80D43F3A, 1], [0xFFF0F0F0,,, 0, 0x80D43F3A, 1]]

IBStyles["info-outline"]           := [[0xFFF0F0F0,,, 0, 0x8046B8DA, 1], [0x80C6E9F4,,, 0, 0x8046B8DA, 1], [0x8086D0E7,,, 0, 0x8046B8DA, 1], [0xFFF0F0F0,,, 0, 0x8046B8DA, 1]]
IBStyles["success-outline"]        := [[0xFFF0F0F0,,, 0, 0x805CB85C, 1], [0x80C6E6C6,,, 0, 0x805CB85C, 1], [0x8091CF91,,, 0, 0x805CB85C, 1], [0xFFF0F0F0,,, 0, 0x805CB85C, 1]]
IBStyles["warning-outline"]        := [[0xFFF0F0F0,,, 0, 0x80F0AD4E, 1], [0x80FCEFDC,,, 0, 0x80F0AD4E, 1], [0x80F6CE95,,, 0, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 0, 0x80F0AD4E, 1]]
IBStyles["critical-outline"]       := [[0xFFF0F0F0,,, 0, 0x80D43F3A, 1], [0x80F0B9B8,,, 0, 0x80D43F3A, 1], [0x80E27C79,,, 0, 0x80D43F3A, 1], [0xFFF0F0F0,,, 0, 0x80D43F3A, 1]]

IBStyles["info-round"]             := [[0x80C6E9F4,,, 8, 0x8046B8DA, 1], [0x8086D0E7,,, 8, 0x8046B8DA, 1], [0x8046B8DA,,, 8, 0x8046B8DA, 1], [0xFFF0F0F0,,, 8, 0x8046B8DA, 1]]
IBStyles["success-round"]          := [[0x80C6E6C6,,, 8, 0x805CB85C, 1], [0x8091CF91,,, 8, 0x805CB85C, 1], [0x805CB85C,,, 8, 0x805CB85C, 1], [0xFFF0F0F0,,, 8, 0x805CB85C, 1]]
IBStyles["warning-round"]          := [[0x80FCEFDC,,, 8, 0x80F0AD4E, 1], [0x80F6CE95,,, 8, 0x80F0AD4E, 1], [0x80F0AD4E,,, 8, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 8, 0x80F0AD4E, 1]]
IBStyles["critical-round"]         := [[0x80F0B9B8,,, 8, 0x80D43F3A, 1], [0x80E27C79,,, 8, 0x80D43F3A, 1], [0x80D43F3A,,, 8, 0x80D43F3A, 1], [0xFFF0F0F0,,, 8, 0x80D43F3A, 1]]

IBStyles["info-outline-round"]     := [[0xFFF0F0F0,,, 8, 0x8046B8DA, 1], [0x80C6E9F4,,, 8, 0x8046B8DA, 1], [0x8086D0E7,,, 8, 0x8046B8DA, 1], [0xFFF0F0F0,,, 8, 0x8046B8DA, 1]]
IBStyles["success-outline-round"]  := [[0xFFF0F0F0,,, 8, 0x805CB85C, 1], [0x80C6E6C6,,, 8, 0x805CB85C, 1], [0x8091CF91,,, 8, 0x805CB85C, 1], [0xFFF0F0F0,,, 8, 0x805CB85C, 1]]
IBStyles["warning-outline-round"]  := [[0xFFF0F0F0,,, 8, 0x80F0AD4E, 1], [0x80FCEFDC,,, 8, 0x80F0AD4E, 1], [0x80F6CE95,,, 8, 0x80F0AD4E, 1], [0xFFF0F0F0,,, 8, 0x80F0AD4E, 1]]
IBStyles["critical-outline-round"] := [[0xFFF0F0F0,,, 8, 0x80D43F3A, 1], [0x80F0B9B8,,, 8, 0x80D43F3A, 1], [0x80E27C79,,, 8, 0x80D43F3A, 1], [0xFFF0F0F0,,, 8, 0x80D43F3A, 1]]

; ===============================================================================================================================

UseGDIP()

MyGui := Gui(, "Bootstrap Buttons")
MyGui.MarginX := 20
MyGui.MarginY := 20
MyGui.SetFont("s11", "Segoe UI")
CreateImageButton("SetDefGuiColor", 0xFFF0F0F0)

; -----------------------------------------------------------------------------

Btn11 := MyGui.AddButton("xm ym w80 h24", "Info")
CreateImageButton(Btn11, 0, IBStyles["info"]*)

Btn12 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn12, 0, IBStyles["success"]*)

Btn13 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn13, 0, IBStyles["warning"]*)

Btn14 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn14, 0, IBStyles["critical"]*)

; -----------------------------------------------------------------------------

Btn21 := MyGui.AddButton("x+20 yp w80 h24", "Info")
CreateImageButton(Btn21, 0, IBStyles["info-outline"]*)

Btn22 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn22, 0, IBStyles["success-outline"]*)

Btn23 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn23, 0, IBStyles["warning-outline"]*)

Btn24 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn24, 0, IBStyles["critical-outline"]*)

; -----------------------------------------------------------------------------

Btn31 := MyGui.AddButton("xm y+20 w80 h24", "Info")
CreateImageButton(Btn31, 0, IBStyles["info-round"]*)

Btn32 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn32, 0, IBStyles["success-round"]*)

Btn33 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn33, 0, IBStyles["warning-round"]*)

Btn34 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn34, 0, IBStyles["critical-round"]*)

; -----------------------------------------------------------------------------

Btn41 := MyGui.AddButton("x+20 yp w80 h24", "Info")
CreateImageButton(Btn41, 0, IBStyles["info-outline-round"]*)

Btn42 := MyGui.AddButton("x+20 yp w80 h24", "Success")
CreateImageButton(Btn42, 0, IBStyles["success-outline-round"]*)

Btn43 := MyGui.AddButton("x+20 yp w80 h24", "Warning")
CreateImageButton(Btn43, 0, IBStyles["warning-outline-round"]*)

Btn44 := MyGui.AddButton("x+20 yp w80 h24", "Critical")
CreateImageButton(Btn44, 0, IBStyles["critical-outline-round"]*)

; -----------------------------------------------------------------------------

Btn51 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn51, 0, IBStyles["info"]*)

Btn52 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn52, 0, IBStyles["success"]*)

Btn53 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn53, 0, IBStyles["warning"]*)

Btn54 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn54, 0, IBStyles["critical"]*)

; -----------------------------------------------------------------------------

Btn61 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn61, 0, IBStyles["info-outline"]*)

Btn62 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn62, 0, IBStyles["success-outline"]*)

Btn63 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn63, 0, IBStyles["warning-outline"]*)

Btn64 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn64, 0, IBStyles["critical-outline"]*)

; -----------------------------------------------------------------------------

Btn71 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn71, 0, IBStyles["info-round"]*)

Btn72 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn72, 0, IBStyles["success-round"]*)

Btn73 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn73, 0, IBStyles["warning-round"]*)

Btn74 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn74, 0, IBStyles["critical-round"]*)

; -----------------------------------------------------------------------------

Btn81 := MyGui.AddButton("xm y+20 w200 h40", "Info")
CreateImageButton(Btn81, 0, IBStyles["info-outline-round"]*)

Btn82 := MyGui.AddButton("x+20 yp w200 h40", "Success")
CreateImageButton(Btn82, 0, IBStyles["success-outline-round"]*)

Btn83 := MyGui.AddButton("x+20 yp w200 h40", "Warning")
CreateImageButton(Btn83, 0, IBStyles["warning-outline-round"]*)

Btn84 := MyGui.AddButton("x+20 yp w200 h40", "Critical")
CreateImageButton(Btn84, 0, IBStyles["critical-outline-round"]*)

; -----------------------------------------------------------------------------

MyGui.Show()

; ===============================================================================================================================

#Include CreateImageButton.ahk
#Include UseGDIP.ahk
:
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: CreateImageButton() - 2023-07-10

Post by sashaatx » 19 Nov 2023, 05:08

question - for handling resizing controls without overloading or adding on to memory, is a simple gui redraw going to handle the changing of a button size?

for example Im using GuiResizer viewtopic.php?f=83&t=113921 from our friend @FanaticGuru

I before I insert a function to handle creating an image button based on the ControlObject every time a resize event happens, I want to ensure that's not poor handling of memory/cpu
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

WKen
Posts: 182
Joined: 21 Feb 2023, 00:01

Re: CreateImageButton() - 2023-07-10

Post by WKen » 24 Nov 2023, 14:49

This combination will cause Opt2 to lose its gradient color, any chance of making it work? Thanks!

Code: Select all

Opt1 := ["A.png", , 0xE9E9E9]

just me
Posts: 9425
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CreateImageButton() - 2023-07-10

Post by just me » 25 Nov 2023, 06:59

sashaatx wrote: question - for handling resizing controls without overloading or adding on to memory, is a simple gui redraw going to handle the changing of a button size?
Did you try it?
WKen wrote: This combination will cause Opt2 to lose its gradient color, any chance of making it work? Thanks!
Images can be used only with mode 0 which ignores the additional color options.

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: CreateImageButton() - 2023-07-10

Post by sashaatx » 25 Nov 2023, 11:11

just me wrote:
25 Nov 2023, 06:59
sashaatx wrote: question - for handling resizing controls without overloading or adding on to memory, is a simple gui redraw going to handle the changing of a button size?
Did you try it?
WKen wrote: This combination will cause Opt2 to lose its gradient color, any chance of making it work? Thanks!
Images can be used only with mode 0 which ignores the additional color options.
I'm asking a fundamental question as unless I'm unaware, autohotkey doesn't have memory capacity and usage data returned to a script.

I don't use c++, or complex languages that focus/care about memory IE python, ahk is memory efficient but that doesn't mean Im taking the best approach to implementation.

I was curious the best approach. Im using a responsive script that changes GUI button size when window is resized in real time, Im asking if combining these methods is a bad potential approach.

I work on win11 but the workstations Im building for are win7-10. I could see redrawing an image in realtime for every pixel size changed could lock up a computers memory, Or not! That's the question .
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: CreateImageButton() - 2023-07-10

Post by hasantr » 26 Dec 2023, 08:32

I made one dark button ChatGpt coloring over the sashaatx examples. It's not an important job, but maybe I'm sharing it to make someone's job easier.
image.png
image.png (2.52 KiB) Viewed 1399 times
IBStyles["dark"] := [[0xFF1C1C1C, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1], ; Normal Condition: Very Dark Gray
[0xFF262626, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1], ; On Mouse: Darker Gray
[0xFF2F2F2F, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1], ; Mouse Click: Very Dark Gray
[0xFF626262, 0xFF474747, 0xFFFFFFFF, 0, 0xFF474747, 1]] ; Disabled Light Gray

Code: Select all

; =============================================================================================================================
IBStyles := Map()
IBStyles["dark"] := [[0xFF1C1C1C, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1],  ; Normal Condition: Very Dark Gray
                                        [0xFF262626, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1],  ; On Mouse: Darker Gray
                                        [0xFF2F2F2F, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1],  ; Mouse Click: Very Dark Gray
                                        [0xFF626262, 0xFF474747, 0xFFFFFFFF, 0, 0xFF474747, 1]]  ; Disabled Light Gray
UseGDIP()
MyGui := Gui(, "Bootstrap Buttons")
MyGui.BackColor := "000000"
MyGui.MarginX := 20
MyGui.MarginY := 20
MyGui.SetFont("s11", "Segoe UI")
CreateImageButton("SetDefGuiColor", 0xFFF0F0F0)

; -----------------------------------------------------------------------------
BtnDarkSmall := MyGui.AddButton("xm ym w80 h24", "Dark")
CreateImageButton(BtnDarkSmall, 0, IBStyles["dark"]*)

BtnDarkBig := MyGui.AddButton("xm y+20 w200 h40", "Dark Button")
CreateImageButton(BtnDarkBig, 0, IBStyles["dark"]*)

MyGui.Show()
; ===============================================================================================================================
; ----------------------------------------------------------------------------------------------------------------------
; Loads and initializes the Gdiplus.dll.
; Must be called once before you use any of the DLL functions.
; ----------------------------------------------------------------------------------------------------------------------
#DllLoad "Gdiplus.dll"
UseGDIP() {
   Static GdipObject := 0
   If !IsObject(GdipObject) {
      GdipToken := 0
      SI := Buffer(24, 0) ; size of 64-bit structure
      NumPut("UInt", 1, SI)
      If DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", &GdipToken, "Ptr", SI, "Ptr", 0, "UInt") {
         MsgBox("GDI+ could not be startet!`n`nThe program will exit!", A_ThisFunc, 262160)
         ExitApp
      }
      GdipObject := {__Delete: UseGdipShutDown}
   }
   UseGdipShutDown(*) {
      DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", GdipToken)
   }
}
#Include CreateImageButton.ahk
;#Include UseGDIP.ahk

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: CreateImageButton() - 2023-07-10

Post by sashaatx » 26 Dec 2023, 14:30

hasantr wrote:
26 Dec 2023, 08:32
I made one dark button ChatGpt coloring over the sashaatx examples. It's not an important job, but maybe I'm sharing it to make someone's job easier.

image.png
IBStyles["dark"] := [[0xFF1C1C1C, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1], ; Normal Condition: Very Dark Gray
[0xFF262626, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1], ; On Mouse: Darker Gray
[0xFF2F2F2F, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1], ; Mouse Click: Very Dark Gray
[0xFF626262, 0xFF474747, 0xFFFFFFFF, 0, 0xFF474747, 1]] ; Disabled Light Gray

Code: Select all

; =============================================================================================================================
IBStyles := Map()
IBStyles["dark"] := [[0xFF1C1C1C, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1],  ; Normal Condition: Very Dark Gray
                                        [0xFF262626, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1],  ; On Mouse: Darker Gray
                                        [0xFF2F2F2F, 0xFF1A1A1A, 0xFFFFFFFF, 0, 0xFF1A1A1A, 1],  ; Mouse Click: Very Dark Gray
                                        [0xFF626262, 0xFF474747, 0xFFFFFFFF, 0, 0xFF474747, 1]]  ; Disabled Light Gray
UseGDIP()
MyGui := Gui(, "Bootstrap Buttons")
MyGui.BackColor := "000000"
MyGui.MarginX := 20
MyGui.MarginY := 20
MyGui.SetFont("s11", "Segoe UI")
CreateImageButton("SetDefGuiColor", 0xFFF0F0F0)

; -----------------------------------------------------------------------------
BtnDarkSmall := MyGui.AddButton("xm ym w80 h24", "Dark")
CreateImageButton(BtnDarkSmall, 0, IBStyles["dark"]*)

BtnDarkBig := MyGui.AddButton("xm y+20 w200 h40", "Dark Button")
CreateImageButton(BtnDarkBig, 0, IBStyles["dark"]*)

MyGui.Show()
; ===============================================================================================================================
; ----------------------------------------------------------------------------------------------------------------------
; Loads and initializes the Gdiplus.dll.
; Must be called once before you use any of the DLL functions.
; ----------------------------------------------------------------------------------------------------------------------
#DllLoad "Gdiplus.dll"
UseGDIP() {
   Static GdipObject := 0
   If !IsObject(GdipObject) {
      GdipToken := 0
      SI := Buffer(24, 0) ; size of 64-bit structure
      NumPut("UInt", 1, SI)
      If DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", &GdipToken, "Ptr", SI, "Ptr", 0, "UInt") {
         MsgBox("GDI+ could not be startet!`n`nThe program will exit!", A_ThisFunc, 262160)
         ExitApp
      }
      GdipObject := {__Delete: UseGdipShutDown}
   }
   UseGdipShutDown(*) {
      DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", GdipToken)
   }
}
#Include CreateImageButton.ahk
;#Include UseGDIP.ahk
thanks so much just what I needed!
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: 2.0-beta.1: CreateImageButton() - 2021-08-03 alpha.1

Post by sashaatx » 26 Dec 2023, 21:11

ezgif-2-2e6994005c.gif
ezgif-2-2e6994005c.gif (1.06 MiB) Viewed 496 times
Im finally trying out this whole gdpi thing @jNizM, using your bootstrap buttons.
I also use @FanaticGuru's GuiResizer and decided - might as well combine the two.

Here's a method that allows for the responsive design of GuiResizer.ahk while using CreaeteImageButton. I put it together in an hour, I'll clean up any excess variables or inefficiencies over the next day.

viewtopic.php?f=83&t=113921

Code: Select all

DetectHiddenWindows("off")

/*
    Class: StyleButton

    Description:
    This class defines a set of functionalities for configuring and styling buttons. Combining GuiResizer's
    Responsive design and accounting for size changes while applying styles to buttons.

    Requires:
    1. CreateImageButton.ahk
    2. UseGDIP.ahk
    3. GuiResizer.ahk

    Example:
    ButtonOK := myGui.Add("Button", "", "&OK")
    SetButton(ButtonOK, 0, "success") ; success being a map key in IBStyles, any style map should do. 

    Class Functions:
    1. Call(ctrl, offset, style)
        ; see CreateImageButton.ahk for more information
        This function handles the button call and applies styling based on parameters. It gets the position of the button
        and pushes the set function with certain parameters to funcArray based on the button's width and height.

    2. Set(ctrl, style, offset, isEvent?)
       This function sets the button's configuration. It gets the button's position, sets the control size in a map,
       and styles the button by calling the myStyleMap function.

    3. enumButtons(*)
       This function enumerates through the buttons, resets the error flag, and runs each function in the funcArray.

    Nested Functions:
    1. myStyleMap(ctrl, offset, style)
       This defined function styles the button by calling the CreateImageButton function with certain parameters.
*/
class StyleButton
{
    static err := 0
    static buttons := []
    
    static Call(ctrl, offset, style)
    {
        ctrl.GetPos(, , &w, &h)
        StyleButton.buttons.Push({ctrl: ctrl, offset: offset, style: style, w: w, h: h})
        ctrl.Gui.OnEvent("Size", StyleButton.enumButtons)
        if w != 0 && h != 0 && WinActive(ctrl.Gui.hwnd)
        {
            StyleButton.Set(StyleButton.buttons[StyleButton.buttons.Length], offset, style)
        }
    }

    static Set(btn, offset, style)
    {
        btn.ctrl.Gui.GetPos(, , &w, &h)

        if h = 0 or w = 0 or !WinActive(btn.ctrl.Gui.hwnd)
        {
            return
        }
        btn.ctrl.GetPos(, , &w, &h)
        if btn.w < w or btn.h < h
        {
            btn.w := w
            btn.h := h
            btn.ctrl.Opt("w" w " h" h)
        }
        
        UseGDIP()
        if btn.ctrl.gui
            myStyleMap(btn.ctrl, offset, style)
    }

    static enumButtons(*)
    {
        for btn in StyleButton.buttons
        {
            StyleButton.set(btn, btn.offset, btn.style)
        }
    }
}

myStyleMap(ctrl, offset, style) => CreateImageButton(ctrl, offset, IBStyles[style]*)


IBStyles := Map()
; credit to jNizM
; https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=75  

IBStyles["info"] := [[0x80C6E9F4, , , 0, 0x8046B8DA, 1], [0x8086D0E7, , , 0, 0x8046B8DA, 1], [0x8046B8DA, , , 0, 0x8046B8DA, 1], [0xFFF0F0F0, , , 0, 0x8046B8DA, 1]]
IBStyles["success"] := [[0x80C6E6C6, , , 0, 0x805CB85C, 1], [0x8091CF91, , , 0, 0x805CB85C, 1], [0x805CB85C, , , 0, 0x805CB85C, 1], [0xFFF0F0F0, , , 0, 0x805CB85C, 1]]
IBStyles["warning"] := [[0x80FCEFDC, , , 0, 0x80F0AD4E, 1], [0x80F6CE95, , , 0, 0x80F0AD4E, 1], [0x80F0AD4E, , , 0, 0x80F0AD4E, 1], [0xFFF0F0F0, , , 0, 0x80F0AD4E, 1]]
IBStyles["critical"] := [[0x80F0B9B8, , , 0, 0x80D43F3A, 1], [0x80E27C79, , , 0, 0x80D43F3A, 1], [0x80D43F3A, , , 0, 0x80D43F3A, 1], [0xFFF0F0F0, , , 0, 0x80D43F3A, 1]]

IBStyles["info-outline"] := [[0xFFF0F0F0, , , 0, 0x8046B8DA, 1], [0x80C6E9F4, , , 0, 0x8046B8DA, 1], [0x8086D0E7, , , 0, 0x8046B8DA, 1], [0xFFF0F0F0, , , 0, 0x8046B8DA, 1]]
IBStyles["success-outline"] := [[0xFFF0F0F0, , , 0, 0x805CB85C, 1], [0x80C6E6C6, , , 0, 0x805CB85C, 1], [0x8091CF91, , , 0, 0x805CB85C, 1], [0xFFF0F0F0, , , 0, 0x805CB85C, 1]]
IBStyles["warning-outline"] := [[0xFFF0F0F0, , , 0, 0x80F0AD4E, 1], [0x80FCEFDC, , , 0, 0x80F0AD4E, 1], [0x80F6CE95, , , 0, 0x80F0AD4E, 1], [0xFFF0F0F0, , , 0, 0x80F0AD4E, 1]]
IBStyles["critical-outline"] := [[0xFFF0F0F0, , , 0, 0x80D43F3A, 1], [0x80F0B9B8, , , 0, 0x80D43F3A, 1], [0x80E27C79, , , 0, 0x80D43F3A, 1], [0xFFF0F0F0, , , 0, 0x80D43F3A, 1]]

IBStyles["info-round"] := [[0x80C6E9F4, , , 8, 0x8046B8DA, 1], [0x8086D0E7, , , 8, 0x8046B8DA, 1], [0x8046B8DA, , , 8, 0x8046B8DA, 1], [0xFFF0F0F0, , , 8, 0x8046B8DA, 1]]
IBStyles["success-round"] := [[0x80C6E6C6, , , 8, 0x805CB85C, 1], [0x8091CF91, , , 8, 0x805CB85C, 1], [0x805CB85C, , , 8, 0x805CB85C, 1], [0xFFF0F0F0, , , 8, 0x805CB85C, 1]]
IBStyles["warning-round"] := [[0x80FCEFDC, , , 8, 0x80F0AD4E, 1], [0x80F6CE95, , , 8, 0x80F0AD4E, 1], [0x80F0AD4E, , , 8, 0x80F0AD4E, 1], [0xFFF0F0F0, , , 8, 0x80F0AD4E, 1]]
IBStyles["critical-round"] := [[0x80F0B9B8, , , 8, 0x80D43F3A, 1], [0x80E27C79, , , 8, 0x80D43F3A, 1], [0x80D43F3A, , , 8, 0x80D43F3A, 1], [0xFFF0F0F0, , , 8, 0x80D43F3A, 1]]

IBStyles["info-outline-round"] := [[0xFFF0F0F0, , , 8, 0x8046B8DA, 1], [0x80C6E9F4, , , 8, 0x8046B8DA, 1], [0x8086D0E7, , , 8, 0x8046B8DA, 1], [0xFFF0F0F0, , , 8, 0x8046B8DA, 1]]
IBStyles["success-outline-round"] := [[0xFFF0F0F0, , , 8, 0x805CB85C, 1], [0x80C6E6C6, , , 8, 0x805CB85C, 1], [0x8091CF91, , , 8, 0x805CB85C, 1], [0xFFF0F0F0, , , 8, 0x805CB85C, 1]]
IBStyles["warning-outline-round"] := [[0xFFF0F0F0, , , 8, 0x80F0AD4E, 1], [0x80FCEFDC, , , 8, 0x80F0AD4E, 1], [0x80F6CE95, , , 8, 0x80F0AD4E, 1], [0xFFF0F0F0, , , 8, 0x80F0AD4E, 1]]
IBStyles["critical-outline-round"] := [[0xFFF0F0F0, , , 8, 0x80D43F3A, 1], [0x80F0B9B8, , , 8, 0x80D43F3A, 1], [0x80E27C79, , , 8, 0x80D43F3A, 1], [0xFFF0F0F0, , , 8, 0x80D43F3A, 1]]

#Include CreateImageButton.ahk
#Include UseGDIP.ahk




Example

Code: Select all

#Include SetButton.ahk
#Include GuiResizer.ahk
#Requires Autohotkey v2
#SingleInstance Force
SetWinDelay(10)
;AutoGUI 2.5.8 creator: Alguimist autohotkey.com/boards/viewtopic.php?f=64&t=89901
;AHKv2converter creator: github.com/mmikeww/AHK-v2-script-converter
;Easy_AutoGUI_for_AHKv2 github.com/samfisherirl/Easy-Auto-GUI-for-AHK-v2

myGui := Gui()
myGui.opt("+Resize +MinSize250x150")
myGui.OnEvent("Size", GuiResizer)
ButtonOK := myGui.Add("Button", "", "&OK")

OkayAgain := myGui.Add("Button", "x+10", "&OK")
ButtonOK.wp := 0.5
OkayAgain.xp := -0.45
OkayAgain.wp := 0.4
StyleButton(ButtonOK, 0, "success")
StyleButton(OkayAgain, 0, "critical-round")

; Finished := myGui.Add("Button", "", "Finished")
; my custom method for GuiResizer formatting
; GuiReSizer.FormatOpt(Finished, .1, .2, 0.8, 0.2)
; Cancel := myGui.Add("Button", "", "Cancel")
; my custom method for GuiResizer formatting
; GuiReSizer.FormatOpt(Cancel, .1, .5, 0.8, 0.2)

StyleButton(Finished, 0, "info-round")
StyleButton(Cancel, 0, "critical-round")

myGui.OnEvent('Close', (*) => ExitApp())
myGui.Title := "Window"
myGui.Show("w620 h320")


https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Post Reply

Return to “Scripts and Functions (v2)”