2015-01-13 49 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);