Results 1 to 14 of 14

Thread: Painting issue, when to draw/erase snap lines

Threaded View

  1. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Painting issue, when to draw/erase snap lines

    Hi Nick,

    Yesterday I was puzzled why you were having problems simply invalidating the whole form in the MouseMove sub. After all, you see GDI+ demos with hundreds of shapes flitting around on a (maximized) form and they successfully refresh the form quickly enough to do it in time with an animation clock. So why should dragging a control or shape be different?

    Looking at your Canvas.cs code again, I see that you are using Refresh and Update in several places. I think that is a mistake.

    The reason is that Invalidate just involves putting some numbers on a queue and takes a few microseconds. The corresponding pixels will be not be redrawn until the processor has some idle time, and the then area refreshed will be the union of all the invalidated regions.

    Refresh on the other hand takes several milliseconds. What is more it has to happen straight away, before the next line of code is executed. So it must surely be a mistake to loop through a list of shapes with This.Refresh in each pass. I am even doubtful about putting Update in the Shape move handler.

    Here's a little demo of the difference:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim sw As New Stopwatch
            sw.Start()
            For i As Integer = 0 To 100
                Me.Invalidate()
            Next
            MessageBox.Show("Invalidate: " & (sw.ElapsedTicks / Stopwatch.Frequency * 1000).ToString("0.000") & " milliseconds")
            sw = Stopwatch.StartNew
            For i As Integer = 0 To 100
                Me.Refresh()
            Next
            MessageBox.Show("Refresh: " & (sw.ElapsedTicks / Stopwatch.Frequency * 1000).ToString("0.000") & " milliseconds")
        End Sub
    (I've used a 100x loop to eliminate the Stopwatch overhead which I believe is a couple of microseconds.) For me, Refreshing appears to take about 1,500 times as long as Invalidating.

    So I wonder how your app would behave if you just deleted all the lines with Refresh or Update, and put This.Invalidate at the end of the MouseMove sub. I suspect it will work smoothly and you will not have to worry about invalidating the snap lines. I'm not a fluent Cs reader and not having a working project makes it hard to try it out, so I may be wrong about this.

    BB
    Last edited by boops boops; Oct 7th, 2010 at 04:29 AM. Reason: correction to Invalidate line in code

Posting Permissions

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



Click Here to Expand Forum to Full Width