Frame3DGadet avec couleurs personnalisées

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Le Soldat Inconnu
Messages : 4312
Inscription : mer. 28/janv./2004 20:58
Localisation : Clermont ferrand OU Olsztyn
Contact :

Frame3DGadet avec couleurs personnalisées

Message 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
Je ne suis pas à moitié Polonais mais ma moitié est polonaise ... Vous avez suivi ?

[Intel quad core Q9400 2.66mhz, ATI 4870, 4Go Ram, XP (x86) / 7 (x64)]
Anonyme2
Messages : 3518
Inscription : jeu. 22/janv./2004 14:31
Localisation : Sourans

Re: Frame3DGadet avec couleurs personnalisées

Message par Anonyme2 »

Très bon, j'adopte.
Avatar de l’utilisateur
Kwai chang caine
Messages : 6989
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Frame3DGadet avec couleurs personnalisées

Message par Kwai chang caine »

C'est beau 8O
Soldat t'es vraiment une valeur pour ce forum, merci 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Le Soldat Inconnu
Messages : 4312
Inscription : mer. 28/janv./2004 20:58
Localisation : Clermont ferrand OU Olsztyn
Contact :

Re: Frame3DGadet avec couleurs personnalisées

Message 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
Je ne suis pas à moitié Polonais mais ma moitié est polonaise ... Vous avez suivi ?

[Intel quad core Q9400 2.66mhz, ATI 4870, 4Go Ram, XP (x86) / 7 (x64)]
Avatar de l’utilisateur
venom
Messages : 3137
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: Frame3DGadet avec couleurs personnalisées

Message 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.





@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Répondre