如何将Label放在另一个Label上

时间:2013-02-17 17:31:17

标签: c# .net winforms label transparency

如何在另一个标签上贴上标签,同时显示它们的文字? 当我做这项工作时,其中一个标签位于另一个标签之上,上面标签的文字显示。 即使我选择BackColor = Transparent但它不起作用。 见下图。 这是两个标签,label1在label2下面,而label1的文本缺失。

enter image description here

我希望得到这样的结果:

enter image description here

想象一下我有两个标签。其中一个是24pt字体大小,另一个是8pt。 当我使用更大的字体时,标签的框架比另一个标签更大。我不能让它们更接近。

4 个答案:

答案 0 :(得分:3)

您可以尝试这种方式:

label.Parent = parent with background
label.BackColor = Color.Transparent
label.Location = location you want offset with parent

以下是C#中的转换代码:

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
public class TransparentLabel {

    public TransparentLabel() {
        //  This call is required by the designer.
        InitializeComponent();
        //  Add any initialization after the InitializeComponent() call.
        //  Add any initialization after the InitializeComponent() call.
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.Opaque, true);
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.components = new System.ComponentModel.Container();
        RF = new RectangleF(0, 0, base.Width, base.Height);
        LabelForeColorBrush = new SolidBrush(base.ForeColor);
    }

    private StringFormat sFormat;

    private RectangleF RF;

    private SolidBrush LabelForeColorBrush = null;

    private void UpdateText() {
        try {
            sFormat = new StringFormat();
            int x = 0;
            int y = 0;
            // With...
            switch (TextAlignment) {
                case ContentAlignment.BottomCenter:
                    sFormat.Alignment = StringAlignment.Center;
                    sFormat.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomLeft:
                    sFormat.Alignment = StringAlignment.Near;
                    sFormat.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomRight:
                    sFormat.Alignment = StringAlignment.Far;
                    sFormat.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.MiddleLeft:
                    sFormat.Alignment = StringAlignment.Near;
                    sFormat.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleCenter:
                    sFormat.Alignment = StringAlignment.Center;
                    sFormat.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleRight:
                    sFormat.Alignment = StringAlignment.Far;
                    sFormat.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.TopCenter:
                    sFormat.Alignment = StringAlignment.Center;
                    sFormat.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopLeft:
                    sFormat.Alignment = StringAlignment.Near;
                    sFormat.LineAlignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopRight:
                    sFormat.Alignment = StringAlignment.Far;
                    sFormat.LineAlignment = StringAlignment.Near;
                    break;
            }
            sFormat.FormatFlags = StringDirection;
            ResizeControl();
        }
        catch (Exception ex) {
        }
    }

    private void ResizeControl() {
        RF.Size = new Size(base.Size);
        Invalidate();
    }

    private StringFormatFlags _StringDirection = (StringFormatFlags.NoClip < Description("The Direction of the Text."));

    public StringFormatFlags StringDirection {
        get {
            return _StringDirection;
        }
        set {
            _StringDirection = value;
            UpdateText;
        }
    }

    private System.Drawing.ContentAlignment _TextAlignment = (ContentAlignment.MiddleCenter < Description("The Text Alignment that will appear on this control."));

    public System.Drawing.ContentAlignment TextAlignment {
        get {
            return _TextAlignment;
        }
        set {
            _TextAlignment = value;
            UpdateText();
        }
    }

    public override System.Drawing.Color ForeColor {
        get {
            return base.ForeColor;
        }
        set {
            base.ForeColor = value;
            LabelForeColorBrush = new SolidBrush(value);
        }
    }

    private string _Labeltext = ("TransparentLabel" < Description("The text to be displayed in supports with real transparency."));

    public string LabelText {
        get {
            return _Labeltext;
        }
        set {
            _Labeltext = value;
            Invalidate();
        }
    }

    [Browsable(false)]
    [EditorBrowsable(false)]
    public override System.Drawing.Color BackColor {
        get {
            return base.BackColor;
        }
        set {
            base.BackColor = value;
        }
    }

    protected override System.Windows.Forms.CreateParams CreateParams {
        get {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = (cp.ExStyle | 32);
            return cp;
        }
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
        try {
            base.OnPaint(e);
            //  draw the text on the control
            e.Graphics.DrawString(LabelText, base.Font, LabelForeColorBrush, RF, sFormat);
            //  MyBase.OnPaint(e)
        }
        catch (Exception ex) {
        }
    }

    private void TransparentLabel_Resize(object sender, System.EventArgs e) {
        ResizeControl();
    }
}

答案 1 :(得分:0)

Winforms标签不支持透明度。如果你想要实现这一目标,你需要创建自己的标签。

答案 2 :(得分:0)

这不是标签,但它可以帮助您实现目标:

public void Form1()
{
     Paint += Form1_Paint;
}

public void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(SystemColors.Control);
    DrawOverlappingLabels(e, *new point for label 1*, *new point for label 2*);
}


private void DrawOverlappingLabels(PaintEventArgs e, _
    Point positionLabel1, Point positionLabel2)
{
    var graphics = e.Graphics();
    var rectLabel1 = new Rectangle(new positionLabel1, new Size(150, 30));
    var rectLabel2 = new Rectangle(new positionLabel2, new Size(150, 30));

    graphics.DrawString("Label1", new Font(Font.FontFamily, 24f), _
        new SolidBrush(Color.Black), rectLabel1);

    graphics.DrawString("Label2", new Font(Font.FontFamily, 8f), _ 
        new SolidBrush(Color.Black), rectLabel2);
}

定义第二个Rectangle以调整第二个“标签”的位置。

答案 3 :(得分:0)

我不知道我的问题是否正确。但不是标签掉落阴影你想要什么?

如果是这种情况,那么您应该查看此网站:Label drop shadow

小样本使用:

using System;
using System.Drawing;
using System.Windows.Forms;

class WinFormsDropShadow: Form
{
    public static void Main()
    {
        Application.Run(new WinFormsDropShadow());
    }
    public WinFormsDropShadow()
    {
        Text = "Windows Forms Drop Shadow";
        BackColor = Color.White;
        Size = new Size(640, 480);
    }
    protected override void OnPaint(PaintEventArgs args)
    {
        Graphics grfx = args.Graphics;
        Font fnt = new Font("Arial Black", 96);
        string str = "Shadow";

        grfx.DrawString(str, fnt, Brushes.Gray, grfx.DpiX / 12, grfx.DpiY / 12);
        grfx.DrawString(str, fnt, Brushes.Black, 0, 0);
    }
}