Results 1 to 5 of 5

Thread: canvas loops and question

  1. #1
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    44
    Posts
    914
    Rep Power
    101

    canvas loops and question

    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	loops-counting-canvas1.png 
Views:	79 
Size:	9.3 KB 
ID:	7131  
    Attached Files Attached Files
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  2. #2
    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 ...
    Last edited by zak; 01-04-2011 at 13:04.

  3. #3
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Blog Entries
    1
    Rep Power
    153

    Thumbs down

    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
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  4. #4
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    44
    Posts
    914
    Rep Power
    101

    canvas loops + counter

    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,2
    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	counter-canvas-loops2.jpg 
Views:	60 
Size:	51.0 KB 
ID:	7138  
    Attached Files Attached Files
    Last edited by Lionheart008; 06-04-2011 at 11:39.
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  5. #5
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Blog Entries
    1
    Rep Power
    153

    Post

    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
    
    Attached Files Attached Files
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

Similar Threads

  1. Replies: 6
    Last Post: 16-06-2010, 06:40
  2. Loops
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 18-03-2009, 08:58

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •