Canvas Boost - Procedures utiles
Publié : mar. 20/nov./2012 18:12

Je compte me faire une lib pour me faciliter la vie lors de l'utilisation de Canvas.
Voilà les deux 1ères procédures et un exemple d'utilisation.
- PaintBackGround_Canvas(Gadget, Color, BorderColor = $0, BorderSize = 2)
- CanvasText(Gadget, canvas_text.s, Fcolor, BGcolor, FONT_NAME.s, FONT_SIZE = 12)
S'en serve qui voudra

Code : Tout sélectionner
; LIB Canvas_Boost par Ar-S / 2012 - PB 5.00 / Nov 2012
; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; // Repeindre le canvas avec ou sans bordure. Pour ne pas mettre la bordure, indiquez juste la même couleur pour Color et BorderColor
; // Paint Canvas With or Without a border. To not see border color, just put the same color to Color and BorderColor value
; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Procedure.i PaintBackGround_Canvas(Gadget, Color, BorderColor = $0, BorderSize = 2)
StartDrawing(CanvasOutput(Gadget))
Box(0, 0, GadgetWidth(Gadget), GadgetHeight(Gadget), BorderColor)
Box(BorderSize, BorderSize, GadgetWidth(Gadget)-(BorderSize*2), GadgetHeight(Gadget)-(BorderSize*2), Color)
StopDrawing()
EndProcedure
; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; Resultat = CanvasText(Gadget, canvas_text.s, Fcolor, BGcolor, FONT_NAME.s, FONT_SIZE = 12)
; Retourne 1 si réussi, 0 si echec
; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Procedure CanvasText(Gadget, canvas_text.s, Fcolor, BGcolor, FONT_NAME.s, FONT_SIZE = 12)
Protected LF, LC, HC, LTxt, Htxt, DiffL, DiffH, XTexte, YTexte
If LF = 0
FontB = LoadFont(#PB_Any, FONT_NAME, FONT_SIZE , #PB_Font_HighQuality)
LF = 1
EndIf
If StartDrawing(CanvasOutput(Gadget)) <> 0
DrawingFont(FontID(FontB))
LC = GadgetWidth(Gadget)
HC = GadgetHeight(Gadget)
LTxt = TextWidth(canvas_text.s)
HTxt = TextHeight(canvas_text.s)
DiffL = LC - LTxt
DiffH = HC - HTxt
XTexte = DiffL / 2
YTexte = DiffH / 2
; Ecriture du texte et ombre portée
; Text and DropShadow
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(XTexte-1, YTexte+1, canvas_text, BGcolor)
DrawText(XTexte, YTexte, canvas_text, Fcolor )
StopDrawing()
retour = 1
Else
retour = 0
EndIf
ProcedureReturn retour
EndProcedure
;; ////////////////////////////////////////////////////// EXEMPLE //////////////////////////////////////////////////////
hwnd = OpenWindow(0, #PB_Ignore, #PB_Ignore, 400, 400, "Canvas", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
If hwnd <> 0
CanvasGadget (1, 1, 1, 150,150)
CanvasGadget (2, 151, 1, 200,200)
CanvasGadget (3, 1, 201, 350,100)
; ; ////////////// TEST DU RESULTAT ICI / TEST & RESULT HERE ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
; Peindre le canvas en noir avec une bordure rouge ($FF) de 10 pixels. / Paint Canvas with a 10 px Red ($FF) border
PaintBackGround_Canvas(1, $0 ,$FF, 10)
PaintBackGround_Canvas(2, $0 ,#Green, 2)
PaintBackGround_Canvas(3, #Red , $0, 4)
; Centre le texte en police Microsoft Sans Serif de taille 24 ; Draw a Center Text with shadow in "Microsoft Sans Serif" size 24
CanvasText(1, "Hello World !", $FFFFFF, $FF, "Microsoft Sans Serif", 16)
CanvasText(2, "Hello World !", $00CCFF, $0, "Microsoft Sans Serif", 20)
CanvasText(3, "PURE BASIC !", $0, $0, "Microsoft Sans Serif", 25)
; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Repeat
If WaitWindowEvent() = #PB_Event_CloseWindow And EventWindow() = 0
CloseWindow(0)
End
EndIf
ForEver
EndIf
; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ FIN EXEMPLE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\