绘制透明按钮
我正在尝试在 C# (.NET 3.5 SP1) 中创建一个透明按钮以在我的 WinForms 应用程序中使用。 我已尝试一切使按钮透明(它应该显示按钮下方的渐变背景),但它不起作用。
下面是我正在使用的代码:
public class ImageButton : ButtonBase, IButtonControl
{
public ImageButton()
{
this.SetStyle(
ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.FillRectangle(Brushes.Transparent, this.ClientRectangle);
g.DrawRectangle(Pens.Black, this.ClientRectangle);
}
// rest of class here...
}
问题是该按钮似乎从某处获取随机 UI 内存,并用 Visual Studio UI 中的一些缓冲区填充自身(在设计模式下)。 在运行时,它会抓取一些归零的缓冲区并且完全是黑色的。
我的最终目标是在不可见的按钮而不是矩形上绘制图像。 然而,这个概念应该保持不变。 当用户将鼠标悬停在按钮上时,会绘制按钮类型的形状。
有任何想法吗?
编辑:谢谢大家,以下内容对我有用:
public class ImageButton : Control, IButtonControl
{
public ImageButton()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.DrawRectangle(Pens.Black, this.ClientRectangle);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// don't call the base class
//base.OnPaintBackground(pevent);
}
protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x20;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
// rest of class here...
}
I'm trying to create a transparent button in C# (.NET 3.5 SP1) to use in my WinForms application. I've tried everything to get the button to be transparent (it should show the gradient background underneath the button) but it's just not working.
Here is the code I'm using:
public class ImageButton : ButtonBase, IButtonControl
{
public ImageButton()
{
this.SetStyle(
ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.FillRectangle(Brushes.Transparent, this.ClientRectangle);
g.DrawRectangle(Pens.Black, this.ClientRectangle);
}
// rest of class here...
}
The problem is that the button seems to be grabbing random UI memory from somewhere and filling itself with some buffer from Visual Studio's UI (when in design mode). At runtime it's grabbing some zero'd buffer and is completely black.
My ultimate goal is to paint an image on an invisible button instead of the rectangle. The concept should stay the same however. When the user hovers over the button then a button-type shape is drawn.
Any ideas?
EDIT: Thanks everybody, the following worked for me:
public class ImageButton : Control, IButtonControl
{
public ImageButton()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.DrawRectangle(Pens.Black, this.ClientRectangle);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// don't call the base class
//base.OnPaintBackground(pevent);
}
protected override CreateParams CreateParams
{
get
{
const int WS_EX_TRANSPARENT = 0x20;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
// rest of class here...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
WinForms(和底层 User32)根本不支持透明度。 然而,WinForms 可以通过使用您提供的控件样式 - SupportsTransparentBackColor 来模拟透明度,但在这种情况下,“透明”控件所做的一切,它允许绘制父级其背景。
ButtonBase 使用一些阻止此机制工作的窗口样式。 我看到两种解决方案:一是从 Control(而不是 ButtonBase)派生控件,二是使用 Parent 的 DrawToBitmap 获取按钮下的背景,然后在 OnPaint 中绘制此图像。
WinForms (and underlying User32) does not support transparency at all. WinForms however can simulate transparency by using control style you provide - SupportsTransparentBackColor, but in this case all that "transparent" control does, it to allow drawing parent its background.
ButtonBase uses some windows styles that prevent working this mechanism. I see two solutions: one is to derive your control from Control (instead of ButtonBase), and second is to use Parent's DrawToBitmap to get background under your button, and then draw this image in OnPaint.
在 winforms 中,有一些技巧可以让控件在使用透明度时正确绘制背景。 您可以将此代码添加到 OnPaint 或 OnPaintBackground 以获取正在绘制的背景中的控件:
In winforms there are some tricks to allow a control having its background correctly painted when using transparency. You can add this code to the OnPaint or OnPaintBackground to get the controls you have in the background being painted:
我不确定 ButtonBase 支持透明度...你检查过吗?
我已经编写了许多透明控件,但我总是继承自 Control 或 UserControl。
当您想要阻止控件绘制其背景时 - 您应该重写 OnPaintBackground 而不是 OnPaint 并且不调用基类。
用 Brushes.Transparent 填充一个矩形很有趣 - 你正在用一种看不见的颜色在现有的东西上绘画。 或者换句话说:它什么也没做!
I'm not sure ButtonBase supports transparency... have you checked that out?
I've written a number of transparent controls, but I have always inherited from Control or UserControl.
When you want to block out a control painting it's background - you should override OnPaintBackground instead of OnPaint and not call the base class.
Filling a rectangle with Brushes.Transparent is funny though - you're painting with an invisible color over what's aready there. Or to put it another way: it does nothing!
我知道这个问题已经很老了,但是如果有人不想创建一个控件来执行此操作,我从另一篇文章中提出了此代码,并将其更改为扩展方法。
并且调用如下:
I know this question is old, but if someone doesn't want to create a control to do this I came up with this code from a different article and changed it an extension method.
And the call like: