Page 1 sur 1

Frame3DGadet avec couleurs personnalisées

Publié : ven. 02/oct./2009 10:01
par Le Soldat Inconnu
Salut,

Je reprend un code d'une discussion sur des gadgets personnalisés pour le mettre en évidence
http://www.purebasic.fr/french/viewtopic.php?f=6&t=9788

il s'agit de Frame3D avec couleur de fond personnalisable, couleur de bordure personnalisable, et police personnalisable

Code : Tout sélectionner

; Auteur : Le Soldat Inconnu
; Version de PB : 4.40
;
; Explication du programme :
; Frame3D personnalisé

Procedure.l LoadWindowFont(Bold = -1, Italic = -1, UnderLine = -1, Size.f = -1)
  Protected ncm.NONCLIENTMETRICS
  ncm\cbSize = SizeOf(NONCLIENTMETRICS)
  SystemParametersInfo_(#SPI_GETNONCLIENTMETRICS, SizeOf(NONCLIENTMETRICS), @ncm, 0)
  If Bold = 0
    ncm\lfMessageFont\lfWeight = 0
	ElseIf Bold = 1
    ncm\lfMessageFont\lfWeight = 700
	EndIf
  If Italic = 0
    ncm\lfMessageFont\lfItalic = 0
	ElseIf Italic = 1
    ncm\lfMessageFont\lfItalic = 1
	EndIf
  If UnderLine = 0
    ncm\lfMessageFont\lfUnderline = 0
	ElseIf UnderLine = 1
    ncm\lfMessageFont\lfUnderline = 1
	EndIf
  If Size > 0
    ncm\lfMessageFont\lfheight * Size
	EndIf
  
  ProcedureReturn CreateFontIndirect_(@ncm\lfMessageFont)
EndProcedure
Procedure CircleAA(x, y, Radius, Color, Thickness = 1, Mode = #PB_2DDrawing_Default)
  Protected n, nn, Distance.f, Application.f, Couleur_Fond.l
  If Mode & #PB_2DDrawing_Outlined ; Cercle vide
    ; on dessine 1/4 du cercle et on duplique pour le reste
    For n = 0 To Radius
      For nn = 0 To Radius
        Distance.f = Sqr(n * n + nn * nn)
        If Distance <= Radius And Distance > Radius - 1
          Application.f = 1 - Abs(Radius - 1 - Distance)
          Alpha = 255 * Application
					Plot(x + n, y + nn, Color | Alpha << 24)
					Plot(x - n, y + nn, Color | Alpha << 24)
					Plot(x + n, y - nn, Color | Alpha << 24)
					Plot(x - n, y - nn, Color | Alpha << 24)
				ElseIf Distance <= Radius - Thickness And Distance > Radius - Thickness - 1
          Application.f = 1 - Abs(Radius - Thickness - Distance)
          Alpha = 255 * Application
					Plot(x + n, y + nn, Color | Alpha << 24)
					Plot(x - n, y + nn, Color | Alpha << 24)
					Plot(x + n, y - nn, Color | Alpha << 24)
					Plot(x - n, y - nn, Color | Alpha << 24)
				ElseIf Distance <= Radius - 1 And Distance > Radius - Thickness
          Plot(x + n, y + nn, Color | $FF000000)
          Plot(x - n, y + nn, Color | $FF000000)
          Plot(x + n, y - nn, Color | $FF000000)
          Plot(x - n, y - nn, Color | $FF000000)
				EndIf
			Next
		Next
	Else ; Cercle plein
    ; on dessine 1/4 du cercle et on duplique pour le reste
    For n = 0 To Radius
      For nn = 0 To Radius
        Distance.f = Sqr(n * n + nn * nn)
        If Distance <= Radius And Distance > Radius - 1
          Application.f = (Radius - Distance)
          Alpha = 255 * Application
					Plot(x + n, y + nn, Color | Alpha << 24)
					Plot(x - n, y + nn, Color | Alpha << 24)
					Plot(x + n, y - nn, Color | Alpha << 24)
					Plot(x - n, y - nn, Color | Alpha << 24)
				ElseIf Distance <= Radius - 1
          Plot(x + n, y + nn, Color | $FF000000)
          Plot(x - n, y + nn, Color | $FF000000)
          Plot(x + n, y - nn, Color | $FF000000)
          Plot(x - n, y - nn, Color | $FF000000)
				EndIf
			Next
		Next
	EndIf
EndProcedure

Procedure Frame3DGadgetEx(Gadget, x, y, Width, Height, Text.s, FontID.l = 0, BorderColor.l = -1, BackColor.l = -1)
	Protected Retour.l, Image.l, Image_G.l, Image_D.l, Image_H.l, Image_B.l, WindowsFont.l
	
	#Frame3DGadgetEx_Border = 16
	#Frame3DGadgetEx_Round = 16
	#Frame3DGadgetEx_TextBorder = 4
	
	If BorderColor = -1
		BorderColor = $000000
	EndIf
	
	Image = CreateImage(#PB_Any, Width, Height, 32)
	StartDrawing(ImageOutput(Image))
		; Fond totalement transparent
		DrawingMode(#PB_2DDrawing_AlphaChannel)
		Box(0, 0, Width, Height, 0)
		DrawingMode(#PB_2DDrawing_AlphaBlend)
		If BackColor >= 0
			; Dessin des angles arrondis du fond
			CircleAA(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, #Frame3DGadgetEx_Round - 1, BackColor)
			CircleAA(Width - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, #Frame3DGadgetEx_Round - 1, BackColor)
			CircleAA(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, Height - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Round - 1, BackColor)
			CircleAA(Width - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, Height - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Round - 1, BackColor)
		EndIf
		; Dessin des angles arrondis du Frame3D
		CircleAA(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, #Frame3DGadgetEx_Round - 1, BorderColor, 2, #PB_2DDrawing_Outlined)
		CircleAA(Width - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, #Frame3DGadgetEx_Round - 1, BorderColor, 2, #PB_2DDrawing_Outlined)
		CircleAA(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round - 1, Height - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Round - 1, BorderColor, 2, #PB_2DDrawing_Outlined)
		CircleAA(Width - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, Height - #Frame3DGadgetEx_Border / 2 - #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Round - 1, BorderColor, 2, #PB_2DDrawing_Outlined)
		If BackColor >= 0
			; Dessin des bords du fond
			Box(#Frame3DGadgetEx_Border / 2 + 1, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, Width - 2 - #Frame3DGadgetEx_Border, Height - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, BackColor | $FF000000)
			Box(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Border / 2 + 1, Width - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, Height - 2 - #Frame3DGadgetEx_Border, BackColor | $FF000000)
		Else
			; Ou si il n'y a pas de fond, on efface les parties qui ne doivent pas être dessinées (on dessine un cercle complet pour les angles donc il faut effacer pour avoir des quart de cercle)
			DrawingMode(#PB_2DDrawing_AlphaChannel)
			Box(#Frame3DGadgetEx_Border / 2 + 1, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, Width - 2 - #Frame3DGadgetEx_Border, Height - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, $00000000)
			Box(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Border / 2 + 1, Width - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, Height - 2 - #Frame3DGadgetEx_Border, $00000000)
			DrawingMode(#PB_2DDrawing_AlphaBlend)
		EndIf
		; Dessin des bords du Frame3D
		Box(#Frame3DGadgetEx_Border / 2 + 1, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, 2, Height - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, BorderColor | $FF000000)
		Box(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, #Frame3DGadgetEx_Border / 2 + 1, Width - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, 2, BorderColor | $FF000000)
		Box(Width - #Frame3DGadgetEx_Border / 2 - 3, #Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, 2, Height - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, BorderColor | $FF000000)
		Box(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Round, Height - #Frame3DGadgetEx_Border / 2 - 3, Width - #Frame3DGadgetEx_Round * 2 - #Frame3DGadgetEx_Border, 2, BorderColor | $FF000000)
		
		; Chargement de la police
		If FontID
			DrawingFont(FontID)
		Else
			WindowsFont = LoadWindowFont()
			DrawingFont(WindowsFont)
		EndIf
		
		; On dessine l'espace pour la police
		DrawingMode(#PB_2DDrawing_AlphaChannel)
		Box(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Border, #Frame3DGadgetEx_Border / 2 + 1, TextWidth(Text) + #Frame3DGadgetEx_TextBorder * 2, 2, $00000000)
		If BackColor >= 0
			Box(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Border, #Frame3DGadgetEx_Border / 2 + 3, TextWidth(Text) + #Frame3DGadgetEx_TextBorder * 2, #Frame3DGadgetEx_Border, BackColor | $FF000000)
		EndIf
		
		; Dessin du titre du Frame3D
		DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
		DrawText(#Frame3DGadgetEx_Border / 2 + #Frame3DGadgetEx_Border + #Frame3DGadgetEx_TextBorder, (#Frame3DGadgetEx_Border - TextHeight(Text)) / 2, Text, BorderColor | $FF000000)
		
		If WindowsFont
			DeleteObject_(WindowsFont)
		EndIf
		
	StopDrawing()
	
	Image_G = GrabImage(Image, #PB_Any, 0, 0, #Frame3DGadgetEx_Border, Height)
	ImageGadget(#PB_Any, x, y, #Frame3DGadgetEx_Border, Height, ImageID(Image_G))
	Image_D = GrabImage(Image, #PB_Any, Width - #Frame3DGadgetEx_Border, 0, #Frame3DGadgetEx_Border, Height)
	ImageGadget(#PB_Any, x + Width - #Frame3DGadgetEx_Border, y, #Frame3DGadgetEx_Border, Height, ImageID(Image_D))
	Image_H = GrabImage(Image, #PB_Any, #Frame3DGadgetEx_Border, 0, Width - 2 * #Frame3DGadgetEx_Border, #Frame3DGadgetEx_Border)
	ImageGadget(#PB_Any, x + #Frame3DGadgetEx_Border, y, Width - 2 * #Frame3DGadgetEx_Border, #Frame3DGadgetEx_Border, ImageID(Image_H))
	Image_B = GrabImage(Image, #PB_Any, #Frame3DGadgetEx_Border, Height - #Frame3DGadgetEx_Border, Width - 2 * #Frame3DGadgetEx_Border, #Frame3DGadgetEx_Border)
	ImageGadget(#PB_Any, x + #Frame3DGadgetEx_Border, y + Height - #Frame3DGadgetEx_Border, Width - 2 * #Frame3DGadgetEx_Border, #Frame3DGadgetEx_Border, ImageID(Image_B))
	
	Retour = ContainerGadget(Gadget, x + #Frame3DGadgetEx_Border, y + #Frame3DGadgetEx_Border, Width - 2 * #Frame3DGadgetEx_Border, Height - 2 * #Frame3DGadgetEx_Border, #PB_Container_BorderLess)
		If Gadget = #PB_Any
			Gadget = Retour
		EndIf
		If BackColor >= 0
			SetGadgetColor(Gadget, #PB_Gadget_BackColor, BackColor)
		EndIf
	CloseGadgetList()
	
	ProcedureReturn Retour
EndProcedure


; Création de la fenêtre et de la GadgetList
If OpenWindow(0, 0, 0, 300, 300, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget) = 0
  End
EndIf

Gadget = Frame3DGadgetEx(#PB_Any, 10, 10, 200, 200, "Frame3D", 0, $00A0FF, $FFFFFF)

; On met un bouton dans le frame
OpenGadgetList(Gadget)
	ButtonGadget(1, 10, 10, 100, 30, "Button")
CloseGadgetList()

LoadFont(0, "Verdana", 9, #PB_Font_Bold)

Frame3DGadgetEx(10, 10, 220, 280, 60, "No background", FontID(0), $00A0FF)


Repeat
  Event = WaitWindowEvent()
  
  Select Event
		Case #PB_Event_Menu
      Select EventMenu() ; Menus
          
			EndSelect
      
		Case #PB_Event_Gadget
      Select EventGadget() ; Gadgets
          
			EndSelect
	EndSelect
  
Until Event = #PB_Event_CloseWindow

Re: Frame3DGadet avec couleurs personnalisées

Publié : sam. 03/oct./2009 12:34
par Anonyme2
Très bon, j'adopte.

Re: Frame3DGadet avec couleurs personnalisées

Publié : sam. 03/oct./2009 17:38
par Kwai chang caine
C'est beau 8O
Soldat t'es vraiment une valeur pour ce forum, merci 8)

Re: Frame3DGadet avec couleurs personnalisées

Publié : sam. 03/oct./2009 19:38
par Le Soldat Inconnu
J'ai corrigé une petite boulette dans mon code

j'ai profite également pour une petite remarque :

Ce type pseudo gadget est en fait composé de 5 gadgets (4 ImageGadget + 1 ContainerGadget)
Donc si vous voulez le déplacer ou le redimensionner, ce n'est pas faisable dans l'état actuel.
Il faudra adapter un peu pour ça

Re: Frame3DGadet avec couleurs personnalisées

Publié : dim. 04/oct./2009 0:21
par venom
yep, sa a un bon rendu avec ton post sur les png ont peut faire de très belles fenêtres.
merci de ces codes.





@++