C# 双缓冲在面板上不起作用?
我正在做一个类似ms paint的小绘画程序。目前,我正在尝试实现“选择函数”。我面临着闪烁问题,所以我做了一些研究,我发现,我应该创建自己的面板类C# 双缓冲在面板上不起作用?,c#,graphics,doublebuffered,C#,Graphics,Doublebuffered,我正在做一个类似ms paint的小绘画程序。目前,我正在尝试实现“选择函数”。我面临着闪烁问题,所以我做了一些研究,我发现,我应该创建自己的面板类 public class MyDisplay : Panel { public MyDisplay() { this.DoubleBuffered = true; this.SetStyle(ControlStyles.UserP
public class MyDisplay : Panel
{
public MyDisplay()
{
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.ContainerControl |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.SupportsTransparentBackColor
, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.UpdateStyles();
}
}
主窗体中有以下字段:
MyDisplay panel1 = new MyDisplay();
Graphics graphics1 = panel1.CreateGraphics();
我在一个面板上使用3个事件:
MouseDown-我到了p1点
MouseMove-这就是我遇到闪烁问题的地方,我正在打电话
图形1.绘图矩形。。。和图形1.每次单击鼠标移动时清除
我最后一次画矩形。
怎么了?为什么我仍然面临闪烁的问题,即使整个面板是白色的,只有一个矩形在那里?多谢各位
编辑:
我已经重写了画法,但我仍然不知道下一步该怎么办
protected override void OnPaint(PaintEventArgs e)
{
// Call the OnPaint method of the base class.
base.OnPaint(e);
// Call methods of the System.Drawing.Graphics object.
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle);
}
编辑2:
我是否应该在位图/图像上绘制,并覆盖OnPaint方法以从那里复制图像并将其粘贴到面板?删除定义graphics1字段的行 使用随PaintEventArgs对象传入的图形对象,在OnPaint的覆盖中执行所有绘制。使用方法Invalidate、Refresh和Update控制从其他代码重新绘制的时间
如果在此设计中遇到任何特定困难,请回调。您正在使用CreateGraphics破坏双缓冲功能。你必须重写OnPaint方法来进行绘制并避免闪烁。你介意给我一些关于使用OnPaint的提示吗?我不知道该怎么做,我一直试图在谷歌上找到它,但没有结果。你能给我举个例子吗?我不确定它应该是什么样子,如果我想画矩形和圆形,我应该把其中的两种方法放在OnPaint中吗?我应该在位图/图像上绘制并覆盖OnPaint方法来复制图像并将其粘贴到面板上吗?在位图上绘制,以及在屏幕上绘制位图,通常都是通过OnPaint方法完成的。