嗨我正在尝试制作一个面板,当它悬停在图片上时会显示一些文字,我希望它跟随光标,所以我
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;
但它只是使我看起来像我移动鼠标后面板的图像
答案 0 :(得分:2)
将this.DoubleDuffered = true;
添加到Form
只会影响Form
,而不影响Panel
。
因此请使用doubleBuffered Panel
子类:
class DrawPanel : Panel
{
public DrawPanel ()
{
this.DoubleBuffered = true;
}
}
然而,移动大件物品会造成损失。
顺便说一句,PictureBox
类已经是doubleBuffered。将Panel添加到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 Form
和刷新的正确组合,Form
和PictureBox