Bouton avec Thème et texte en couleur

Partagez votre expérience de PureBasic avec les autres utilisateurs.
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Bouton avec Thème et texte en couleur

Message par nico »

Ce code vient du forum Anglais, comme il m'a semblé plutôt sympa et bien utile, je le poste ici.
http://www.purebasic.fr/english/viewtop ... 12&t=42505

Code : Tout sélectionner

;Coded by c4s
Procedure ColoredButtonGadget(GadgetNr, X, Y, Width, Height, Text.s, TextColor=$000000, TextFontID=#PB_Font_Default)
		Protected ImageNr

		If GadgetNr = #PB_Any : ProcedureReturn -1 : EndIf


		ImageNr = CreateImage(#PB_Any, Width, Height, 32 | #PB_Image_Transparent)
		If ImageNr
				StartDrawing(ImageOutput(ImageNr))
						DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
						DrawingFont(TextFontID)
						DrawText((Width - TextWidth(Text)) / 2, (Height - TextHeight(Text)) / 2, Text, TextColor | $FF000000)
				StopDrawing()

				ButtonImageGadget(GadgetNr, X, Y, Width, Height, ImageID(ImageNr))
		EndIf

		ProcedureReturn ImageNr
EndProcedure


OpenWindow (0, #PB_Ignore, #PB_Ignore, 200, 200, "Button with text color!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Debug ColoredButtonGadget(1, 20, 20, 160, 160, "Test Text", $0000FF)  ; Returns ImageNr

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Avatar de l’utilisateur
Jacobus
Messages : 1559
Inscription : mar. 06/avr./2004 10:35
Contact :

Re: Bouton avec Thème et texte en couleur

Message par Jacobus »

Sympa comme petit effet! ca peut servir.

le même avec un effet Toggle...

Code : Tout sélectionner

Procedure ColoredButtonGadget(GadgetNr, X, Y, Width, Height, Text.s, TextToggle.s, TextColor=$000000, TextFontID=#PB_Font_Default, TextToggleColor=$000000, TextToggleFontID=#PB_Font_Default)
      Protected ImageNr,ImageNr2

      If GadgetNr = #PB_Any : ProcedureReturn -1 : EndIf


      ImageNr = CreateImage(#PB_Any, Width, Height, 32 | #PB_Image_Transparent)
      If ImageNr
            StartDrawing(ImageOutput(ImageNr))
                  DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
                  DrawingFont(TextFontID)
                  DrawText((Width - TextWidth(Text)) / 2, (Height - TextHeight(Text)) / 2, Text, TextColor | $FF000000)
            StopDrawing()
      EndIf
      
      ImageNr2 = CreateImage(#PB_Any, Width, Height, 32 | #PB_Image_Transparent)
      If ImageNr2
            StartDrawing(ImageOutput(ImageNr2))
                  DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
                  DrawingFont(TextToggleFontID)
                  DrawText((Width - TextWidth(TextToggle)) / 2, (Height - TextHeight(TextToggle)) / 2, TextToggle, TextToggleColor | $FF000000)
            StopDrawing()
      EndIf    
                    
      ButtonImageGadget(GadgetNr, X, Y, Width, Height, ImageID(ImageNr),#PB_Button_Toggle)
      SetGadgetAttribute(GadgetNr, #PB_Button_PressedImage, ImageID(ImageNr2))

      ProcedureReturn ImageNr
EndProcedure
    

If LoadFont(1, "Arial", 18)
EndIf 

OpenWindow (0, #PB_Ignore, #PB_Ignore, 200, 200, "Button with text color!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ColoredButtonGadget(1, 20, 20, 160, 80, "Test Text","Toggle effect", $0000FF,RGB(29, 31, 226))  ; Returns ImageNr

ColoredButtonGadget(2, 20, 100, 160, 80, "Text Bleu","Texte Rouge", RGB(29, 31, 226),FontID(1),$0000FF,FontID(1))  ; Returns ImageNr/ImageNr2


Repeat
  EventID = WaitWindowEvent()
  If EventID = #PB_Event_Gadget
    Select EventGadget()
      
      Case 1 : Debug "Bouton 1"
     
      
      Case 2 : Debug "Bouton 2"
     
    
    EndSelect
  EndIf
Until EventID = #PB_Event_CloseWindow
End
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
gildev
Messages : 380
Inscription : mar. 19/juin/2007 10:28
Localisation : Picardie (France)

Re: Bouton avec Thème et texte en couleur

Message par gildev »

Hmmm... C'est bien tout ça. Hop dans la popoche! :lol:
Avatar de l’utilisateur
kernadec
Messages : 1606
Inscription : ven. 25/avr./2008 11:14

Re: Bouton avec Thème et texte en couleur

Message par kernadec »

bonjour
pour ce code j'ai remercié Arctic Fox et c4s du forum anglais.
mais j'avais oublié que j'avais déjà vu un code identique réalisé par le soldat inconnu ici :
http://www.purebasic.fr/french/viewtopi ... &hilit=%23
donc je lui adresse aussi un merci.. pour son talent.

le post du forum anglais à remis le sujet au gout du jour et comme maintenant il y de nouvelles commandes "gradient"
je n'ai pas eu de mal à adapter cette fonction sur les boutons.

mais on pourrait aussi avec ce programme freeware :http://www.clubic.com/telecharger-fiche ... eknop.html
faire des images de bouton 3D sympa et ainsi skinner nos boutons.
bien sur, il y a d'autres programmes dans le même genre mais il sont pas gratuit..

cordialement

Code : Tout sélectionner

Enumeration
  #windows
  #ButtonGadget1
  #ButtonGadget2
  #TextGadget1
  #TextGadget2
  #TextGadget3
  #TextGadget4
  #StringGadget1
  #StringGadget2
  #StringGadget3
  #StringGadget4
  #StringGadget5
  #StringGadget6
  #StringGadget7
  #StringGadget8
EndEnumeration
Global cb.l,cf.l,TextColor.l,Textshadow.l,passe.l
Procedure.f GradientCallback(x, y)
  ProcedureReturn Sin(x * 0.1) - Cos(y * 0.1) ; will be between 0 and 1
EndProcedure
Procedure ColoredButtonGadget(GadgetNr,X,Y,Width,Height,Text.s,TextFontID,TextColor,Textshadow)
  Protected ImageNr
  
  If GadgetNr = #PB_Any : ProcedureReturn -1 : EndIf
  
  ImageNr = CreateImage(#PB_Any,Width,Height,32 );| #PB_Image_Transparent
  If ImageNr
    StartDrawing(ImageOutput(ImageNr))
    DrawingMode(#PB_2DDrawing_Gradient) 
    If passe=0
      cb=BackColor(RGB(Random(255),Random(255),Random(255)))
      cf=FrontColor(RGB(Random(255),Random(255),Random(255)))
    Else
      BackColor(cb)
      FrontColor(cf)
    EndIf
    Debug "Back=RGB("+Str(Red(cb))+","+Str(Green(cb))+","+Str(Blue(cb))+") : Front=RGB("+Str(Red(cf))+","+Str(Green(cf))+","+Str(Blue(cf))+")"
    Debug "TextColor=RGB("+Str(Red(TextColor))+","+Str(Green(TextColor))+","+Str(Blue(TextColor))+") : Textshadow=RGB("+Str(Red(Textshadow))+","+Str(Green(Textshadow))+","+Str(Blue(Textshadow))+")"
    ; EllipticalGradient(Width/2,Height/2,Width/2,Height/3)
    BoxedGradient(0,0,Width,Height)          
    Box(0,0,Width,Height)
    DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
    DrawingFont(TextFontID)
    DrawText((Width - TextWidth(Text)) / 2+1,(Height - TextHeight(Text)) / 2-1,Text,Textshadow | $FF000000)
    DrawText((Width - TextWidth(Text)) / 2,(Height - TextHeight(Text)) / 2,Text,TextColor | $FF000000)
    StopDrawing()
    
    ButtonImageGadget(GadgetNr,X,Y,Width,Height,ImageID(ImageNr))
  EndIf
  
  
  ProcedureReturn ImageNr
EndProcedure
CreateImage(21, 200, 300)
OpenWindow (#windows,#PB_Ignore,#PB_Ignore,200,300,"Button with text color!",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Text.s="Thanks"
LoadFont(0,"Arial",8)
StartDrawing(ImageOutput(21))
DrawingMode(#PB_2DDrawing_Gradient) 
BackColor(RGB(Random(255),Random(255),Random(255)))
FrontColor(RGB(Random(255),Random(255),Random(255)))
;BoxedGradient(0,0,200,300)
;CustomGradient(@GradientCallback())
LinearGradient(0,0,200,300)
Box(0, 0, 200,300) 
DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
DrawingFont(FontID(0))
DrawText(20,20,"Text RGB:",RGB(0,0,0) | $FF000000)
DrawText(20,43,"ShadeTx RGB:",RGB(0,0,0) | $FF000000)
DrawText(20,242,"Back:",RGB(0,0,0) | $FF000000)
DrawText(20,267,"Front:",RGB(0,0,0) | $FF000000)
StopDrawing() 
ImageGadget(20, 0, 0,200,300, ImageID(21))
DisableGadget(20, 1)

StringGadget(#StringGadget1,95,15,25,20,"180",#PB_String_Numeric)
StringGadget(#StringGadget2,125,15,25,20,"128",#PB_String_Numeric)
StringGadget(#StringGadget3,155,15,25,20,"0",#PB_String_Numeric)

StringGadget(#StringGadget4,95,40,25,20,"50",#PB_String_Numeric)
StringGadget(#StringGadget5,125,40,25,20,"50",#PB_String_Numeric)
StringGadget(#StringGadget6,155,40,25,20,"50",#PB_String_Numeric)

StringGadget(#StringGadget7,60,240,120,20,"RGB("+Str(Red(cb))+","+Str(Green(cb))+","+Str(Blue(cb))+")",#PB_String_ReadOnly )
StringGadget(#StringGadget8,60,265,120,20,"RGB("+Str(Red(cf))+","+Str(Green(cf))+","+Str(Blue(cf))+")",#PB_String_ReadOnly )

TextColor=RGB(Val(GetGadgetText(#StringGadget1)),Val(GetGadgetText(#StringGadget2)),Val(GetGadgetText(#StringGadget3)))
Textshadow=RGB(Val(GetGadgetText(#StringGadget4)),Val(GetGadgetText(#StringGadget5)),Val(GetGadgetText(#StringGadget6)))

ColoredButtonGadget(#ButtonGadget1,20,70,160,160,Text,LoadFont(1,"Arial",26,#PB_Font_Bold),TextColor,Textshadow)  ; Returns ImageNr
ButtonGadget(#ButtonGadget2,72,15,20,20,"?")

Repeat 
  event= WaitWindowEvent()
  Select EventGadget()
    Case #ButtonGadget1
      passe=0
      If Text="Thanks":Text="Merci":Else:Text="Thanks":EndIf
      TextColor=RGB(Val(GetGadgetText(#StringGadget1)),Val(GetGadgetText(#StringGadget2)),Val(GetGadgetText(#StringGadget3)))
      Textshadow=RGB(Val(GetGadgetText(#StringGadget4)),Val(GetGadgetText(#StringGadget5)),Val(GetGadgetText(#StringGadget6)))
      ColoredButtonGadget(#ButtonGadget1,20,70,160,160,Text,LoadFont(1,"Arial",26,#PB_Font_Bold),TextColor,Textshadow)
      SetGadgetText(#StringGadget7,"RGB("+Str(Red(cb))+","+Str(Green(cb))+","+Str(Blue(cb))+")")
      SetGadgetText(#StringGadget8,"RGB("+Str(Red(cf))+","+Str(Green(cf))+","+Str(Blue(cf))+")")
    Case  #ButtonGadget2
      passe=1
      If Text="Thanks":Text="Merci":Else:Text="Thanks":EndIf
      TextColor=RGB(Val(GetGadgetText(#StringGadget1)),Val(GetGadgetText(#StringGadget2)),Val(GetGadgetText(#StringGadget3)))
      Textshadow=RGB(Val(GetGadgetText(#StringGadget4)),Val(GetGadgetText(#StringGadget5)),Val(GetGadgetText(#StringGadget6)))
      ColoredButtonGadget(#ButtonGadget1,20,70,160,160,Text,LoadFont(1,"Arial",26,#PB_Font_Bold),TextColor,Textshadow)
      SetGadgetText(#StringGadget7,"RGB("+Str(Red(cb))+","+Str(Green(cb))+","+Str(Blue(cb))+")")
      SetGadgetText(#StringGadget8,"RGB("+Str(Red(cf))+","+Str(Green(cf))+","+Str(Blue(cf))+")")
  EndSelect
Until event= #PB_Event_CloseWindow



;#;new  procedure ........
;#Procedure ColoredButtonGadget(GadgetNr,X,Y,Width,Height,Text.s,TextFontID,cf,cb,TextColor,Textshadow)
;#Protected ImageNr
;# 
;# If GadgetNr = #PB_Any : ProcedureReturn -1 : EndIf
;# 
;# ImageNr = CreateImage(#PB_Any,Width,Height,32 );| #PB_Image_Transparent
;# If ImageNr
;#   StartDrawing(ImageOutput(ImageNr))
;#   DrawingMode(#PB_2DDrawing_Gradient) 
;#   BackColor(cb)
;#   FrontColor(cf)
;#   BoxedGradient(0,0,Width,Height)      
;#   Box(0,0,Width,Height)
;#   DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
;#   DrawingFont(TextFontID)
;#   DrawText((Width - TextWidth(Text)) / 2+1,(Height - TextHeight(Text)) / 2-1,Text,Textshadow | $FF000000)
;#   DrawText((Width - TextWidth(Text)) / 2,(Height - TextHeight(Text)) / 2,Text,TextColor | $FF000000)
;#   StopDrawing()
;#   
;#   ButtonImageGadget(GadgetNr,X,Y,Width,Height,ImageID(ImageNr))
;# EndIf
;# 
;#ProcedureReturn ImageNr
;#EndProcedure
;#Global cb.l,cf.l,TextColor.l,Textshadow.l,front.l,back.l
;#OpenWindow (0,#PB_Ignore,#PB_Ignore,200,200,"Button with text color!",#PB_Window_SystemMenu | #PB_Window_ScreenCentered);#
;#
;#; copy 2 lines debugger:
;#Back=RGB(78,89,221) : Front=RGB(185,211,38)
;#TextColor=RGB(180,128,0) : Textshadow=RGB(50,50,50)
;#;-----------------------------------------------------
;#
;#ColoredButtonGadget(1,20,20,160,160,"CANCEL",LoadFont(1,"Arial",26,#PB_Font_Bold),front,back,TextColor,Textshadow)
;#Repeat 
;#  event= WaitWindowEvent()
;#  Select EventGadget()
;#    Case 1
;#  EndSelect
;#Until event= #PB_Event_CloseWindow

Dernière modification par kernadec le sam. 10/juil./2010 17:43, modifié 1 fois.
Avatar de l’utilisateur
SPH
Messages : 4947
Inscription : mer. 09/nov./2005 9:53

Re: Bouton avec Thème et texte en couleur

Message par SPH »

Kern, ton code est tres sympa !!

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.12LTS- 64 bits
Répondre