为什么我不能将标签(Label)的BackColor设置为透明?我以前做过,但现在它就是不想要...
我创建了一个新的用户控件(UserControl),向其中添加了一个进度条(ProgressBar)和一个标签(Label)。当我将标签的BackColor设置为透明时,它仍然是灰色的 =/ 为什么会这样呢?
我的目的是让标签位于进度条的顶部,以便其文本显示在进度条内部...
为什么我不能将标签(Label)的BackColor设置为透明?我以前做过,但现在它就是不想要...
我创建了一个新的用户控件(UserControl),向其中添加了一个进度条(ProgressBar)和一个标签(Label)。当我将标签的BackColor设置为透明时,它仍然是灰色的 =/ 为什么会这样呢?
我的目的是让标签位于进度条的顶部,以便其文本显示在进度条内部...
在你的项目中添加一个新类,并贴上下面展示的代码。构建项目,然后从工具箱的顶部将新控件拖放到你的表单上。
using System;
using System.Windows.Forms;
public class TransparentLabel : Label {
public TransparentLabel() {
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
}
protected override CreateParams CreateParams {
get {
CreateParams parms = base.CreateParams;
parms.ExStyle |= 0x20; // Turn on WS_EX_TRANSPARENT
return parms;
}
}
}
Enabled
设置为false,此时透明度就会丢失。 - BrianWinForms并不真正支持透明控件,但是您可以自己创建透明控件。 在这里看看我的答案。
在您的情况下,您应该子类化进度条并重写OnPaint方法,在进度条上绘制文本。
在可视化编辑器或您的表单构造函数中将背景颜色设置为透明:
this.label1.BackColor = System.Drawing.Color.Transparent;
将标签的 Parent 属性设置为您想要在文本后面显示的控件。这可以在表单构造函数或 Load 方法中完成:
this.label1.Parent = progressBar1;
private void MakeTransparentLabel(System.Windows.Forms.LinkLabel LinkLabel)
{
this.MakeTransparentLabel(LinkLabel, Color.White);
}
private void MakeTransparentLabel(System.Windows.Forms.LinkLabel LinkLabel, Color ForeColor)
{
LinkLabel.ForeColor = ForeColor;
LinkLabel.LinkColor = ForeColor;
LinkLabel.VisitedLinkColor = ForeColor;
LinkLabel.ActiveLinkColor = ForeColor;
LinkLabel.DisabledLinkColor = ForeColor;
LinkLabel.LinkArea = new LinkArea(0, 0);
LinkLabel.LinkBehavior = LinkBehavior.NeverUnderline;
LinkLabel.Cursor = Cursors.Arrow;
LinkLabel.BackColor = Color.Transparent;
}
private void SetTransparentLabelText(System.Windows.Forms.LinkLabel LinkLabel, string Text)
{
if (string.IsNullOrEmpty(Text)) { LinkLabel.Text = " "; return; }
LinkLabel.Text = Text;
}
这是一种非常简单的解决方案,非常实用:
public class MyLabel : Label
{
private bool fTransparent = false;
public bool Transparent
{
get { return fTransparent; }
set { fTransparent = value; }
}
public MyLabel() : base()
{
}
protected override CreateParams CreateParams
{
get
{
if (fTransparent)
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
else return base.CreateParams;
}
}
protected override void WndProc(ref Message m)
{
if (fTransparent)
{
if (m.Msg != 0x14 /*WM_ERASEBKGND*/ && m.Msg != 0x0F /*WM_PAINT*/)
base.WndProc(ref m);
else
{
if (m.Msg == 0x0F) // WM_PAINT
base.OnPaint(new PaintEventArgs(Graphics.FromHwnd(Handle), ClientRectangle));
DefWndProc(ref m);
}
}
else base.WndProc(ref m);
}
}
这是我一段时间前编写的一个透明控件,它可以显示旋转的文本。大部分代码来自这里,尽管我记得我不得不进行一些调整才能使其正常工作。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Windows.Forms;
namespace MyNamespace
{
public partial class RotatedText : UserControl
{
private readonly Timer _invalidationTimer;
private const int WS_EX_TRANSPARENT = 0x00000020;
public RotatedText()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
_invalidationTimer = new Timer {Interval = 500, Enabled = true};
_invalidationTimer.Tick += TickHandler;
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Appearance")]
[Description("Text which appears in control")]
public string Text { get; set; }
#region Transparent background
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
private void TickHandler(object sender, EventArgs e)
{
InvalidateEx();
}
private void InvalidateEx()
{
if (Parent != null)
Parent.Invalidate(Bounds, false);
else
Invalidate();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Intentionally do nothing - stops background from drawing
//base.OnPaintBackground(e);
}
#endregion
//Rotate text and draw
protected override void OnPaint(PaintEventArgs e)
{
double angleRadians = Math.Atan2(Height, Width);
float angleDegrees = -1*(float) (angleRadians*180/Math.PI);
angleDegrees *= 0.9f;
e.Graphics.RotateTransform(angleDegrees, MatrixOrder.Append);
e.Graphics.TranslateTransform(20, Height - 75, MatrixOrder.Append);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Font font = new Font("Ariel", 50);
e.Graphics.DrawString(Text, font, Brushes.Gray, 1, 2); //Shadow
e.Graphics.DrawString(Text, font, Brushes.Red, 0, 0);
}
}
}
如果你想专注于设计Windows应用程序,我建议你使用WPF。
在WPF中使控件透明非常容易。
<TextBox Width="200" Height="40" Opacity="0.5"/>
你想要实现的完全可以做到。只需要花点时间来调整控件。可以创建一个透明背景的 Label 控件,然后将其放置在 Progressbar 控件上方。
请查看我的回答另一个 SO 问题。
正如我之前回答中的评论所述,控件是默认行为,并且是我记得的透明。
无论如何,您是否尝试设置UserControl的背景属性或包含标签的容器(Panel、Form等)?您的标签应该反映该颜色 :)
旧回答: 我已经有一段时间没有做winforms编程了,但是据我回忆,标签默认是透明的?因此只有文本获得实际颜色,背景颜色模仿其后面的任何内容 :)