2013-02-16 51 views
4

因此,在嘗試一段時間沒有運氣的情況下,我最終決定詢問一種使透明控件相互顯示的方法。Control To Control Transparency

enter image description here

正如你在圖片中看到我有2個透明的圖片框,它們顯示的背景很好,但是當涉及到選定的圖片框,你可以在圖片中看到的只是呈現的背景圖像窗體,但不是下面的其他圖片框。我知道,由於缺乏適當的渲染,winforms中有一個常見的情況,但問題是:

有沒有辦法解決這個渲染故障,有沒有辦法讓透明控件渲染對方?

嗯,這就是答案:Transparent images with C# WinForms

回答

2

控制的透明度取決於其母公司控制。您但是,可以使用自定義的容器控件,而不是圖片框父image.and也許這代碼usfull

using System; 
using System.Windows.Forms; 
using System.Drawing; 

public class TransparentControl : Control 
{ 
    private readonly Timer refresher; 
    private Image _image; 

    public TransparentControl() 
    { 
     SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
     BackColor = Color.Transparent; 
     refresher = new Timer(); 
     refresher.Tick += TimerOnTick; 
     refresher.Interval = 50; 
     refresher.Enabled = true; 
     refresher.Start(); 
    } 

    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= 0x20; 
      return cp; 
     } 
    } 

    protected override void OnMove(EventArgs e) 
    { 
     RecreateHandle(); 
    } 


    protected override void OnPaint(PaintEventArgs e) 
    { 
     if (_image != null) 
     { 
      e.Graphics.DrawImage(_image, (Width/2) - (_image.Width/2), (Height/2) - (_image.Height/2)); 
     } 
    } 

    protected override void OnPaintBackground(PaintEventArgs e) 
    { 
     //Do not paint background 
    } 

    //Hack 
    public void Redraw() 
    { 
     RecreateHandle(); 
    } 

    private void TimerOnTick(object source, EventArgs e) 
    { 
     RecreateHandle(); 
     refresher.Stop(); 
    } 

    public Image Image 
    { 
     get 
     { 
      return _image; 
     } 
     set 
     { 
      _image = value; 
      RecreateHandle(); 
     } 
    } 
} 
+0

不幸的是,這不會給任何其他想法的預期結果? – 2013-02-16 19:26:59

+0

http://stackoverflow.com/questions/11412169/is-it-possible-to-have-two-overlapping-picturebox-controls-with-transparent-imag – 2013-02-16 19:27:23

+0

非常感謝Shahrooz的幫助。 – 2013-02-16 19:34:58