SetButtonColor

Programmation d'applications complexes
Avatar de l’utilisateur
Micoute
Messages : 2614
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

SetButtonColor

Message 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
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 6.20 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Avatar de l’utilisateur
Jacobus
Messages : 1601
Inscription : mar. 06/avr./2004 10:35
Contact :

Re: SetButtonColor

Message 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
Quand tous les glands seront tombés, les feuilles dispersées, la vigueur retombée... Dans la morne solitude, ancré au coeur de ses racines, c'est de sa force maturité qu'il renaîtra en pleine magnificence...Jacobus.
Avatar de l’utilisateur
Syntax Horror
Messages : 3
Inscription : jeu. 29/janv./2026 11:32

Re: SetButtonColor

Message par Syntax Horror »

Merci Micoute :P
Répondre