Code : Tout sélectionner
;************************************************************************************************************************
; Author : MicrodevWeb
; Project Name : Tooltip for canvas
; File Name : TootipCanvas.pb
; Description : Comment affiché une bulle d'aide à un canvas
; Version: B0.1 Date of begin : 2016/04/
;************************************************************************************************************************
DeclareModule tst
Declare AddForm(X,Y,W,H,BgColor,FgColor,HelpMsg.s)
Declare Init(X.i,Y.i,W.i,H.i)
Declare Draw()
EndDeclareModule
Module tst
;-* Structure et liste pou afiché des formes dans un canvas
Structure pos
X.i
Y.i
W.i
H.i
EndStructure
Structure form
myPos.pos
helpMsg.s
BgColor.i
FgColor.i
EndStructure
Structure tst
myPos.pos
List myForm.form()
idForm.i
idCanvas.i
idHelpWin.i
HelpOn.b
BgColor.i
FgColor.i
TxtFont.i
TxtColor.i
EndStructure
Global myTst.tst
Global gMouseX,gMouseY
;}
;-* LOCAL PROCEDURE
Procedure HideHelp()
If myTst\HelpOn
; Enfin on affiche la fenêtre
HideWindow(myTst\idHelpWin,#True,#PB_Window_NoActivate)
; Et on signale que la fenêtre n'est plus affichée
myTst\HelpOn=#False
EndIf
EndProcedure
Procedure DisplayHelp()
Protected Margin=10,W,H,X,Y
; On dessine dans la fenêtre Bulle d'aide
StartDrawing(WindowOutput(myTst\idHelpWin))
DrawingFont(FontID(myTst\TxtFont))
; La largeur
W=TextWidth(myTst\myForm()\helpMsg)+(Margin*2)
; La hauteur
H=TextHeight(myTst\myForm()\helpMsg)+(Margin*2)
; On mémorise la position ou sera affichée la fenêtre
; On prend pour cela la position souris relative à la fenêtre
X=WindowMouseX(myTst\idForm)
; On dessend également la position Y pour ne pâs marqué le curseur
Y=WindowMouseY(myTst\idForm)+40
; On vérifier que la bulle d'aide n'est en dehors de la fenêtre
If (X+W)>(WindowMouseX(myTst\idForm)+WindowWidth(myTst\idForm))-Margin
X=(WindowMouseX(myTst\idForm)+WindowWidth(myTst\idForm))-Margin
EndIf
If (Y+H)>(WindowMouseY(myTst\idForm)+WindowHeight(myTst\idForm))-Margin
Y=(WindowMouseY(myTst\idForm)+WindowHeight(myTst\idForm))-Margin
EndIf
; On redimentionne la fenêtre pour la bulle d'aide
ResizeWindow(myTst\idHelpWin,X,Y,W,H)
; Si l'aide n'est pas déjà affichée
; On dessine le fond
DrawingMode(#PB_2DDrawing_Default)
Box(0,0,W,H,myTst\BgColor)
; On dessine le cadre
DrawingMode(#PB_2DDrawing_Outlined)
Box(0,0,W,H,myTst\FgColor)
; On dessine le texte
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(Margin,Margin,myTst\myForm()\helpMsg,myTst\TxtColor)
StopDrawing()
If myTst\HelpOn=#False
; Enfin on affiche la fenêtre
HideWindow(myTst\idHelpWin,#False,#PB_Window_NoActivate)
; Et on signale que la fenêtre est affichée
myTst\HelpOn=#True
EndIf
EndProcedure
Procedure IsHoverForm()
ForEach myTst\myForm()
With myTst\myForm()\myPos
If (gMouseX>=\X And gMouseX<=(\X+\W)) And (gMouseY>=\Y And gMouseY<=(\Y+\H))
ProcedureReturn #True
EndIf
EndWith
Next
ProcedureReturn #False
EndProcedure
Procedure WhereIsMouse()
; On est sur une forme
If IsHoverForm()
; On affiche la bulle d'aide
DisplayHelp()
; On change le curseur
SetGadgetAttribute(myTst\idCanvas,#PB_Canvas_Cursor,#PB_Cursor_Hand)
; On ne va pas plus loint
ProcedureReturn
EndIf
; Ici on est sur aucun élément donc on affiche le curseur normal
SetGadgetAttribute(myTst\idCanvas,#PB_Canvas_Cursor,#PB_Cursor_Default)
; Masque la bulle d'aide
HideHelp()
EndProcedure
Procedure Event()
; Si aucune forme on sort
If ListSize(myTst\myForm())=0:ProcedureReturn :EndIf
Select EventType()
; La souris bouge
Case #PB_EventType_MouseMove
; On relève les positions de la souris (relative au canvas)
gMouseX=GetGadgetAttribute(myTst\idCanvas,#PB_Canvas_MouseX)
gMouseY=GetGadgetAttribute(myTst\idCanvas,#PB_Canvas_MouseY)
; Regarde on est ?
WhereIsMouse()
; On est en dehors du canvas
Case #PB_EventType_MouseLeave
; Si en dehors du canvas
HideHelp()
EndSelect
EndProcedure
;}
;-* PUBLIC PROCEDURE
Procedure AddForm(X,Y,W,H,BgColor,FgColor,HelpMsg.s)
With myTst\myForm()
AddElement(myTst\myForm())
\BgColor=BgColor
\FgColor=FgColor
\helpMsg=HelpMsg
\myPos\X=X
\myPos\Y=Y
\myPos\W=W
\myPos\H=H
EndWith
EndProcedure
Procedure Init(X.i,Y.i,W.i,H.i)
Protected flag=#PB_Window_BorderLess|#PB_Window_Invisible
With myTst\myPos
\X=X
\Y=Y
\W=W
\H=H
myTst\idForm=GetActiveWindow()
; Création du canvas dans la fenêtre
myTst\idCanvas=CanvasGadget(#PB_Any,X,Y,W,H)
EndWith
With myTst
; Ouverture de la fenêtre pour afficher l'aide invisible
\idHelpWin=OpenWindow(#PB_Any,0,0,100,100,"",flag,WindowID(\idForm))
StickyWindow(myTst\idHelpWin,#True)
; Par défaut l'aide n'est pas active
\HelpOn=#False
; Couleur et police pour la bulle d'aide
\BgColor=RGB(135, 206, 250)
\FgColor=RGB(8, 127, 200)
\TxtColor=RGB(1, 24, 38)
\TxtFont=LoadFont(#PB_Any,"Arial",11,#PB_Font_HighQuality)
; Mise en place du callback
BindGadgetEvent(\idCanvas,@Event())
EndWith
EndProcedure
Procedure Draw()
StartDrawing(CanvasOutput(myTst\idCanvas))
ForEach myTst\myForm()
With myTst\myForm()
DrawingMode(#PB_2DDrawing_Default)
Box(\myPos\X,\myPos\Y,\myPos\W,\myPos\H,\BgColor)
DrawingMode(#PB_2DDrawing_Outlined)
Box(\myPos\X,\myPos\Y,\myPos\W,\myPos\H,\FgColor)
EndWith
Next
StopDrawing()
EndProcedure
;}
EndModule
; Code pour le teste
Procedure Exit()
End
EndProcedure
Define Flag,X
Flag=#PB_Window_SystemMenu|#PB_Window_Maximize
OpenWindow(0,0,0,800,600,"teste",Flag)
tst::Init(10,10,800,200)
X=10
tst::AddForm(X,10,50,50,RGB(255, 0, 0),RGB(5, 2, 37),"Voici le carré rouge")
X+60
tst::AddForm(X,10,50,50,RGB(15, 177, 76),RGB(5, 2, 37),"Voici le carré vert")
X+60
tst::AddForm(X,10,50,50,RGB(249, 245, 122),RGB(5, 2, 37),"Voici le carré jaune")
tst::Draw()
BindEvent(#PB_Event_CloseWindow,@Exit(),0)
Repeat :WaitWindowEvent():ForEver