如何在Windows窗体中为标签添加边框? [英] How can I add borders to label in Windows Forms?

查看:112
本文介绍了如何在Windows窗体中为标签添加边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个内部带有白色标签的表单,当我单击某些内容时,该表单将消失,仅显示标签.到目前为止,我尝试将TransparencyKey放在Lime上,当我单击某些内容时,将BackColor更改为Lime并将FormBorderStyle设置为None.但是问题出在我现在正在做的事情是白色标签没有边框,所以您看不到它.我知道BorderStyle属性,而这不是我想要的,我希望边框恰好位于文本周围,以便您可以在其他内容上方看到该文本.有什么方法可以在标签上添加边框吗?

I'm trying to create a form with white label inside, that when I click on something the form will disappear and only show the label. So far I tried to put the TransparencyKey on Lime and when I click on something I changed the BackColor to Lime and set the FormBorderStyle to None. But the problem is with what I'm doing right now is that the white label has no borders, so You can't really see it. I know about the BorderStyle property and this is not what I want, I want the border to be exactly around the text so you can see the text above other things. Is there any way to add borders to a label?

这是我的代码,

private void label1_Click(object sender, EventArgs e)
{
    if (BackColor == Color.Lime)
    {
        FormBorderStyle = FormBorderStyle.Sizable;
        BackColor = Color.Black;
        Location = new Point(Left - 8, Top - 30);
    }
    else
    {
        FormBorderStyle = FormBorderStyle.None;
        BackColor = Color.Lime;
        Location = new Point(Left + 8, Top + 30);
    }
}

推荐答案

好的;Label上有一个BorderStyle属性,可以将其设置为FixedSingle或Fixed3D.FixedSingle是ForeColor颜色的单像素边框,而Fixed3D是使用标签背景灰度的斜面3D边框.

Well sure; there is a BorderStyle property on Label that can be set to FixedSingle or Fixed3D. FixedSingle is a single-pixel border in the ForeColor color, while Fixed3D is a beveled 3D border using greyscales of the label's background.

好的,确切需要些什么.在我看来,您有两种选择.

OK, a little more detail in what exactly is needed. As I see it you have a couple options.

  1. 放置两个标签,一个放在另一个标签上,内容和格式相同,除了后面的一个标签是白色,前面的一个标签是黑色,后面的标签与前面的标签偏移在X和/或Y尺寸上增加一个像素.您会在黑色文字后面看到一个白色的阴影".您甚至可以设置四个标签,每个标签在X和y方向上偏移1个像素,以获得完整的光晕".如果您想在多个地方执行此操作,则可以将其设置为UserControl.设置一次控件的文本,控件将填充所有5个标签.您可以尝试使用字体大小或粗细来进行播放,但是我怀疑您会得到正确排列的东西,并且在所有情况下字母周围都有一个完美的1像素边框.

  1. Put two labels, one on top of the other, with the same content and formatting EXCEPT the one in back is white and the one in front is black, and the label in back is offset from the one in front by one pixel in the X and/or Y dimensions. You'll get a white "shadow" behind the black text. You could even set up four labels, each offset 1 pixel in both X and y, for a complete "halo". You could set this up as a UserControl if you wanted to do this in multiple places; set the text of the control once and the control will populate all 5 labels. You could try playing with font size or weight, but I doubt you'd get something that lined up correctly and had a perfect 1-pixel border around the letters in all cases.

在洋红色背景上创建文本图像,将其打成白色,然后将洋红色键入为透明颜色,将其另存为位图.然后,使用标签(或PictureBox)中的图像.

Create an image of your text on a magenta background, ring it in white, and save it as a bitmap with the magenta keyed as the transparent color. Then, use the image in the label (or a PictureBox).

这篇关于如何在Windows窗体中为标签添加边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆