Ben non, comme je l'écris c'est comme une "feuille de dessins"...
Maintenant, tu peux toujours "dessiner" des gadgets... Comme le fait "NetMaestro" dans ce superbe exemple
Code : Tout sélectionner
;=============================================================
; Library: TrackSelectionGadget
; Author: Lloyd Gallant (netmaestro)
; Date: April 30, 2011
; Target compiler: Purebasic 4.60 and later
; Target OS: Microsoft Windows all
; License: Free, unrestricted, no warranty
;=============================================================
; I wanted To give the new CanvasGadget a run through its paces And I got this idea from a question
; in the coding questions forum. It's a selection tool that allows you to set an upper and lower selection
; point in a large set of <whatever>, say, movie frames For example. I obviously haven't tapped into every
; feature of the canvas gadget in this project but it's being put To a pretty reasonable test imho.
; So far it performs exactly As advertised With no disappointments. Beta 2, 1 really As this wasn't
; improved For 2 And the performance is rock-solid. Really impressive For something this far from its
; release date. Probably a few bugs in it, it's early. But here's the Canvas Gadget implemented As a
; TrackSelection tool.
; ---------------------------
; Je voulais donner au CanvasGadget une nouvelle forme d'usage et j'ai eu cette idée suite à une
; question sur le forum. C'est un outil de sélection qui vous permet de définir un Point de sélection
; supérieure et inférieure d'une zone, disons, une image vidéo par exemple ou page editeur de texte.
; Je n'ai évidemment pas exploité toutes les fonctionnalités du Gadget "Canvas" dans ce projet mais il est
; mis soumis à un test raisonnable dans cet exemple. Jusqu'à présent, il se comporte exactement comme annoncé
; sans déceptions. Cela dans les version Bêta 2, 1 vraiment et comme cela n'a pas été améliorée pour la Beta 3
; et la performance semble très stable. Vraiment impressionnant pour une version aussi loin de sa date de
; libération. Il y a/aura probablement quelques bugs dedans ? Il est tôt pour le dire. mais voici le Gadget
; "CanvasGadget" mis en œuvre comme un outil de "TrackSelection".
;
; How to use the gadget :
;
; - Leftclick and drag the lower pointer to set the selection Endpoint
; - Leftclick and drag the upper pointer to set the selection Startpoint
; - Fine-tune the selections with the mousewheel while the mousepointer is over the appropriate pointer
; - Move the selection endpoint one increment higher/lower with the Right/Left tArrow keys
; - Move the selection startpoint one increment higher/lower with the Shift/Right and Shift/left arrow keys
; - Leftclicks outside the thumbs affect the startpoint if the mousepointer is in the top half of the gadget,
; and the endpoint if the mousepointer is in the bottom half.
;
; - Note: keyboard control of fine-tuning is scrapped in favor of the mousewheel only.
; -------------------------------------------------------------------------------------------
; Comment utiliser le Gadget !
;
; - Click Gauche : et faites glisser le curseur du bas pour regler la fin de zone
; - Click Gauche : et faites glisser le curseur du dessus pour régler le début de la zone
; - Ajustez finement le pointage des curseurs en plaçant la souris sur le curseur approprié
; et actionner la molette de la souris...
; - Déplacer le curseur de début d'un point avec la Maj+ Flèches Gauche/droite
; - Déplacer le curseur de fin de zone d'un point avec la Ctrl + Flèches Gauche/droite
; - Leftclicks dans la zone sans les curseurs déplace l'un ou l'autre curseurs vers l'endroit
; ou se trouve le pointeur de la souris
;
Structure TrackData
p_region1.l
p_region2.l
baseimage.l
baseimagetop.l
baseimageleft.l
bclientwidth.l
baseimagewidth.l
pointer1.l
pointer2.l
selection_start.l
selection_end.l
selection_max.l
tickmin.l
tickmax.l
tickleftx.l
tickrightx.l
pointer1top.l
pointer2top.l
pointer1x.l
pointer2x.l
pointermin.l
pointermax.l
startchanged.l
endchanged.l
displayfont.l
EndStructure
Procedure CreatePointers()
Protected ptr, i, j
ptr = CreateImage(#PB_Any, 11,30,32|#PB_Image_Transparent)
Protected Dim pcolors(4)
pcolors(0) = RGBA(0,0,0,0)
pcolors(1) = GetSysColor_(#COLOR_3DDKSHADOW) |$FFFFFFFFFF000000
pcolors(2) = GetSysColor_(#COLOR_3DSHADOW) |$FFFFFFFFFF000000
pcolors(3) = GetSysColor_(#COLOR_3DFACE) |$FFFFFFFFFF000000
pcolors(4) = GetSysColor_(#COLOR_3DHIGHLIGHT)|$FFFFFFFFFF000000
StartDrawing(ImageOutput(ptr))
DrawingMode(#PB_2DDrawing_AllChannels)
; Dessous
Restore base : For i=0 To 10 : Read.i Index : Plot(i,0,pcolors(Index)) : Next
For j=1 To 9 : Restore body : For i=0 To 10 : Read.i Index : Plot(i,j,pcolors(Index)) : Next : Next
Restore pdn : For j=10 To 14 : For i=0 To 10 : Read.i Index : Plot(i,j,pcolors(Index)) : Next : Next
; Dessus
Restore pup : For j=15 To 19 : For i=0 To 10 : Read.i Index : Plot(i,j,pcolors(Index)) : Next : Next
For j=20 To 28 : Restore body : For i=0 To 10 : Read.i Index : Plot(i,j,pcolors(Index)) : Next : Next
Restore base : For i=0 To 10 : Read.i Index : Plot(i,29,pcolors(Index)) : Next
StopDrawing()
ProcedureReturn ptr
DataSection
base: Data.i 4,4,4,4,4,4,4,4,4,4,1
body: Data.i 4,3,3,3,3,3,3,3,3,2,1
pdn: Data.i 0,4,3,3,3,3,3,3,2,1,0,0,0,4,3,3,3,3,2,1,0,0,0,0,0,4,3,3
Data.i 2,1,0,0,0,0,0,0,0,4,2,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
pup: Data.i 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,4,2,1,0,0,0,0,0,0,0,4,3,3
Data.i 2,1,0,0,0,0,0,4,3,3,3,3,2,1,0,0,0,4,3,3,3,3,3,3,2,1,0
EndDataSection
EndProcedure
Procedure RedrawTrackSelectionGadget(Gadget)
*p.TrackData = GetGadgetData(Gadget)
StartDrawing(CanvasOutput(Gadget))
Box(0,0,GadgetWidth(Gadget),GadgetHeight(Gadget),GetSysColor_(#COLOR_3DFACE))
DrawImage(ImageID(*p\baseimage),*p\baseimageleft,*p\baseimagetop)
Box(*p\tickleftx,*p\baseimagetop+2,*p\tickrightx-*p\tickleftx+1,11,GetSysColor_(#COLOR_MENUHILIGHT))
DrawAlphaImage(ImageID(*p\pointer1),*p\pointer1x,*p\pointer1top)
DrawAlphaImage(ImageID(*p\pointer2),*p\pointer2x,*p\pointer2top)
Box(*p\tickmin-17,*p\pointer1top-11,35,10, GetSysColor_(#COLOR_INFOBK))
DrawingMode(#PB_2DDrawing_Outlined)
Box(*p\tickmin-17,*p\pointer1top-11,35,10,GetSysColor_(#COLOR_3DDKSHADOW))
DrawingFont(*p\displayfont)
DrawingMode(#PB_2DDrawing_Transparent)
txt$ = Str(*p\selection_start)
w = TextWidth(txt$)/2
DrawText(*p\tickmin-w,*p\pointer1top-11,txt$,GetSysColor_(#COLOR_INFOTEXT))
DrawingMode(#PB_2DDrawing_Default)
Box(*p\tickmin-17,*p\pointer2top+16,35,10,GetSysColor_(#COLOR_INFOBK))
DrawingMode(#PB_2DDrawing_Outlined)
Box(*p\tickmin-17,*p\pointer2top+16,35,10,GetSysColor_(#COLOR_3DDKSHADOW))
DrawingFont(*p\displayfont)
DrawingMode(#PB_2DDrawing_Transparent)
txt$ = Str(*p\selection_end)
w = TextWidth(txt$)/2
DrawText(*p\tickmin-w,*p\pointer2top+16,txt$,GetSysColor_(#COLOR_INFOTEXT))
StopDrawing()
EndProcedure
ProcedureDLL TrackSelectGadget(gadgetnumber, x, Y, Width, min=0, max=100, drawfocus=0)
tmp = CreatePointers()
pointer1 = GrabImage(tmp, #PB_Any,0,0,11,15)
pointer2 = GrabImage(tmp, #PB_Any,0,15,11,15)
FreeImage(tmp)
If drawfocus
FLAGS = #PB_Canvas_Keyboard|#PB_Canvas_GrabMouse|#PB_Canvas_DrawFocus
Else
FLAGS = #PB_Canvas_Keyboard|#PB_Canvas_GrabMouse
EndIf
Protected Height = 60
If gadgetnumber = #PB_Any
result = CanvasGadget(#PB_Any, x, Y, Width, Height, FLAGS)
gadgetnumber = result
Else
result = CanvasGadget(gadgetnumber, x, Y, Width, Height, FLAGS)
EndIf
; Curseurs 1 -------
h1 = CreateRectRgn_(0,0,11,10)
h2 = CreateRectRgn_(1,10,10,11)
CombineRgn_(h1,h1,h2,#RGN_XOR)
h2 = CreateRectRgn_(2,11,9,12)
CombineRgn_(h1,h1,h2,#RGN_XOR)
h2 = CreateRectRgn_(3,12,8,13)
CombineRgn_(h1,h1,h2,#RGN_XOR)
h2 = CreateRectRgn_(4,13,7,14)
CombineRgn_(h1,h1,h2,#RGN_XOR)
h2 = CreateRectRgn_(5,14,6,15)
CombineRgn_(h1,h1,h2,#RGN_XOR)
; Curseurs 2 -------
h3 = CreateRectRgn_(5,0,6,1)
h4 = CreateRectRgn_(4,1,7,2)
CombineRgn_(h3,h3,h4,#RGN_XOR)
h4 = CreateRectRgn_(3,2,8,3)
CombineRgn_(h3,h3,h4,#RGN_XOR)
h4 = CreateRectRgn_(2,3,9,4)
CombineRgn_(h3,h3,h4,#RGN_XOR)
h4 = CreateRectRgn_(1,4,10,5)
CombineRgn_(h3,h3,h4,#RGN_XOR)
h4 = CreateRectRgn_(0,5,11,15)
CombineRgn_(h3,h3,h4,#RGN_XOR)
;
*gadgetdata.TrackData = AllocateMemory(SizeOf(TrackData))
With *gadgetdata
\p_region1 = h1
\p_region2 = h3
\baseimage = baseimage
\baseimagetop = 23
\baseimageleft = 20
\baseimagewidth = Width-36
\bclientwidth = \baseimagewidth-5
\pointer1 = pointer1
\pointer2 = pointer2
\selection_start = 0
\selection_end = 0
\selection_max = max
\tickmin = 22
\tickmax = Width-19
\pointer1top = 15
\pointer2top = 30
\tickleftx = 22
\tickrightx = 22
\pointer1x = 17
\pointer2x = 17
\pointermin = 17
\pointermax = Width-24
\displayfont = LoadFont(0,"verdana", 6)
EndWith
SetGadgetData(gadgetnumber, *gadgetdata)
*gadgetdata\baseimage = CreateImage(#PB_Any, *gadgetdata\baseimagewidth,15)
StartDrawing(ImageOutput(*gadgetdata\baseimage))
Box(0,0,*gadgetdata\baseimagewidth-1,1,GetSysColor_(#COLOR_3DSHADOW))
Box(0,0,1,14,GetSysColor_(#COLOR_3DSHADOW))
Box(1,1,1,12,GetSysColor_(#COLOR_3DDKSHADOW))
Box(1,1,*gadgetdata\baseimagewidth-3,1,GetSysColor_(#COLOR_3DDKSHADOW))
Box(2,2,*gadgetdata\baseimagewidth-4,11,GetSysColor_(#COLOR_3DHIGHLIGHT))
Box(1,13,*gadgetdata\baseimagewidth-3,1,GetSysColor_(#COLOR_3DFACE))
Box(0,14,*gadgetdata\baseimagewidth-1,1,GetSysColor_(#COLOR_3DHIGHLIGHT))
Box(*gadgetdata\baseimagewidth-2,1,1,13,GetSysColor_(#COLOR_3DFACE))
Box(*gadgetdata\baseimagewidth-1,0,1,15,GetSysColor_(#COLOR_3DHIGHLIGHT))
StopDrawing()
RedrawTrackSelectionGadget(gadgetnumber)
ProcedureReturn result
EndProcedure
ProcedureDLL GetTrackSelectionStart(Gadget)
*p.TrackData = GetGadgetData(Gadget)
ProcedureReturn *p\selection_start
EndProcedure
ProcedureDLL GetTrackSelectionEnd(Gadget)
*p.TrackData = GetGadgetData(Gadget)
ProcedureReturn *p\selection_end
EndProcedure
ProcedureDLL CheckSelectionStartChanged(Gadget)
*p.TrackData = GetGadgetData(Gadget)
If *p\startchanged
*p\startchanged=0
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL CheckSelectionEndChanged(Gadget)
*p.TrackData = GetGadgetData(Gadget)
If *p\endchanged
*p\endchanged=0
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
ProcedureDLL HandleTrackEvents(EventGadget,EventType)
Static hot1, hot2, xoffset
*p.TrackData = GetGadgetData(EventGadget)
mx = GetGadgetAttribute(EventGadget,#PB_Canvas_MouseX)
my = GetGadgetAttribute(EventGadget,#PB_Canvas_MouseY)
Select EventType
;
Case #PB_EventType_KeyDown
key = GetGadgetAttribute(EventGadget, #PB_Canvas_Key)
mods = GetGadgetAttribute(EventGadget, #PB_Canvas_Modifiers)
Select key
Case #PB_Shortcut_Right
If mods & #PB_Canvas_Shift
*p\pointer1x = *p\tickleftx-5
If *p\tickleftx < *p\tickmax
*p\tickleftx+1
*p\pointer1x+1
If *p\tickrightx < *p\tickleftx
*p\tickrightx = *p\tickleftx
*p\pointer2x = *p\tickrightx-5
EndIf
EndIf
s.d = Round(((*p\tickleftx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_start = Int(s)
*p\startchanged = 1
If *p\selection_start > *p\selection_end
*p\selection_end = *p\selection_start
*p\endchanged = 1
EndIf
RedrawTrackSelectionGadget(EventGadget)
Else
*p\pointer2x = *p\tickrightx-5
If *p\tickrightx < *p\tickmax
*p\tickrightx+1
*p\pointer2x+1
EndIf
e.d = Round(((*p\tickrightx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_end = Int(e)
*p\endchanged = 1
RedrawTrackSelectionGadget(EventGadget)
EndIf
Case #PB_Shortcut_Left
If mods & #PB_Canvas_Shift
*p\pointer1x = *p\tickleftx-5
If *p\tickleftx > *p\tickmin
*p\tickleftx-1
*p\pointer1x-1
EndIf
s.d = Round(((*p\tickleftx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_start = Int(s)
*p\startchanged = 1
RedrawTrackSelectionGadget(EventGadget)
Else
*p\pointer2x = *p\tickrightx-5
If *p\tickrightx > *p\tickmin
*p\tickrightx-1
*p\pointer2x-1
If *p\tickleftx > *p\tickrightx
*p\tickleftx = *p\tickrightx
*p\pointer1x = *p\tickleftx-5
EndIf
EndIf
e.d = Round(((*p\tickrightx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_end = Int(e)
*p\endchanged = 1
If *p\selection_end < *p\selection_start
*p\selection_start = *p\selection_end
*p\startchanged = 1
EndIf
RedrawTrackSelectionGadget(EventGadget)
EndIf
EndSelect
Case #PB_EventType_LeftButtonDown
If mx <= *p\tickrightx And mx >= *p\tickmin And (my <= 26)
*p\pointer1x = mx-5
If *p\pointer1x < *p\pointermin
*p\pointer1x = *p\pointermin
EndIf
*p\tickleftx = *p\pointer1x+5
s.d = Round(((*p\tickleftx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_start = Int(s)
*p\startchanged = 1
hot1 = #True : #hot2 = #False
RedrawTrackSelectionGadget(EventGadget)
ElseIf mx >= *p\tickleftx And mx <=*p\tickmax And (my >= 28)
*p\pointer2x = mx-5
If *p\pointer2x > *p\pointermax
*p\pointer2x = *p\pointermax
EndIf
*p\tickrightx = *p\pointer2x+5
e.d = Round(((*p\tickrightx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_end = Int(e)
*p\endchanged = 1
hot2 = #True : hot1 = #False
RedrawTrackSelectionGadget(EventGadget)
EndIf
Case #PB_EventType_MouseWheel
delta = GetGadgetAttribute(EventGadget, #PB_Canvas_WheelDelta)
If PtInRegion_(*p\p_region2, mx-*p\pointer2x, my-*p\pointer2top)
If delta>0
thisone = *p\selection_end
e.d = Round(((*p\tickrightx+1-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
nextone = Int(e)
testone = thisone+1
*p\pointer2x = *p\tickrightx-5
If testone<nextone
If *p\selection_end < nextone And *p\selection_end < *p\selection_max
*p\selection_end+1
*p\endchanged = 1
RedrawTrackSelectionGadget(EventGadget)
EndIf
EndIf
ElseIf delta < 0
If *p\pointer2x = *p\tickrightx-5
thisone = *p\selection_end
e.d = Round(((*p\tickrightx-1-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
nextone = Int(e)
testone = thisone-1
*p\pointer2x = *p\tickrightx-5
If testone>nextone
If *p\selection_end > nextone And *p\selection_end > *p\selection_start
*p\selection_end-1
*p\endchanged = 1
RedrawTrackSelectionGadget(EventGadget)
EndIf
EndIf
EndIf
EndIf
ElseIf PtInRegion_(*p\p_region1, mx-*p\pointer1x, my-*p\pointer1top)
If delta > 0
thisone = *p\selection_start
s.d = Round(((*p\tickleftx+1-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
nextone = Int(s)
testone = thisone+1
*p\pointer1x = *p\tickleftx-5
If testone>0
If testone<nextone
If *p\selection_start < nextone And *p\selection_start < *p\selection_end
*p\selection_start+1
*p\startchanged = 1
RedrawTrackSelectionGadget(EventGadget)
EndIf
EndIf
EndIf
ElseIf delta < 0
thisone = *p\selection_start
s.d = Round(((*p\tickleftx-1-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
nextone = Int(s)
testone = thisone-1
*p\pointer1x = *p\tickleftx-5
If testone>=0
If testone>nextone
If *p\selection_start > nextone And *p\selection_start > 0
*p\selection_start-1
*p\startchanged = 1
RedrawTrackSelectionGadget(EventGadget)
EndIf
EndIf
EndIf
EndIf
EndIf
Case #PB_EventType_LeftButtonUp
hot1 = #False
hot2 = #False
Case #PB_EventType_MouseMove
buttonstate = GetGadgetAttribute(EventGadget, #PB_Canvas_Buttons)
lbutton = buttonstate & #PB_Canvas_LeftButton
If lbutton
If hot1
*p\pointer1x = mx-xoffset
If *p\pointer1x < *p\pointermin
*p\pointer1x = *p\pointermin
ElseIf *p\pointer1x > *p\pointermax
*p\pointer1x = *p\pointermax
EndIf
*p\tickleftx = *p\pointer1x+5
If *p\tickrightx < *p\tickleftx
*p\tickrightx = *p\tickleftx
*p\pointer2x = *p\tickrightx-5
EndIf
s.d = Round(((*p\tickleftx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_start = Int(s)
*p\startchanged = 1
If *p\selection_start > *p\selection_end
*p\selection_end = *p\selection_start
*p\endchanged = 1
EndIf
RedrawTrackSelectionGadget(EventGadget)
ElseIf hot2
*p\pointer2x = mx-xoffset
If *p\pointer2x < *p\pointermin
*p\pointer2x = *p\pointermin
ElseIf *p\pointer2x > *p\pointermax
*p\pointer2x = *p\pointermax
EndIf
*p\tickrightx = *p\pointer2x+5
If *p\tickleftx > *p\tickrightx
*p\tickleftx = *p\tickrightx
*p\pointer1x = *p\tickleftx-5
EndIf
e.d = Round(((*p\tickrightx-*p\tickmin)/*p\bclientwidth) * *p\selection_max, #PB_Round_Nearest)
*p\selection_end = Int(e)
*p\endchanged = 1
If *p\selection_end < *p\selection_start
*p\selection_start = *p\selection_end
*p\startchanged = 1
EndIf
EndIf
RedrawTrackSelectionGadget(EventGadget)
Else
If PtInRegion_(*p\p_region1, mx-*p\pointer1x, my-*p\pointer1top)
SetGadgetAttribute(EventGadget, #PB_Canvas_Cursor,#PB_Cursor_Hand)
hot1 = #True : hot2 = #False
xoffset = mx-*p\pointer1x
ElseIf PtInRegion_(*p\p_region2, mx-*p\pointer2x, my-*p\pointer2top)
SetGadgetAttribute(EventGadget, #PB_Canvas_Cursor,#PB_Cursor_Hand)
hot2 = #True : hot1 = #False
xoffset = mx-*p\pointer2x
Else
hot1=#False : hot2 = #False
SetGadgetAttribute(EventGadget, #PB_Canvas_Cursor,#PB_Cursor_Default)
EndIf
EndIf
EndSelect
EndProcedure
; ========================================================
; End of Library Code
; ========================================================
;
; Test program
;
OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ButtonGadget(0,20,20,100,20,"Button")
TrackSelectGadget(1,20,360,600,0,20129,1)
;
Repeat
EV = WaitWindowEvent()
Select EV
Case #PB_Event_Gadget
Select EventGadget()
Case 1
HandleTrackEvents(EventGadget(), EventType())
EndSelect
EndSelect
Until EV = #PB_Event_CloseWindow
End
; IDE Options = PureBasic 4.60 Beta 3 (Windows - x86)
; CursorPosition = 176
; FirstLine = 165
; Folding = --
; CurrentDirectory = C:\0 0 PureBasic46\PureBasic\
; CompileSourceDirectory
Voilà, et c'est pas de moi...