avoid flicker for panel control upon cursor mousemove

ridhwaans

Active member
Joined
Jun 1, 2012
Messages
34
Programming Experience
3-5
VB.NET:
Imports System.Drawing.Drawing2D

Public Class Form1
    Private myPath As New GraphicsPath

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If myPath.IsVisible(e.X, e.Y) = True Then

            Panel1.Visible = True
            Panel1.Location = New Point(e.X, e.Y - Panel1.Height)
        Else
            Panel1.Visible = False
        End If

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        myPath.AddRectangle(New Rectangle(457, 428, 602 - 457, 472 - 428))
        Dim myPen As New Pen(Color.Black, 1)
        e.Graphics.DrawPath(myPen, myPath)
    End Sub
End Class

I've got a picturebox in a wpf, in which I've drawn a rectangle (and polygons) using graphics path. If the user mouses over the rectangle area, determined by the isVisible property, a panel pops up (like a tooltip) and displayed, and its location moved along the cursor position while the cursor is within the area. The panel has got a bunch of labels as attributes
I've used panel visible property to achieve this, but as the panel moves with the cursor, there is a lot of flickering. Is there any way to avoid flickering? I've tried using doublebuffered and setstyles
Please advise
thanks
 
Not within the program as it stands. You're asking for a complete repaint every time the cursor moves so much as a pixel's width. This might be OK with a superfast graphics card but for the average display flicker is almost inevitable. It certainly doesn't help that you're checking the myPath visible property within the MouseMove sub in addition to all the other happenings.
 
Not within the program as it stands. You're asking for a complete repaint every time the cursor moves so much as a pixel's width. This might be OK with a superfast graphics card but for the average display flicker is almost inevitable. It certainly doesn't help that you're checking the myPath visible property within the MouseMove sub in addition to all the other happenings.

yeah I know,
do you know of an alternative, to suggest if any?
 
Couldn't the panel be static? Does it really have to move with the cursor? Take a look at the status bar on MSPaint for example. People are more than used to looking away from the cursor to get information.
 
yeah I know,
do you know of an alternative, to suggest if any?

The reason the panel is flickering is because you are adding a new rectangle to the graphics path every time the Paint event fires. That happens every time the panel moves over the picture box. Within a few seconds you may have added hundreds of rectangles to the path, even if they are all the same dimensions. That makes it really slow for myPath.IsVisible to do its work, so that there is a delay every time you try to show the panel. Answer: move myPath.AddRectangle to the form's Load or Shown event sub.


Vic
 
Back
Top