在C#窗體中顯示PictureBox上方的透明標籤
在C#窗體中,嘗試在PictureBox上顯示透明標籤時,用戶可能會遇到灰色背景而不是預期的透明效果。這是因為PictureBox不是容器控件。
解決方案1:代碼實現
要解決此問題,請修改窗體構造函數,將標籤的父級更改為PictureBox並重新計算其位置。
<code class="language-csharp">public Form1() { InitializeComponent(); var pos = label1.Parent.PointToScreen(label1.Location); pos = pictureBox1.PointToClient(pos); label1.Parent = pictureBox1; label1.Location = pos; label1.BackColor = Color.Transparent; }</code>
解決方案2:設計時增強
或者,通過創建一個繼承自ParentControlDesigner的自定義類來解決設計時問題。
<code class="language-csharp">using System.ComponentModel; using System.Windows.Forms; using System.Windows.Forms.Design; // 添加对System.Design的引用 [Designer(typeof(ParentControlDesigner))] class PictureContainer : PictureBox {}</code>
說明
通過以上方法將PictureBox設為容器控件,標籤將成為PictureBox的子控件,其透明度將正確地顯示在PictureBox之上。
以上是如何在 C# 中在 PictureBox 上顯示透明標籤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!