简体   繁体   中英

How can put a Label on another Label

How can i put a label on another label and the text of them shows simultaneously? When ever i do this work ,one of the labels goes top of another and the text of upper label shows. Even i select BackColor = Transparent but it doesn't worked. See Image below. These are two labels and label1 goes under label2 and the text of label1 is missing.

在此输入图像描述

And i want to have this result :

在此输入图像描述

Just imagine i have two labels.one of them with 24pt font size,and another one is 8pt. When i use larger Font ,the label has larger Frame than the other label.and i can't make them closer.

You can try this way:

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

Here is the converted code in 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();
    }
}

Winforms labels don't support transparency. You need to create your own label if you want to make that happen.

This isn't labels, but it should get you what you're trying to achieve:

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);
}

Define a second Rectangle to tweak the positioning of the 2nd "label".

I don't know if I got your question right. But isn't label drop shadowing what you want?

If that is the case then you should check out this site: Label drop shadow

Small sample of usage:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM