1

如何将标签放在另一个标签上,并且它们的文本同时显示?当我做这项工作时,一个标签会出现在另一个标签的顶部,并且上面标签的文本会显示出来。即使我选择 BackColor = Transparent 但它不起作用。见下图。这是两个标签,标签 1 位于标签 2 下,标签 1 的文本丢失。

在此处输入图像描述

我想得到这个结果:

在此处输入图像描述

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

4

4 回答 4

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();
    }
}
于 2013-02-17T17:41:22.600 回答
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来调整第二个“标签”的位置。

于 2013-02-17T18:02:51.893 回答
0

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

于 2013-02-17T17:36:20.473 回答
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);
    }
}
于 2013-02-17T18:14:17.627 回答