locked
laggy graphics

    Question

  • i have a form which is all drawn in the paint event of it. (bg, buttons, etc... everything is drawn, instead of using controls - everything is skinned)

    by mouse move and mouse click i invalidate parts of the form. well, the appearance of the form is very laggy. what could i do with this? i use the me.suspendlayout and me.resumelayout but its still laggy.

    Monday, 24 March, 2008 12:01 AM

All replies

  • Hi Attila,

    Could you post your code snippet about this scenario? It sounds like that main UI thread is executing some code snippet and it doesn't handle Paint event in time. We need to see your code to know what happens behind the scene.

    Best regards,
    Riquel

    Thursday, 27 March, 2008 6:22 AM
  • course:

     

    Code Snippet

    Private Sub menu_form_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Me.SuspendLayout()
            e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\background.png", True), 0, 0, Me.Width, Me.Height)
            If Me.user_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 52, 135, 34)
            End If
            If Me.doc_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 85, 135, 34)
            End If
            If Me.kepek_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 119, 135, 34)
            End If
            If Me.music_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 151, 135, 34)
            End If
            If Me.games_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 184, 135, 34)
            End If
            If Me.rec_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 225, 135, 34)
            End If
            If Me.comp_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 258, 135, 34)
            End If
            If Me.net_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 291, 135, 34)
            End If
            If Me.connect_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 324, 135, 34)
            End If
            If Me.ctrl_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 372, 135, 34)
            End If
            If Me.printers_hovered = True Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 405, 135, 34)
            End If
            If Me.help_hovered = True And Me.allprogfunction <> "search" Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 440, 135, 34)
            End If
            If Me.run_hovered = True And Me.issearching = False Then
                e.Graphics.DrawImage(Image.FromFile(Me.skinpath & "\proghover.png"), 265, 472, 135, 34)
            End If
            e.Graphics.DrawString(username, Me.Font, Brushes.White, 270, 60)

     

     

    this is most part of the code. the hovered variable are boolean variables and change the value according to mouse movements. if the mouse is over a part of the form i change the hovered to true and invalidate that part in the mousemove event of the form. if the mouse leave the area i set the hovered variable for false and invalidate the area again.

     

    this is fast but when i add a flowlayoutpanel and items to that when the form is loading it shows the main form with the flowlayoutpanel but instead of the items on it it shows a transparent background first and then adds the controls. it's like the graphics are stuck.

    Thursday, 27 March, 2008 6:46 AM
  • Hi Attila,

    Is your form programmatically adding and removing child controls or perform dynamic layout in Form's Paint event? Commonly if you programmatically add and remove child controls or perform dynamic layout to the Form, you should call the SuspendLayout and ResumeLayout methods you should, but it is not in the Paint event.

    Also note that SuppendLayout will influence any redrawing of the Form. You shouldn't use this method in your Paint event handler. I am not sure how you use Flowlayoutpanel control in this scenario. Could you give me the more information about this scenario? Also read Practical Tips For Boosting The Performance Of Windows Forms Apps for more general guideline to boost Painting performance. If your Form's your Paint event handler isn't a lightweight procedure, it will have a bad performance.

    Best regards,
    Riquel

    Thursday, 27 March, 2008 8:45 AM
  • okay, i just tried the suspendlayout method, since i thought it will suspend the layout of the form and then painted the "controls" and resumed the layout.

     

    i mean, those are not real controls just painted texts and images and i check whether the mouse is over them or not and repaint the specified region of the form according to a boolean variable, example:

     

    Code Snippet

    dim mousehovered as boolean=false

    dim hoveredimage as image=image.fromfile("C:\hovered.png")

     

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

    if e.x>0 and e.x<10 and e.y>0 and e.y>10 then

    me.mousehovered=true

    else

    me.mousehovered=false

    end if

    me.invalidate(new rectangle(0,0,10,10)

    end sub

     

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    if me.mousehovered=true then

    e.graphics.drawimage(hoveredimage, 0,0,10,10)

    end if

    e.graphics.drawstring("Hover me",me.font, brushes.black, 0,0)

    End Sub

     

     

    is it clear now? i can't explain this better. and i need to use the paint event since im working with alpha blended png images and i can't put to pictureboxes onto eachother using alpha transparency.

     

    And when the user opens the form, then i should read a few items from a file and then creating a new usercontrol containing those items written onto usercontrols (they are like buttons) and then add those usercontrols to my flowlayoutpanel.

     

    anyways, thnx for your reply i'll read through that page you linked me... it looks interesting.

    Thursday, 3 April, 2008 11:32 PM