C# - 隐藏表单,只显示标签?

时间:2021-07-06 15:52:26

How can I hide all of the form, but only show a label?

如何隐藏所有表单,但只显示标签?

I want my program to only display a label. No minimize, maximize buttons. No title bar. No form background. Just a label.

我希望我的程序只显示标签。没有最小化,最大化按钮。没有标题栏。没有表格背景。只是一个标签。

I tried using FormBorderStyle to none, and resize it as small as possible. But there seems to be a minimum size of the form. It goes back to about 3cm wide, and 1cm high.

我尝试使用FormBorderStyle为none,并尽可能小地调整它的大小。但似乎表格的最小尺寸。它可以回到约3厘米宽,1厘米高。

Is there any way to completely get rid of everything but my label? I also want to be able to move this label around when I hold down the mouse.

除了我的标签之外,还有什么方法可以彻底摆脱一切吗?我还希望能够在按住鼠标时移动此标签。

I found no code for this, so unfortunately I dont have anything at the moment.

我找不到这个代码,所以不幸的是我现在没有任何东西。

Any help is appreciated!

任何帮助表示赞赏!

edit: I am not talking about this.hide(), I want the form to pretty much vanish, while only the label is shown.

编辑:我不是在谈论this.hide(),我希望表单几乎消失,而只显示标签。

One way would be to set opacity to full on the form. But that affects my label as well. Any way to not make the label transparent?

一种方法是在表单上将opacity设置为full。但这也影响了我的标签。任何不使标签透明的方法?

3 个解决方案

#1


What you need to use is Region property. Assuming you

您需要使用的是Region属性。假设你

  • have a form
  • 有一个表格

  • have a label on the form
  • 在表格上有一个标签

  • set FormBorderStyle ==> None (this is important, otherwise you'll have to take form's non-client area into account - header, etc.) and ShowInTaskBar ==> false

    set FormBorderStyle ==> None(这很重要,否则你必须将表单的非客户区域考虑在内 - 标题等)和ShowInTaskBar ==> false

    private void Form4_Load(object sender, EventArgs e)
    {
        Region = new Region(label1.Bounds);
    }
    

#2


This is what you will get if you follow my tutorial:

如果您按照我的教程,这将是您将获得的:

C# - 隐藏表单,只显示标签?

Alright, i believe i understand what you want.

好吧,我相信我明白你想要的。

Do as it follows:

做如下:

First, create a form with FormBorderStyle = none, then you go to the last property of the form and set a transparency key, i'd recommend lime, because green is a really strong color.

首先,创建一个FormBorderStyle = none的表单,然后转到表单的最后一个属性并设置透明度键,我建议使用lime,因为绿色是一种非常强烈的颜色。

Once you have your transparency key, you gotta change the form background color to the same as your transparency key.

获得透明度密钥后,您必须将表单背景颜色更改为与透明度密钥相同。

If you run your code you will notice it will be only your label, but you can't move it... yet.

如果您运行代码,您会发现它只是您的标签,但您无法移动它......

The code for moving it around is taken from here:

移动它的代码取自这里:

private bool _dragging = false;
private Point _offset;
private Point _start_point = new Point(0, 0);

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    _dragging = true;  // _dragging is your variable flag
    _start_point = new Point(e.X, e.Y);
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    _dragging = false;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (_dragging)
    {
        Point p = PointToScreen(e.Location);
        Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
    }
}

Add this to your form code, then set the right actions to your LABEL. On your label MouseDown should be panel1_MouseDown, etc, etc. If you did everything right, you should have a floating movable text by now! :)

将其添加到表单代码中,然后将正确的操作设置为LABEL。在你的标签上MouseDown应该是panel1_MouseDown等等。如果你做的一切正确,你现在应该有一个浮动的可移动文本! :)

#3


I have an easy way to do it:

我有一个简单的方法:

  1. Check out "TransparencyKey" in the properties of Form
  2. 在Form的属性中查看“TransparencyKey”

  3. Set the color of transparency the color of your form background color.
  4. 将透明度的颜色设置为表单背景颜色的颜色。

  5. Change "FormBorderStyle" to none.
  6. 将“FormBorderStyle”更改为none。

  7. Add a Label to your project and write something.
    http://s1.freeupload.ir/i/00082/vmi50rd5w8nd.jpg
  8. 为项目添加标签并编写一些内容。 http://s1.freeupload.ir/i/00082/vmi50rd5w8nd.jpg

  9. Test your project it should be like this:

  10. 测试你的项目它应该是这样的:

http://s1.freeupload.ir/i/00082/oy06dqx4wuxx.jpg

I hope this helped you!

我希望这对你有所帮助!

#1


What you need to use is Region property. Assuming you

您需要使用的是Region属性。假设你

  • have a form
  • 有一个表格

  • have a label on the form
  • 在表格上有一个标签

  • set FormBorderStyle ==> None (this is important, otherwise you'll have to take form's non-client area into account - header, etc.) and ShowInTaskBar ==> false

    set FormBorderStyle ==> None(这很重要,否则你必须将表单的非客户区域考虑在内 - 标题等)和ShowInTaskBar ==> false

    private void Form4_Load(object sender, EventArgs e)
    {
        Region = new Region(label1.Bounds);
    }
    

#2


This is what you will get if you follow my tutorial:

如果您按照我的教程,这将是您将获得的:

C# - 隐藏表单,只显示标签?

Alright, i believe i understand what you want.

好吧,我相信我明白你想要的。

Do as it follows:

做如下:

First, create a form with FormBorderStyle = none, then you go to the last property of the form and set a transparency key, i'd recommend lime, because green is a really strong color.

首先,创建一个FormBorderStyle = none的表单,然后转到表单的最后一个属性并设置透明度键,我建议使用lime,因为绿色是一种非常强烈的颜色。

Once you have your transparency key, you gotta change the form background color to the same as your transparency key.

获得透明度密钥后,您必须将表单背景颜色更改为与透明度密钥相同。

If you run your code you will notice it will be only your label, but you can't move it... yet.

如果您运行代码,您会发现它只是您的标签,但您无法移动它......

The code for moving it around is taken from here:

移动它的代码取自这里:

private bool _dragging = false;
private Point _offset;
private Point _start_point = new Point(0, 0);

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    _dragging = true;  // _dragging is your variable flag
    _start_point = new Point(e.X, e.Y);
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    _dragging = false;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (_dragging)
    {
        Point p = PointToScreen(e.Location);
        Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
    }
}

Add this to your form code, then set the right actions to your LABEL. On your label MouseDown should be panel1_MouseDown, etc, etc. If you did everything right, you should have a floating movable text by now! :)

将其添加到表单代码中,然后将正确的操作设置为LABEL。在你的标签上MouseDown应该是panel1_MouseDown等等。如果你做的一切正确,你现在应该有一个浮动的可移动文本! :)

#3


I have an easy way to do it:

我有一个简单的方法:

  1. Check out "TransparencyKey" in the properties of Form
  2. 在Form的属性中查看“TransparencyKey”

  3. Set the color of transparency the color of your form background color.
  4. 将透明度的颜色设置为表单背景颜色的颜色。

  5. Change "FormBorderStyle" to none.
  6. 将“FormBorderStyle”更改为none。

  7. Add a Label to your project and write something.
    http://s1.freeupload.ir/i/00082/vmi50rd5w8nd.jpg
  8. 为项目添加标签并编写一些内容。 http://s1.freeupload.ir/i/00082/vmi50rd5w8nd.jpg

  9. Test your project it should be like this:

  10. 测试你的项目它应该是这样的:

http://s1.freeupload.ir/i/00082/oy06dqx4wuxx.jpg

I hope this helped you!

我希望这对你有所帮助!