如何让我的面板停止闪烁

本文关键字:闪烁 我的 | 更新日期: 2023-09-27 18:27:53

嗨,我正在尝试制作一个面板,当它悬停在图片上时会显示一些文本,我希望它跟随光标,所以我

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);  
    }

我尝试添加this.doublebuffered = true;但它只是让它看起来像是当我移动鼠标时面板的后图像

当我将鼠标悬停在我的图片上时,它会显示面板,但它疯狂地闪烁这是正常的还是有解决此问题的方法,或者这是我的电脑问题

如何让我的面板停止闪烁

this.DoubleDuffered = true;添加到Form中只会影响Form,而不会影响Panel

因此,请使用双缓冲Panel子类

class DrawPanel : Panel
{
    public DrawPanel ()
    {
      this.DoubleBuffered = true;
    }
}

然而,移动大东西会付出代价。顺便说一句,PictureBox类已经是双缓冲的。此外,将面板添加到图片框而不是表单似乎更合乎逻辑:pictureBox1.Controls.Add(pan);并向MouseMove添加pictureBox1.Refresh();

更新:由于您没有在面板上绘制,并且还需要它与PictureBox重叠,因此上述想法并不真正适用;使用子类不是必需的,尽管它在某些时候可能会派上用场。是的,面板需要添加到窗体的控件集合中!

这段代码在这里工作得很好:

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
}

看起来您错过了双重缓冲Form刷新FormPictureBox的正确组合。