【问题标题】:How to make my panel stop flickering如何让我的面板停止闪烁
【发布时间】:2015-12-24 07:29:36
【问题描述】:

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

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; 但它只是让它看起来像当我移动鼠标时有面板的后图像

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

【问题讨论】:

  • 我假设每次移动鼠标时面板都会重新绘制。也许只使用MouseHover 事件(没有MouseMove)并设置MousHoverTime 以查看它是否正常工作。
  • 对不起,我将操作从悬停更改为输入,我只是重用代码
  • 我是这么认为的。请编辑问题!

标签: c# panel windows-forms-designer


【解决方案1】:

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

所以使用 doubleBuffered Panel 子类

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

但是,移动大件物品会造成损失。 顺便说一句,PictureBox 类已经是双缓冲的。此外,将面板添加到图片框而不是表单似乎更合乎逻辑:pictureBox1.Controls.Add(pan); 并将pictureBox1.Refresh(); 添加到MouseMove

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

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

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
}

看起来您缺少正确的 doublebuffering Formrefreshing 组合,FormPictureBox

【讨论】:

  • 不,移动鼠标时面板仍然闪烁,即使它发生的次数较少
  • 实际上我按原样尝试了您的代码,发现 no 闪烁。图片有多大?
  • 宽是209高是319
  • 即使在鼠标不动的情况下仍然有闪烁,但它只是闪烁几次,这是一个巨大的改进
  • 将面板添加到picturebox1甚至不显示面板,因为它在picturebox1显示区域内被困
猜你喜欢
  • 2014-11-25
  • 2013-05-23
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
相关资源
最近更新 更多