0

I've created a transparent control as suggested here

public partial class TransparentControl : UserControl
{
    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)
        {
            e.Graphics.DrawImage(_image, new Rectangle((Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2), _image.Width, _image.Height));
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {}

        public void Redraw()
        {
            RecreateHandle();
        }

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

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

The problem is that if i overlap two TransparentControl object, the one that stays on top is not being rendered (or maybe it's being rendered but is not visible at all). Is there something to add to the control to fix this?

Because using PictureBox for the top image works, but on Linux with Mono its not rendering alpha pixels. That's why I need to use this TransparentControl.

Thanks everyone.

Community
  • 1
  • 1
Dario Emerson
  • 127
  • 4
  • 13
  • I presume this is Winforms? Transparency in GDI is a hack where it just draws the color underneath the control – Sayse Nov 04 '14 at 07:55
  • Yes is Winforms. But lets i got a circle to draw on a square. Both got different colors. It should render them correctly. Instead i only see the square – Dario Emerson Nov 04 '14 at 07:59
  • Its a little hard to distinguish the problem in the code here but its possible that there is a refresh between the drawing of both controls – Sayse Nov 04 '14 at 08:02
  • Yes there's a refresh timer in both controls – Dario Emerson Nov 04 '14 at 08:03
  • If you want quality looking controls in winforms its better to draw everything yourself via paint. Works great there – Vajura Nov 04 '14 at 08:27
  • But I'm actually using OnPaint method, then calling DrawImage in it. It's not working. – Dario Emerson Nov 04 '14 at 16:16

0 Answers0