1

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.

enter image description here

And i want to have this result :

enter image description here

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.

Saman Gholami
  • 3,416
  • 7
  • 30
  • 71

4 Answers4

3

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();
    }
}
coder
  • 13,002
  • 31
  • 112
  • 214
  • Thank you for your reply,but is your solution like my goal?please see second image. – Saman Gholami Feb 17 '13 at 17:50
  • Yes,It is like your goal :) – coder Feb 17 '13 at 17:51
  • Or here is the one you might need it http://www.sourcehints.com/articles/creating-a-real-transparent-label-in-vb-net.html – coder Feb 17 '13 at 17:52
  • Thank you for your help,your link is that i want exactly but it is VB.NET :( – Saman Gholami Feb 17 '13 at 17:59
  • Thank you so much.ye use that website for converting code.but how should i do this?is this a class that extends from Label?or a component? – Saman Gholami Feb 17 '13 at 19:43
  • Can't remember where, but I did read somewhere that overlapping controls should be avoided (a control inside a *container* control is not overlapping), for lots of good reasons; one being that every overlapping pixel gets written twice in the process. To me it seems that the *natural behavior* of a `Label` agrees with that statement. – Mathieu Guindon Feb 17 '13 at 22:40
  • I exactly need to implement this method that Coder prepare for us.but i don't know what should i do.the converted code to C# has error. – Saman Gholami Feb 17 '13 at 23:10
0

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

Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
0

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".

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • Thank you for your reply,your solution is correct ,but i want to movable the label for the User and width your solution it can't be. – Saman Gholami Feb 17 '13 at 20:13
  • If you register the form's `Paint` event, you can animate your label with a drop shadow and gradually increasing font size if you want: with little tweaks you can totally control where, how, when and if the text gets drawn - this runs whenever `Form1` needs to be painted, so calling the form's `Refresh` method should just work. – Mathieu Guindon Feb 17 '13 at 21:36
  • ...come to think about it, you're probably better off drawing inside a picturebox and refreshing only that container (i.e. register the picturebox's Paint event instead), I think there could be flickering if the form has other things (controls) to refresh - wouldn't be pretty! – Mathieu Guindon Feb 17 '13 at 21:46
  • I'm agree with you ,i think the code that Coder prepare for us is suitable ,please see the link and result. – Saman Gholami Feb 17 '13 at 22:00
  • @SamanGholami feel free to *accept* the answer that best answers your question. Remember that questions with an accepted answer stop showing up in the "unanswered" questions stack. – Mathieu Guindon Feb 19 '13 at 01:00
0

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);
    }
}
MUG4N
  • 19,377
  • 11
  • 56
  • 83
  • Thank you for your reply,your solution is correct ,but i want to movable the label for the User and width your solution it can't be. – Saman Gholami Feb 17 '13 at 20:13