PDA

View Full Version : canvas loops and question



Lionheart008
31-03-2011, 20:43
hello. some weeks ago I have made this little loop example with canvas. there are two problems:

a) how I can stop the loop by "pause" modus? how I can stop clearly the loop ?
b) I wanted to make a second loop (simple counter) in textfield, but it doesn't work with "control set text"...

something I am missing here and doing wrong, I can imagine.
any help would be nice.

I am working at school since six month fulltime (teach foreign children at the afternoon with german language problems and give some edv/it-teaching hours for older teacher!), so my time isn't very big enough for programming at the moment. it's a pity ;)

best regards, frank

zak
01-04-2011, 11:56
Hi Frank
i havn't found the bug yet, it causes the program to hang.
but there is a
Canvas_Attach(cbhndl, %cCanvasDB, %true)
even you havn't created %cCanvasDB but %cCanvasSB:
CONTROL ADD CANVAS, hDlg, %cCanvasSB ...

Michael Clease
01-04-2011, 17:11
Hello Frank,

Did you realise that you call the timer every 10 milliseconds and inside that have a sleep for 250 so it is being called while its still sleeping.

if you want proof change your timer routine to add a messgebox


CASE %WM_TIMER
Dim tx, ty As Long
MsgBox 0, "close window"
Canvas_Attach(CBHNDL, %cCanvasSB, %FALSE)
DO NOT USE SLEEP AND TIMERS.

regards

Mike

Lionheart008
06-04-2011, 11:29
hi michael and zak, thanks for replies.

a) I've had more time to check the little loop example and found the problem. I wanted only to have a "pause" for the loop and perhaps a "stop" function. (see my example A. )

b) I have added three different approachs for counting numbers and canvas loops. perhaps somebody can check the last two example if that's correct or if there's another (better) way to stop a counter (I've taken stop command, but that's not perfect I am thinking).

example A)

' Empty GUI script created on 04-05-2011 16:54:03 by lionheart008 (ThinAIR) Uses "UI" Begin Const %cCanvasSB = %WM_USER + 500 %cCanvasDB %btnClose %btnPause %btnstop %tAnimationTimer End Const ' -- Create dialog here Function TBMain() Local hDlg As DWord Dim cx, cy As Long Dialog New 0, "Canvas numeric loops for fun :)",-1,-1, 300, 180, _ %WS_POPUP Or %WS_VISIBLE Or _ %WS_CLIPCHILDREN Or %WS_CAPTION Or _ %WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg Dialog Pixels hDlg, 200, 200 To Units cx, cy Control Add Canvas, hDlg, %cCanvasDB, "", cx-50, 25, cx,cy Control Add Button, hDlg, %btnstop, "stop proggy", cx-30, 30+cy, cx-50, 14 Control Add Button, hDlg, %btnClose, "Click to close", 60+cx, 30+cy, cx-50, 14, Call btnCloseProc Control Add Button, hDlg, %btnPause, "pause", cx-125, 30+cy, cx-50, 14 ', Call btnCloseProc Dialog Show Modal hDlg, Call dlgProc End Function ' -- Callback for dialog --------------------------------> CallBack Function dlgProc() Local pauseflag As Long Select Case CBMSG Case %WM_COMMAND If CBWPARAM = %btnPause Then PauseFlag = IIf(PauseFlag = %TRUE, %FALSE, %TRUE) End If If PauseFlag = %TRUE Then Sleep 750 DoEvents 'Iterate While End If If CBWPARAM = %btnStop Then MsgBox 0, "click ok-> stop now program and fine" Stop End If Case %WM_INITDIALOG Dialog Set Timer CBHNDL, %tAnimationTimer, 20, %NULL Case %WM_TIMER Canvas_Attach(CBHNDL, %cCanvasDB, %TRUE) DrawGraphics() Canvas_Redraw ' -- Needs this to sync Case %WM_CLOSE End Select End Function ' -- Callback for close button CallBack Function btnCloseProc() If CBMSG = %WM_COMMAND Then If CBCTLMSG = %BN_CLICKED Then Dialog End CBHNDL End If End If End Function '--------> loops and calculations -----------------------------------------> Sub DrawGraphics() Local lenx,leny,proport,zx,zy,i,j As Long Local a,b,c,d,zero, cnt, found As Long Local CmdLine,CmdLine1,CmdLine2,CmdLine3,CmdLine4,CmdLine0 As String Local rnda, rndb,rndc,rndd,rndzero As String cnt = 0 For a = 1 To 4 'Until found = %TRUE If a > 1 Then rnda = rnda + "VT" For b = 1 To 4 If b = 4 And a >= 1 Then rndb = rndb + "XA" ' 4 x 4 = 16 a x b For c = 1 To 4 ' 4 x 16 = 64 a x b x c If c = 4 And b = 1 Then rndc = "" If c = 4 And d = 5 Then rndc = rndc + "YB" For d = 1 To 4 ' 4 x 64 = 256 cnt = cnt +1 If d = 4 And c = 1 Then rndd = "" If d = 4 Then rndd = rndd + "ZC" For zero = 1 To 4 If zero = 4 And d = 4 And a>=4 Then rndzero = rndzero + "+" 'found = %TRUE CmdLine = "4. Loop : " + Format$(a) + " " + rnda CmdLine1 = "3. Loop : " & Format$(b) + " " + rndb CmdLine2 = "2. Loop : " & Format$(c) + " " + rndc CmdLine3 = "1. Loop : " & Format$(d) + " " + rndd CmdLine0 = "FunLoop : " & Format$(zero) + " " + rndzero CmdLine4 = "to 256 at: "+ Format$(cnt) If cnt = 257 Then Exit For Canvas_Color Rgb(128, 255, 0), Rgb(100, 0, 0) Canvas_Clear(Rgb(0,0,0)) Canvas_SetPos(25,12) Canvas_Printl "" Canvas_SetPos(25,20) Canvas_Printl " > " + CmdLine Canvas_SetPos(25,28) Canvas_Printl " > " + CmdLine1 Canvas_SetPos(25,36) Canvas_Printl " > " + CmdLine2 Canvas_SetPos(25,44) Canvas_Printl " > " + CmdLine3 Canvas_SetPos(25,52) Canvas_Printl " > " + CmdLine0 Canvas_SetPos(25,60) Canvas_Printl " > " + CmdLine4 Canvas_Box (zx+10, zy+10, zx+120, zy+100, 0, %BLUE) Canvas_Redraw Next Next Next Next Next End Sub '---------------------> end
B) counting until "21" canvas loops + counter example:



' Empty GUI script created on 04-05-2011 18:54:03 by lionheart008 (ThinAIR) Uses "UI", "console" Begin Const %cCanvasSB = %WM_USER + 500 %cCanvasDB %btnClose %btnPause %texty %btnstop %tAnimationTimer End Const ' 1) -- main dialog ------------------------------------------ Function TBMain() Local hDlg As DWord Dim cx, cy As Long Dialog New 0, "Canvas numeric loops + counter for fun :)",-1,-1, 300, 180, _ %WS_POPUP Or %WS_VISIBLE Or _ %WS_CLIPCHILDREN Or %WS_CAPTION Or _ %WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg Dialog Pixels hDlg, 200, 200 To Units cx, cy Control Add Canvas, hDlg, %cCanvasDB, "", cx-50, 25, cx,cy Control Add Textbox, hDlg, %texty, " ", 225,25,cx-45,cy-55 Control_SetText(hdlg, %texty,"still empty") 'Control Add Label, hDlg, %texty, " ", 225,25,cx-45,cy-55 Control Add Button, hDlg, %btnstop, "stop proggy", cx-35, 30+cy, cx-50, 14 Control Add Button, hDlg, %btnClose, "Click to close", 60+cx, 30+cy, cx-50, 14, Call btnCloseProc Control Add Button, hDlg, %btnPause, "pause", cx-125, 30+cy, cx-50, 14 ', Call btnCloseProc Dialog Show Modal hDlg, Call dlgProc End Function ' -- 2) Callback for dialog --------------------------------> CallBack Function dlgProc() Local pauseflag As Long Static counterx, counterz As Single Local hdlg As DWord Select Case CBMSG Case %WM_COMMAND If CBWPARAM = %btnPause Then PauseFlag = IIf(PauseFlag = %TRUE, %FALSE, %TRUE) End If If PauseFlag = %TRUE Then Sleep 750 DoEvents 'Iterate While End If If CBWPARAM = %btnStop Then MsgBox 0, "click ok-> stop now program and fine" Stop End If Case %WM_INITDIALOG Dialog Set Timer CBHNDL, %tAnimationTimer, 20, %NULL Case %WM_TIMER Canvas_Attach(CBHNDL, %cCanvasDB, %TRUE) DrawGraphics() '---------------------------------> counter idea 2 -----------------> If counterx >= 0 Then End If Do Incr Counterx Console_WriteLine("--> counting up to 21 ->" + Format$(Counterx-1, "#.00")) '+Str$(Counterx)) If Counterx <= 21 Then Exit Do'And Counterx >= 22 Then Exit Do Stop Loop Control_SetText(CBHNDL, %texty,"counter slowly.."+Str$(counterx)) 'Str$(counterx)) '---------------------------------> counter idea 2 end -----------------> Canvas_Redraw Case %WM_CLOSE End Select End Function ' -- 3) Callback for close button --------------------------------------> CallBack Function btnCloseProc() If CBMSG = %WM_COMMAND Then If CBCTLMSG = %BN_CLICKED Then Dialog End CBHNDL End If End If End Function ' 4)--------> loops and calculations -----------------------------------------> Sub DrawGraphics() Local hdlg As DWord Local lenx,leny,proport,zx,zy,i,j As Long Local a,b,c,d,zero, cnt, found As Long Local CmdLine,CmdLine1,CmdLine2,CmdLine3,CmdLine4,CmdLine0 As String Local rnda, rndb,rndc,rndd,rndzero As String cnt = 0 For a = 1 To 4 'Until found = %TRUE If a > 1 Then rnda = rnda + "VT" For b = 1 To 4 If b = 4 And a >= 1 Then rndb = rndb + "XA" ' 4 x 4 = 16 a x b For c = 1 To 4 ' 4 x 16 = 64 a x b x c If c = 4 And b = 1 Then rndc = "" If c = 4 And d = 5 Then rndc = rndc + "YB" For d = 1 To 4 ' 4 x 64 = 256 cnt = cnt +1 If d = 4 And c = 1 Then rndd = "" If d = 4 Then rndd = rndd + "ZC" For zero = 1 To 4 If zero = 4 And d = 4 And a>=4 Then rndzero = rndzero + "+" 'found = %TRUE CmdLine = "4. Loop : " + Format$(a) + " " + rnda CmdLine1 = "3. Loop : " & Format$(b) + " " + rndb CmdLine2 = "2. Loop : " & Format$(c) + " " + rndc CmdLine3 = "1. Loop : " & Format$(d) + " " + rndd CmdLine0 = "FunLoop : " & Format$(zero) + " " + rndzero CmdLine4 = "to 256 at: "+ Format$(cnt) If cnt = 257 Then Exit For Canvas_Color Rgb(128, 255, 0), Rgb(100, 0, 0) Canvas_Clear(Rgb(0,0,0)) Canvas_SetPos(25,12) Canvas_Printl "" Canvas_SetPos(25,20) Canvas_Printl " > " + CmdLine Canvas_SetPos(25,28) Canvas_Printl " > " + CmdLine1 Canvas_SetPos(25,36) Canvas_Printl " > " + CmdLine2 Canvas_SetPos(25,44) Canvas_Printl " > " + CmdLine3 Canvas_SetPos(25,52) Canvas_Printl " > " + CmdLine0 Canvas_SetPos(25,60) Canvas_Printl " " Canvas_SetPos(25,72) Canvas_Printl " > " + CmdLine4 Canvas_Box (zx+10, zy+10, zx+120, zy+100, 0, %BLUE) Canvas_Redraw Next Next Next Next Next End Sub ' 5) ---------------------> counter idea one --------------> Sub counters() Static counterx As Single Local hdlg As DWord counterx = 0 Do Incr Counterx Console_WriteLine("--> counter :"+Str$(Counterx)) If Counterx > 200 Then Exit Do 'PrintL "exit loop" SendMessage hdlg,%EM_GETSEL, %texty,VarPtr(counterx) Control_SetText(hDlg, %texty,Str$(counterx)) Loop End Sub '---------------------> counter idea -------------->


-> only a test for checking code tags


' Empty GUI script created on 04-05-2011 18:54:03 by lionheart008 (ThinAIR)

Uses "UI", "console"

Begin Const
%cCanvasSB = %WM_USER + 500
%cCanvasDB
%btnClose
%btnPause
%texty
%btnstop
%tAnimationTimer
End Const

' 1) -- main dialog ------------------------------------------
Function TBMain()
Local hDlg As DWord
Dim cx, cy As Long

Dialog New 0, "Canvas numeric loops + counter for fun :)",-1,-1, 300, 180, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION Or _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg

Dialog Pixels hDlg, 200, 200 To Units cx, cy
Control Add Canvas, hDlg, %cCanvasDB, "", cx-50, 25, cx,cy
Control Add Textbox, hDlg, %texty, " ", 225,25,cx-45,cy-55
Control_SetText(hdlg, %texty,"still empty")
'Control Add Label, hDlg, %texty, " ", 225,25,cx-45,cy-55
Control Add Button, hDlg, %btnstop, "stop proggy", cx-35, 30+cy, cx-50, 14
Control Add Button, hDlg, %btnClose, "Click to close", 60+cx, 30+cy, cx-50, 14, Call btnCloseProc
Control Add Button, hDlg, %btnPause, "pause", cx-125, 30+cy, cx-50, 14 ', Call btnCloseProc

Dialog Show Modal hDlg, Call dlgProc
End Function

' -- 2) Callback for dialog -------------------------------->
CallBack Function dlgProc()
Local pauseflag As Long
Static counterx, counterz As Single
Local hdlg As DWord

Select Case CBMSG
Case %WM_COMMAND
If CBWPARAM = %btnPause Then
PauseFlag = IIf(PauseFlag = %TRUE, %FALSE, %TRUE)
End If

If PauseFlag = %TRUE Then
Sleep 750
DoEvents
'Iterate While
End If

If CBWPARAM = %btnStop Then
MsgBox 0, "click ok-> stop now program and fine"
Stop
End If

Case %WM_INITDIALOG
Dialog Set Timer CBHNDL, %tAnimationTimer, 20, %NULL


Case %WM_TIMER
Canvas_Attach(CBHNDL, %cCanvasDB, %TRUE)
DrawGraphics()

'---------------------------------> counter idea 2 ----------------->

If counterx >= 0 Then
End If


Do
Incr Counterx
Console_WriteLine("--> counting up to 21 ->" + Format$(Counterx-1, "#.00")) '+Str$(Counterx))
If Counterx <= 21 Then Exit Do'And Counterx >= 22 Then Exit Do
Stop
Loop

Control_SetText(CBHNDL, %texty,"counter slowly.."+Str$(counterx)) 'Str$(counterx))

'---------------------------------> counter idea 2 end ----------------->

Canvas_Redraw


Case %WM_CLOSE
End Select

End Function

' -- 3) Callback for close button -------------------------------------->
CallBack Function btnCloseProc()

If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
Dialog End CBHNDL
End If
End If

End Function

' 4)--------> loops and calculations ----------------------------------------->
Sub DrawGraphics()
Local hdlg As DWord
Local lenx,leny,proport,zx,zy,i,j As Long
Local a,b,c,d,zero, cnt, found As Long
Local CmdLine,CmdLine1,CmdLine2,CmdLine3,CmdLine4,CmdLine0 As String
Local rnda, rndb,rndc,rndd,rndzero As String

cnt = 0
For a = 1 To 4 'Until found = %TRUE
If a > 1 Then rnda = rnda + "VT"
For b = 1 To 4
If b = 4 And a >= 1 Then rndb = rndb + "XA" ' 4 x 4 = 16 a x b
For c = 1 To 4 ' 4 x 16 = 64 a x b x c
If c = 4 And b = 1 Then rndc = ""
If c = 4 And d = 5 Then rndc = rndc + "YB"
For d = 1 To 4 ' 4 x 64 = 256
cnt = cnt +1
If d = 4 And c = 1 Then rndd = ""
If d = 4 Then rndd = rndd + "ZC"
For zero = 1 To 4
If zero = 4 And d = 4 And a>=4 Then rndzero = rndzero + "+"
'found = %TRUE

CmdLine = "4. Loop : " + Format$(a) + " " + rnda
CmdLine1 = "3. Loop : " & Format$(b) + " " + rndb
CmdLine2 = "2. Loop : " & Format$(c) + " " + rndc
CmdLine3 = "1. Loop : " & Format$(d) + " " + rndd
CmdLine0 = "FunLoop : " & Format$(zero) + " " + rndzero
CmdLine4 = "to 256 at: "+ Format$(cnt)

If cnt = 257 Then Exit For

Canvas_Color Rgb(128, 255, 0), Rgb(100, 0, 0)
Canvas_Clear(Rgb(0,0,0))
Canvas_SetPos(25,12)
Canvas_Printl ""
Canvas_SetPos(25,20)
Canvas_Printl " > " + CmdLine
Canvas_SetPos(25,28)
Canvas_Printl " > " + CmdLine1
Canvas_SetPos(25,36)
Canvas_Printl " > " + CmdLine2
Canvas_SetPos(25,44)
Canvas_Printl " > " + CmdLine3
Canvas_SetPos(25,52)
Canvas_Printl " > " + CmdLine0
Canvas_SetPos(25,60)
Canvas_Printl " "
Canvas_SetPos(25,72)
Canvas_Printl " > " + CmdLine4
Canvas_Box (zx+10, zy+10, zx+120, zy+100, 0, %BLUE)
Canvas_Redraw

Next
Next
Next
Next
Next

End Sub

' 5) ---------------------> counter idea one -------------->
Sub counters()

Static counterx As Single
Local hdlg As DWord

counterx = 0
Do
Incr Counterx
Console_WriteLine("--> counter :"+Str$(Counterx))
If Counterx > 200 Then Exit Do
'PrintL "exit loop"
SendMessage hdlg,%EM_GETSEL, %texty,VarPtr(counterx)
Control_SetText(hDlg, %texty,Str$(counterx))
Loop
End Sub
'---------------------> counter idea --------------> I add all example in zip folder. thanks, nice day, best regards, franko :)

Michael Clease
06-04-2011, 15:02
Hello Frank,

I am not sure what your code is trying to do so I wrote a simple version using multiple timers to show how easy it was, also a sleep routine which might help.


Sub mySleep(CBHNDL As Long, TimerID As Long, SleepTime As Long, TimerPeriod As Long)

Dialog Kill Timer CBHNDL, TimerID
Sleep SleepTime
Dialog Set Timer CBHNDL, TimerID, TimerPeriod

End Sub