2016-02-26 89 views
0


我是新的創建用戶控制,並且在我的第一個用戶控件我從圖片框和標籤,
的PictureBox用於繪製形狀和標籤顯示文本在該形狀使用。 我被設置爲標籤的picturebox父級,並且將backcolor標籤設置爲透明,如果沒有設置爲visible = false的任何文本標籤過德魯形狀的透明控制

現在我遇到了問題,當標籤可見時,我無法正確看到圖片框。

enter image description here

我怎樣才能解決這個問題?

也搽用戶控制事件不起作用

private void Ucontrol_Paint(object sender, PaintEventArgs e) 
    { 
     if (RightToLeft) 
     { 
      lblTxt.RightToLeft = System.Windows.Forms.RightToLeft.Yes; 
     } 
     else 
     { 
      lblTxt.RightToLeft = System.Windows.Forms.RightToLeft.No; 
     } 

     lblTxt.ForeColor = FontColor; 
     lblTxt.Text = Txt; 
     if (Question) 
     { 
      BorderColor = Color.DarkBlue; 
      BackColor = Color.FromArgb(75, 163, 234); 
      CreateQuestion(BorderColor, BackColor); 
     } 
     else 
     { 
      BorderColor = Color.DarkGreen; 
      BackColor = Color.FromArgb(59, 226, 75); 
      CreateAnswer(BorderColor, BackColor); 
     } 
    } 
+0

你已經接受了錯誤的答案。您需要添加到代碼中的只有一行,用於將標籤添加到picturebox的控件集合,另一行用於設置其位置。 Pictubrebox在容器中沒有幫助,但它們的工作也是如此。 – TaW

回答

-1

窗體控件沒有一個真正的transpartent背景,他們複製它的父內容。

另外,PictureBox不能作爲其他控件的父項,因爲它們不是容器。

然後,而不是使用picturebox只需設置usercontrol背景圖像並將標籤放在它上面,透明度應該工作。

這裏是一個工作示例手工繪製的控制內容:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 

     Label lbl = new Label(); 
     lbl.Location = new Point(10, 10); 
     lbl.Width = 150; 
     lbl.Height = 150; 
     lbl.BackColor = Color.Transparent; 
     lbl.Text = @"asdfasdfasdfasdf\r\nasdfasdfasdf\r\n\r\nasdfasdfasdf"; 

     lbl.Visible = true; 

     this.Controls.Add(lbl); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     e.Graphics.FillRectangle(Brushes.Red, new Rectangle(10, 10, 100, 100)); 
     e.Graphics.FillEllipse(Brushes.Yellow, new Rectangle(10, 10, 100, 100)); 
    } 
} 
+0

我如何使用Graphics作爲BackgroundImage? – Sara

+0

如果您直接繪製,而不是繪製到繪圖框繪製到usercontrol,請使用Paint事件。 – Gusman

+0

謝謝,但不行! :(,結果相同 – Sara