如何将标签贴在另一个标签上,并且它们的文字同时显示? 当我做这项工作时,其中一个标签会变成另一个标签,上面的标签显示文字。 即使我选择BackColor = Transparent,但它不起作用。 见下图。 这些是两个标签,label1位于label2下,而label1的文本缺失。如何将标签贴在另一个标签上
,我想有这样的结果:
试想一下,我有24PT字体大小是2个labels.one他们,另一个是8磅。 当我使用较大的字体时,标签的框架比其他标签更大。我无法使它们更接近。
如何将标签贴在另一个标签上,并且它们的文字同时显示? 当我做这项工作时,其中一个标签会变成另一个标签,上面的标签显示文字。 即使我选择BackColor = Transparent,但它不起作用。 见下图。 这些是两个标签,label1位于label2下,而label1的文本缺失。如何将标签贴在另一个标签上
,我想有这样的结果:
试想一下,我有24PT字体大小是2个labels.one他们,另一个是8磅。 当我使用较大的字体时,标签的框架比其他标签更大。我无法使它们更接近。
你可以试试这个方法:
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();
}
}
Winforms标签不支持透明度。如果你想做到这一点,你需要创建自己的标签。
谢谢你的回复,但我想同时给他们看。 – 2013-02-17 17:40:44
@SamanGholami你是什么意思同时? – AbZy 2013-02-17 17:43:01
这不是标签,但它应该让你你想要达到什么样的:
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-17 20:13:56
如果您例如表单的“Paint”事件,如果需要,可以使用阴影为标签制作动画效果,并逐渐增加字体大小:只需稍作调整,即可完全控制文字的绘制位置,方式,时间以及是否绘制文本 - 每当“Form1 '需要着色,所以调用窗体的'Refresh'方法应该可以工作。 – 2013-02-17 21:36:13
...来思考它,你可能更好的是在一个picturebox内绘制并刷新该容器(即注册picturebox的Paint事件),我认为如果表单有其他东西(控件)可能会闪烁,刷新 - 不会很漂亮! – 2013-02-17 21:46:19
我不知道我的问题是否正确。但是,不是标签投影你想要什么?
如果是这样的话,那么你应该看看这个网站:
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-17 20:13:05
你能后的你想达到什么样的一个画面:Label drop shadow
使用的小样本? – jAC 2013-02-17 17:35:38
这可能会有所帮助:http://stackoverflow.com/questions/9387267/transparent-background-label-over-picturebox – 2013-02-17 17:40:39
哪个控件应该使用? – 2013-02-17 17:41:29