【发布时间】:2016-03-31 16:57:40
【问题描述】:
您好,我有一个 c# 用户控件,我覆盖了 OnPaint 方法。
在我的 OnPaint 方法中,我有以下代码:
// Draw the new button.
protected override void OnPaint(PaintEventArgs e) {
Color btnColor = this.ButtonColor;
if (!this.ButtonEnabled) {
btnColor = Color.LightGray;
}
Bitmap GraphicsImage = new Bitmap(24, 24, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics.FromImage(GraphicsImage).Clear(btnColor);
Graphics graphics = e.Graphics;
SolidBrush myBrush = new SolidBrush(btnColor);
Pen myPen = new Pen(btnColor);
// Draw the button in the form of a circle
graphics.DrawEllipse(myPen, 0, 0, 40, 40);
graphics.FillEllipse(myBrush, new Rectangle(0, 0, 40, 40));
if (!DesignMode) {
Image iconImg = null;
switch (this.ButtonImage) {
case CircleButtonImage.ArrowDown:
iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowdown_16.png");
break;
case CircleButtonImage.ArrowUp:
iconImg = POS.Framework.Utility.Common.GetResourceImage("arrowup_16.png");
break;
case CircleButtonImage.Cross:
iconImg = POS.Framework.Utility.Common.GetResourceImage("close_16.png");
break;
case CircleButtonImage.Plus:
iconImg = POS.Framework.Utility.Common.GetResourceImage("plus_16.png");
break;
}
graphics = Graphics.FromImage(GraphicsImage);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.DrawImage(iconImg, 5, 5);
this.CreateGraphics().DrawImageUnscaled(GraphicsImage, new Point(7, 6));
}
myBrush.Dispose();
myPen.Dispose();
}
这很好用,但为了避免闪烁,我添加到我的构造函数中:
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
然后png 图像不再显示。
没有双缓冲:
双缓冲:
有关如何解决此问题的任何线索。我想避免闪烁,但需要渲染图像。
【问题讨论】:
-
您必须改用
e.Graphics。