双缓冲非矩形分配
我在 Windows.Forms
中的面板上绘图。
当我像这里一样使用双缓冲时,我只能分配矩形区域。
当我绘制圆形或椭圆形时,剩余空间填充为黑色。
请帮助我找出我做错了什么或如何解决这个问题。
提前致谢! :)
我尝试 this.DoubleBuffered = true
来防止闪烁。这是没有用的。
Var p
是在其上绘制内容的面板。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Dr(bool variant)
{
BufferedGraphicsContext currentContext;
BufferedGraphics myBuffer;
currentContext = BufferedGraphicsManager.Current;
myBuffer = currentContext.Allocate(panel1.CreateGraphics(), panel1.DisplayRectangle);
LinearGradientBrush lgb;
if (variant) lgb = new LinearGradientBrush(new Point(0, 0), new Point(500, 500), Color.Red, Color.DarkRed);
else lgb = new LinearGradientBrush(new Point(0, 0), new Point(500, 500), Color.DarkRed, Color.Red);
myBuffer.Graphics.FillEllipse(lgb, 0, 0, 300, 300);
myBuffer.Graphics.DrawString("Lorem ipsum", new Font("calibri", 40), Brushes.White, new PointF(50, 50));
myBuffer.Render();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Dr(true);
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
Dr(false);
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
Dr(true);
}
}
I draw on Panel in Windows.Forms
.
When I use double buffering like here, I can allocate only rectangular area.
And when I draw circle or ellipse remaining space is filled with black color.
Please, help me figure out what I am doing wrong or how to cope with this problem.
Thanks in advance! :)
I tried this.DoubleBuffered = true
to prevent from flicker. It is no use.
Var p
is Panel to draw something on it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Dr(bool variant)
{
BufferedGraphicsContext currentContext;
BufferedGraphics myBuffer;
currentContext = BufferedGraphicsManager.Current;
myBuffer = currentContext.Allocate(panel1.CreateGraphics(), panel1.DisplayRectangle);
LinearGradientBrush lgb;
if (variant) lgb = new LinearGradientBrush(new Point(0, 0), new Point(500, 500), Color.Red, Color.DarkRed);
else lgb = new LinearGradientBrush(new Point(0, 0), new Point(500, 500), Color.DarkRed, Color.Red);
myBuffer.Graphics.FillEllipse(lgb, 0, 0, 300, 300);
myBuffer.Graphics.DrawString("Lorem ipsum", new Font("calibri", 40), Brushes.White, new PointF(50, 50));
myBuffer.Render();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Dr(true);
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
Dr(false);
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
Dr(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不评论您正在尝试做什么或您可能或可能不会做错的事情,基本问题不是闪烁。双缓冲不会有帮助。您忽略了填充添加到缓冲区中但未在其上绘制的面板的任何部分。添加代码,例如:
这将以面板原始背景色填充整个矩形。
但是,这将解决您眼前的问题,但它仍然可能无法按您想要的方式工作。尝试在表单上移动一个窗口。一旦面板因任何原因必须重新绘制,您绘制的内容就会消失。要制作一个带有持久绘图的面板,您必须创建自己的控件,从Panel 继承,并在控件的OnPaint 方法中实现自定义绘图。
C# Winforms 控件并不是真正透明的,因此如果您想要透明背景,则必须创建自己的控件。该技术的描述如下:
https://web. archive.org/web/20141227200000/http://bobpowell.net/transcontrols.aspx
Without commenting on what you are trying to do or what you may or may not potentially be doing wrong here, the basic problem is not flicker. Double buffering will not help. You have neglected to fill in any part of the panel you added to the buffer that you did not draw on. Add code such as:
This will fill the entire rect with the panels original back color.
However, that will fix your immediate issue but it still may not work like you want. Try moving a window over your form. As soon as the panel has to repaint itself for any reason, what you have drawn is gone. To make a panel with a persistent drawing on it you will have to create your own control, inherit from a Panel, and implement the custom drawing in the control's OnPaint method.
C# Winforms controls are not truly transparent, so if you want a transparent background you have to create your own control. The technique is described here:
https://web.archive.org/web/20141227200000/http://bobpowell.net/transcontrols.aspx