Page 1 sur 1

SetButtonColor

Publié : sam. 31/janv./2026 14:07
par Micoute
Bonjour à tous.

J'avais trouver un programme qui fonctionnait bien jusqu'à la version 6.21 de PB et qui ne fonctionne plus depuis la 6.30, alors j'ai eus l'idée de faire
une version compatible.

Code : Tout sélectionner

Procedure SetButtonColor(idBtn, FrtColor, BkgColor)
  Protected Text.s = GetGadgetText(idBtn)
  Protected L = GadgetWidth(idBtn)
  Protected H = GadgetHeight(idBtn)
  
  ; Police (à adapter si tu veux une autre)
  LoadFont(0, "FontAwesome", 20, #PB_Font_Bold)
  
  ; Création de l'image du bouton
  Protected idImg = CreateImage(#PB_Any, L, H, 32)
  If idImg
    StartDrawing(ImageOutput(idImg))
      Box(0, 0, L, H, BkgColor)              ; IMPORTANT : coordonnées (0,0) dans l'image
      DrawingFont(FontID(0))
      FrontColor(FrtColor)
      BackColor(BkgColor)
      DrawText((L - TextWidth(Text)) / 2,
               (H - TextHeight(Text)) / 2,
               Text)
    StopDrawing()
    
    ; Désactiver le thème Windows pour garder ton rendu
    SetWindowTheme_(GadgetID(idBtn), "", "")
    
    ; Appliquer l'image au bouton
    SetGadgetAttribute(idBtn, #PB_Button_Image, ImageID(idImg))
  EndIf
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Global btnI_B1
Enumeration
  #btnI_B2
EndEnumeration

If OpenWindow(#PB_Any, 0, 0, 240, 150, "SetButtonColor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  btnI_B1 = ButtonImageGadget(#PB_Any, 50, 20, 130, 40, 0)
  SetGadgetText(btnI_B1, "Bouton 1")
  SetButtonColor(btnI_B1, $FFFFFF, RGB(65,114,173))
  
  ButtonImageGadget(#btnI_B2, 50, 80, 130, 40, 0)
  SetGadgetText(#btnI_B2, "Bouton 2")
  SetButtonColor(#btnI_B2, $FFFFFF, RGB(28,94,62))
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case btnI_B1
            SetButtonColor(btnI_B1, RGB(Random(255), Random(255), Random(255)),
                                     RGB(Random(255), Random(255), Random(255)))
        EndSelect
      Case #PB_Event_CloseWindow
        Break
    EndSelect
  ForEver
EndIf
CompilerEndIf

Re: SetButtonColor

Publié : dim. 01/févr./2026 9:32
par Jacobus
Salut Micoute,
Merci pour ta version compatible de ce bouton, mais ceux qui comme moi ont un grand écran ont besoin d'un petit détail supplémentaire : le facteur DPI. Sinon l'affichage n'est pas terrible.
Il faut modifier ta procédure comme ça : :wink:

Code : Tout sélectionner

;-DPI (prise en compte nécessaire pour les grands écrans)
  Global dpix.d = DesktopResolutionX()
  Global dpiy.d = DesktopResolutionY()
  
Procedure SetButtonColor(idBtn, FrtColor, BkgColor)
  Protected Text.s = GetGadgetText(idBtn)
  Protected L = GadgetWidth(idBtn)
  Protected H = GadgetHeight(idBtn)
  
  ; Police (à adapter si tu veux une autre)
  LoadFont(0, "FontAwesome", 20, #PB_Font_Bold)
  
  ; Création de l'image du bouton
  Protected idImg = CreateImage(#PB_Any, L*dpix, H*dpiy, 32)
  If idImg
    StartDrawing(ImageOutput(idImg))
      Box(0, 0, L*dpix, H*dpix, BkgColor)              ; IMPORTANT : coordonnées (0,0) dans l'image
      DrawingFont(FontID(0))
      FrontColor(FrtColor)
      BackColor(BkgColor)
      DrawText((L*dpix - TextWidth(Text)) / 2,
               (H*dpix - TextHeight(Text)) / 2,
               Text)
    StopDrawing()
    
    ; Désactiver le thème Windows pour garder ton rendu
    SetWindowTheme_(GadgetID(idBtn), "", "")
    
    ; Appliquer l'image au bouton
    SetGadgetAttribute(idBtn, #PB_Button_Image, ImageID(idImg))
  EndIf
EndProcedure

Re: SetButtonColor

Publié : dim. 01/févr./2026 9:33
par Syntax Horror
Merci Micoute :P