Reputation: 563
Hi i am trying to make a panel that will show some text when it hovered over a picture and i want it to follow the cursor so i
System.Windows.Forms.Panel pan = new System.Windows.Forms.Panel();
public Form1()
{
InitializeComponent();
Product p = new Product();
p.SetValues();
this.pictureBox1.Image = (Image)Properties.Resources.ResourceManager.GetObject("pictureName");
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pan.Height = 200;
pan.Width = 100;
pan.BackColor = Color.Blue;
this.Controls.Add(pan);
pan.BringToFront();
//pan.Location = PointToClient(Cursor.Position);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Controls.Remove(pan);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pan.Location = PointToClient(Cursor.Position);
}
I tried adding this.doublebuffered = true;
but it just make it look like there are after image of the panel when i move my mouse
when i hover over my picture it shows the panel but it flickers like crazy is this normal or is there a fix for this or this is my computers problem
Upvotes: 0
Views: 4927
Reputation: 54453
Adding this.DoubleDuffered = true;
to the Form
does only affect the Form
, not the Panel
.
So use a doubleBuffered Panel
subclass:
class DrawPanel : Panel
{
public DrawPanel ()
{
this.DoubleBuffered = true;
}
}
However moving large things around will take its toll.
Btw, the PictureBox
class is already doubleBuffered. Also it seems more logical to add the Panel to the PictureBox, not the Form: pictureBox1.Controls.Add(pan);
And adding a pictureBox1.Refresh();
to the MouseMove
.
Update: As you don't draw on the Panel and also need to it to overlap the PictureBox the above ideas do not really apply; using the subclass is not necessary, although it may come handy at some point. And yes, the Panel needs to be added to the Form's Controls collection!
This code works just fine here:
public Form1()
{
InitializeComponent();
// your other init code here
Controls.Add(pan); // add only once
pan.BringToFront();
pan.Hide(); // and hide or show
this.DoubleDuffered = true // !!
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pan.Hide(); // hide or show
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pan.Location = pictureBox1.PointToClient(Cursor.Position);
pictureBox1.Refresh(); // !!
Refresh(); // !!
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pan.Height = 200;
pan.Width = 100;
pan.BackColor = Color.Blue;
pan.Location = pictureBox1.PointToClient(Cursor.Position);
pan.Show(); // and hide or show
}
Looks like you were missing the right combination of doublebuffering the Form
and refreshing both, the Form
and the PictureBox
.
Upvotes: 3