2015-01-13 48 views
0

我正在尋找如何在C#WinForms程序中繪製透明控件(可以看到它的背後)。半透明的自定義控件

我需要類似下面的圖片。正如你所看到的,在圖像的中心有一個半透明的組件(它真的像一個光標一樣),它正在填充一個圓形的扇區。

我專注於獲得半透明控制,現在,如果我在我的組件上設置透明背景(從標準控件類繼承),它的背景具有相同的父背景顏色。在WinForms中顯然是複雜的獲得透明控件,但圖像是在WinForms程序中獲取的。

你有什麼想法嗎?這可能嗎?

enter image description here


編輯: 很抱歉,如果這是一個重複的問題,我要粘貼如下至極我的代碼是採取其他的程序員,但我包括在鏈接的建議。 (只與有問題的代碼顯示,沒有屬性,無屬性等)

public partial class LoadingCircle : Control 
{ 
    public LoadingCircle() 
    { 
     SetStyle(ControlStyles.UserPaint, true); 
     SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     SetStyle(ControlStyles.ResizeRedraw, true); 
     SetStyle(ControlStyles.SupportsTransparentBackColor, true); 

     //Added following StackOverflow 
     SetStyle(ControlStyles.Opaque, true); 
     this.BackColor = Color.Transparent; 

     m_Color = DefaultColor; 

     GenerateColorsPallet(); 
     GetSpokesAngles(); 
     GetControlCenterPoint(); 

     m_Timer = new Timer(); 
     m_Timer.Tick += new EventHandler(aTimer_Tick); 
     ActiveTimer(); 

     this.Resize += new EventHandler(LoadingCircle_Resize); 

    } 

    void aTimer_Tick(object sender, EventArgs e) 
    { 
     m_ProgressValue = ++m_ProgressValue % m_NumberOfSpoke; 
     Invalidate(); 
    } 


    protected override void OnPaint(PaintEventArgs e) 
    { 
     if (m_NumberOfSpoke > 0) 
     { 
      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

      int intPosition = m_ProgressValue; 
      for (int intCounter = 0; intCounter < m_NumberOfSpoke; intCounter++) 
      { 
       intPosition = intPosition % m_NumberOfSpoke; 
       DrawLine(e.Graphics, 
         GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]), 
         GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]), 
         m_Colors[intCounter], m_SpokeThickness); 
       intPosition++; 
      } 
     } 

     base.OnPaint(e); 


    } 

    //Added following StackOverflow 
    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= 0x20; //WS_EX_TRANSPARENT 
      return cp; 
     } 
    } 


    protected override void OnBackColorChanged(EventArgs e) 
    { 
     if (this.Parent != null) Parent.Invalidate(this.Bounds, true); 
     base.OnBackColorChanged(e); 
    } 

    protected override void OnParentBackColorChanged(EventArgs e) 
    { 
     this.Invalidate(); 
     base.OnParentBackColorChanged(e); 
    } 
    //----- End 

    private void DrawLine(Graphics _objGraphics, PointF _objPointOne, PointF _objPointTwo, 
          Color _objColor, int _intLineThickness) 
    { 
     using(Pen objPen = new Pen(new SolidBrush(_objColor), _intLineThickness)) 
     { 
      objPen.StartCap = LineCap.Round; 
      objPen.EndCap = LineCap.Round; 
      _objGraphics.DrawLine(objPen, _objPointOne, _objPointTwo); 
     } 
    } 


    private void ActiveTimer() 
    { 
     if (m_IsTimerActive) 
      m_Timer.Start(); 
     else 
     { 
      m_Timer.Stop(); 
      m_ProgressValue = 0; 
     } 

     GenerateColorsPallet(); 
     Invalidate(); 
    } 

} 

編輯2:我添加了結果的圖像,你可以看到我的控制(LoadingCircle)的背景它的父(形式)相同,但按鈕保持隱藏狀態。

enter image description here

+1

你有沒有在[MSDN](http://msdn.microsoft.com/en-us/library/wk5b13s4%28v=vs.85%29.aspx採取甘德) – Adam

+1

請查看http://www.c-sharpcorner.com/uploadfile/Nildo/making-transparent-control-using-gdi-and-C-Sharp-updated-to-net-3-5/ – Monah

回答

0

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);