在Windows窗体

时间:2017-04-10 13:55:39

标签: c# winforms fonts label outline

我想更改标签的字体,使其为黑色轮廓的白色。我已经为我在代码中创建的标签找到了一种方法,但我想为现有的控件标签添加大纲。

previous stackoverflow question中,我找到了一个类(CustomLabel类),它为标签添加了一个轮廓。

我知道我可以使用以下代码创建一个新标签,并使用此类,但我无法找到任何有关如何将此类(CustomLabel)用于我的控件类(label)的信息,并将大纲添加到我的Windows窗体中已存在标签。

Form newForm = new Form();
CustomLabel newLabel = new CustomLabel();
newForm.Controls.Add(newLabel);

newLabel.BackColor = Color.Black;
newLabel.Font = new System.Drawing.Font("Impact", 18F,
FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
newLabel.ForeColor = Color.Crimson;
newLabel.OutlineForeColor = Color.Black;
newLabel.OutlineWidth = 2;
newLabel.Text = "test text";

newForm.Show();
newForm.TopMost = true;
newLabel.AutoSize = true;
newLabel.Location = new Point(100, 250);

我遇到的问题是我不知道如何将此类应用于控件(标签),以及我在设计(GUI)中创建的几个标签。

我想,如果我可以这样做:

// myLabel is made in the designer window
myLabel.OutlineForeColor = Color.Black;
myLabel.OutlineWidth = 2;

或另一个想法是,创建一个函数,它接受我的设计器中每个标签的所有自定义属性(标签是函数的参数),将它们添加到newLabel(与上面相同),但newLabel有两个自定义像OutLineForeColor和OutlineWidth这样的属性,然后最终newLabel的所有属性都被覆盖到函数的参数(自定义标签之一)。 所以像这样:

private void addNewProperties(Label myFormLabel)
{
    CustomLabel newLabel = new CustomLabel();
    newLabel.BackColor = myFormLabel.BackColor;
    newLabel.Font = myFormLabel.Font;
    newLabel.ForeColor = myFormLabel.ForeColor;
    newLabel.OutlineForeColor = Color.White;
    newLabel.OutlineWidth = 2;
    newLabel.Text = myFormLabel.Text;
    newLabel.Location = myFormLabel.Location;

    myFormLabel = newLabel;
}

最后,我想知道一种不同的方法是否会更容易;如果我可以在C#中将CustomLabel类添加到Label类本身,那么我可以直接将所需的方法添加到表单标签中。

0 个答案:

没有答案