【发布时间】:2014-08-28 17:31:16
【问题描述】:
我需要在 WinForms 上绘制透明的 PNG 图像。我有一个基类:
public abstract class BaseSkinable : Panel
{
protected BaseSkinable()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
protected abstract void OnDraw(Graphics graphics);
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e) { }
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
OnDraw(e.Graphics);
}
}
在继承类中:
protected override void OnDraw(Graphics graphics)
{
Image toDraw = SelectImageToDraw();
if (toDraw == null)
{
NoImageDraw(graphics);
return;
}
int width = toDraw.Size.Width;
int height = toDraw.Size.Height;
Rectangle rect = new Rectangle(0, 0, width, height);
graphics.DrawImage(toDraw, rect);
}
如果用户将鼠标移到控件上,我需要重绘图像。但问题是反对绘制覆盖旧图像。就像我们绘制图层一样。 Winforms 可能无法清除图形,而我的方法会覆盖旧图片。如何解决它,我可能做错了什么。
【问题讨论】:
-
那么,你想在你的面板控件中绘制一个 png 图像吗?
-
我有一个透明的 PNG 图像。我需要制作一个“ImageButton” - 控制鼠标进入、离开和单击时重绘图像的内容。所以问题是这个 ImageButton 需要透明并且没有边框。
-
您想在 Button 上绘制透明图像。按钮后面是
form?form也是透明的吗? -
我解决了这个问题。这不是微不足道的。如果我戴上表单用户控件(包含图像)并戴上我的 ImageButton 错误存在。如果我只放置图像并放置 ImageButton 它会正确绘制。我不知道为什么。