PDA

View Full Version : How to retrieve thumb position of canvas?



EmbeddedMan
12-02-2018, 20:33
When I create a large canvas, set it to virtual mode, and use the scroll bars to move around it, how do I receive events (like %WM_HSCROLL) for those scroll bars? I'd like to update other controls in my main window as the user drags the canvas around under the viewport.

I have set my canvas up to use a callback, and in that callback I'm looking for %WM_HSCROLL and %WM_VSCROLL but I never see them come through.

Thanks-

*Brian

ErosOlmi
13-02-2018, 09:12
Mmmm, not sure you can actually trap that event.

Canvass callback handle only 4 events:

%STN_CLICKED
Sent when the user clicks a mouse button on the graphic control (unless the image control has been disabled).
%STN_DBLCLK
Sent when the user double-clicks on a graphic control (unless the control has been disabled).
%STN_DISABLE
Sent when a graphic control has been disabled.
%STN_ENABLE
Sent when a graphic control has been enabled.



http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?control_add_canvas.htm

I have to study if it is possible and how.
Maybe subclassing the control.

EmbeddedMan
13-02-2018, 15:09
That's OK - I've found a workaround for now. I simply read the position of the viewport from within a timer, which gives me the position the user has moved the thumb to.

*Brian

ErosOlmi
13-02-2018, 15:19
Ah, OK, using Canvas_GetView (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?canvas_getview.htm), right?

In any case I will see what I can do.
Not all functionalities are developed in thinBasic but very often we develop new features when someone is interested.

EmbeddedMan
13-02-2018, 16:02
Eros- yes, that is exactly correct. This system works pretty well actually.

If I could ask for the moon, it would be very helpful to be able to control the width of the thumb on the canvas. For example, with extremely wide canvases, the horizontal scroll thumb becomes very narrow, making it harder to grab with a mouse (especially if you have gloves on, or are using a touchscreen PC). If we had control over that width, it would be kinda nice.

Absolutely not a critical thing though. Very low priority request.

Thanks!

*Brian

ErosOlmi
13-02-2018, 22:24
Not sure I will be able to remove automatic scrollbar or resize it. Will check.

A quick and dirty possible solution: add a new scrollbar bigger than the automatic one and use both to scroll our virtual window.



Uses "UI"

Dim x,y As Double

Begin ControlID
%ID_Canvas
%ID_hScroll
End ControlID

Function TBMain() As Long
Local hDlg As DWord '---Used to store window handle of main dialog

hDlg = Dialog_New Pixels, 0, "very wide graphics ", -1, -1, 1200, 775,
%WS_DLGFRAME |
%DS_CENTER |
%WS_CAPTION |
%WS_SYSMENU |
%WS_OVERLAPPEDWINDOW

Dialog Show Modal hDlg, Call cbDialog
End Function

CallBack Function cbDialog() As Long
STATIC lPos, lMin, lMax, lStep, lPage AS LONG
static vVirtualWidth as Long

Select Case CBMSG
Case %WM_INITDIALOG
Control Add Canvas, CBHNDL, %ID_Canvas, "", 50, 10,1000, 400, %WS_BORDER | %WS_CHILD | %WS_VISIBLE
Canvas_Attach(CBHNDL, %ID_Canvas, %FALSE)
vVirtualWidth = 100000
Canvas_SetVirtual(100000, 380)
Canvas_SetView(0,0)
Canvas_Width(2)
Canvas_Clear %BLACK


'---Add a scrollbar big enough to be used with fingers
Control add SCROLLBAR CBHNDL, %ID_hScroll, "", 50, 430,1000, 100
lStep = 2
lPage = 10
lMax = 110 'set maximum to intended max + page size to allow page scroll to max
lMin = 1
lPos = 1
SCROLLBAR_SETPAGESIZE CBHNDL, %ID_hScroll, lPage
SCROLLBAR_SETRANGE CBHNDL, %ID_hScroll, lMin, lMax
SCROLLBAR_SETPOS CBHNDL, %ID_hScroll, lPos

'---Draw something till 2000
For x = 0 To 2000 Step 0.1
y = Sin(x)*20* Rndf(1, 2)*Cos(x/3)^2
Canvas_Width(2)
Canvas_Line( (x*10, y +100), (x*10, y +100) , Rgb(255, 255, 0) )
Canvas_Redraw
Next

CASE %WM_HSCROLL
'Process messages and perform calculations to change thumb position
SELECT CASE LOWRD(CBWPARAM)
CASE %SB_LINELEFT
lPos -= lStep
CASE %SB_LINERIGHT
lPos += lStep
CASE %SB_PAGELEFT
lPos -= lPage
CASE %SB_PAGERIGHT
lPos += lPage
CASE %SB_TOP
lPos = lMin
CASE %SB_BOTTOM
lPos = lMax
CASE %SB_THUMBTRACK
lPos = HI(WORD, CBWPARAM)
lPos -= mod(lPos, lStep)
CASE ELSE
EXIT FUNCTION
END SELECT

'test limits, position thumb and update label showing position
IF lPos > (lMax - lPage) THEN lPos = (lMax - lPage)
IF lPos < lMin THEN lPos = lMin
SCROLLBAR_SETPOS CBHNDL, %ID_hScroll, lPos

Canvas_SetView(vVirtualWidth / 100 * lpos, 0)

End Select

End Function




9822