【问题标题】:Transparent control over PictureBox对 PictureBox 的透明控制
【发布时间】:2022-01-17 22:33:37
【问题描述】:

在我的 C# 表单中,我有一个在下载事件中显示下载百分比的标签:

  this.lblprg.Text = overallpercent.ToString("#0") + "%";

Label 控件的 BackColor 属性设置为透明,我希望它显示在 PictureBox 上。但这似乎无法正常工作,我看到灰色背景,它在图片框顶部看起来不透明。我该如何解决这个问题?

【问题讨论】:

    标签: c# background label


    【解决方案1】:

    Label 控件很好地支持透明度。只是设计师不会让你正确放置标签。 PictureBox 控件不是容器控件,因此 Form 成为标签的父级。所以你会看到表单的背景。

    通过向表单构造函数添加一些代码很容易解决。您需要更改标签的 Parent 属性并重新计算它的 Location,因为它现在相对于图片框而不是表单。像这样:

        public Form1() {
            InitializeComponent();
            var pos = this.PointToScreen(label1.Location);
            pos = pictureBox1.PointToClient(pos);
            label1.Parent = pictureBox1;
            label1.Location = pos;
            label1.BackColor = Color.Transparent;
        }
    

    在运行时看起来像这样:


    另一种方法是解决设计时问题。那只需要一个属性。添加对 System.Design 的引用并向您的项目添加一个类,粘贴此代码:

    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;    // Add reference to System.Design
    
    [Designer(typeof(ParentControlDesigner))]
    class PictureContainer : PictureBox {}
    

    【讨论】:

    • 谢谢,它工作得很好,但是图片框是一个自定义的 ProgressBar 并且下载详细信息不会出现,直到标签位置的 ProgressBar 增加,我怎样才能让它始终可见,而不仅仅是当 ProgressBar触动了吗?这里有一个!picture
    • 我这样做了,直到我意识到我不需要图片框。 Label 控件本身有一个 Image 属性,如果是不需要图像格式化的简单场景,这可能就足够了。
    • 这也不适用于标签下方的ProgresBar
    • 也许这个技巧可以用 ProgressBar 来完成?怎么做?
    • 尤其是你的'...并重新计算它的位置,因为它现在是相对于图片框而不是表单...'真的很有帮助!
    【解决方案2】:

    你可以使用

    label1.Parent = pictureBox1;
    label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239
    

    【讨论】:

    • 太完美了!从未使用过标签的父属性。做得好。 :)
    • 对我来说,“Color.Transparent”显示为未知,“System.Drawing.Color.Transparent”在运行后失败...
    • 感谢您确认我认为接受的答案过长。另外,你可以在设计器中设置透明的BackColor,也不需要写出来。
    • 谢谢。设置父工作正常。将标签添加到 PictureBox 控件似乎也设置了 Parent 属性(例如 pictureBox1.Controls.Add(label1);)。并且标签 FlatStyle 必须设置为“标准”以允许标签 BackColor 设置为 Color.Transparent。
    【解决方案3】:

    您可以使用 TextRenderer 绘制文本,它会在没有背景的情况下绘制它:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        TextRenderer.DrawText(e.Graphics, 
                              overallpercent.ToString("#0") + "%", 
                              this.Font, 
                              new Point(10, 10), 
                              Color.Red);
    }
    

    当overallpercent值发生变化时,刷新pictureBox:

    pictureBox1.Refresh();
    

    您也可以使用 Graphics.DrawString,但 TextRenderer.DrawText(使用 GDI)比 DrawString (GDI+) 快

    另请查看另一个答案here 和DrawText 参考here

    【讨论】:

    • 谢谢!这是唯一有助于在 ProgressBar 上放置文本(透明背景)的解决方案。
    【解决方案4】:

    为了便于您的设计。 您可以将标签放在面板内。并设置面板的背景图像是您想要的每个图像。设置标签背景是透明的

    【讨论】:

    • 如果图像是动画 gif,此解决方案将不起作用。动画不能作为面板的背景。
    【解决方案5】:

    在尝试了大多数提供的解决方案但没有成功后,以下对我有用:

    label1.FlatStyle = FlatStyle.Standard
    label1.Parent = pictureBox1
    
    label1.BackColor = Color.Transparent    
    

    【讨论】:

      【解决方案6】:

      一种适用于所有事情的方法,但您需要处理位置、调整大小、移动等。使用透明表单:

              Form form = new Form();
              form.FormBorderStyle = FormBorderStyle.None;
              form.BackColor = Color.Black;
              form.TransparencyKey = Color.Black;
              form.Owner = this;
              form.Controls.Add(new Label() { Text = "Hello", Left = 0, Top = 0, Font = new Font(FontFamily.GenericSerif, 20), ForeColor = Color.White });
              form.Show();
      

      【讨论】:

        【解决方案7】:

        将 Visual Studio 与 Windows 窗体一起使用,您可以通过将 using System.Drawing; 添加到 Form1.Designer.cs 中来为标签或其他元素应用透明度。这样,您将拥有可从“属性”面板(在 BackColor 的外观中)获得透明度。或者只是在 Designer.cs this.label1.BackColor = System.Drawing.Color.Transparent;

        中编辑代码

        【讨论】:

          猜你喜欢
          • 2017-11-28
          • 2011-12-14
          • 1970-01-01
          • 2010-12-19
          相关资源
          最近更新 更多