Winforms semi-transparent PNG over semi-transparent PNG

前端 未结 2 1890
梦毁少年i
梦毁少年i 2020-12-04 03:25

I think I must be missing something obvious, but I\'m unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain

相关标签:
2条回答
  • 2020-12-04 03:39

    Sorry, I just found this... once I decided to Google for "winforms transparent panel" instead of the searches I was doing before, the TransPictureBox example show seems to do exactly what I need:

    Transparency Problem by Overlapped PictureBox's at C#

    Looks like there are 2 parts to it:

    1. Set WS_EX_TRANSPARENT for the window style

    2. Override the "draw background" method (or optionally could probably make the control style Opaque).

    0 讨论(0)
  • 2020-12-04 04:06

    The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.

    The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:

        public Form1() {
            InitializeComponent();
            pictureBox1.Controls.Add(pictureBox2);
            pictureBox2.Location = new Point(0, 0);
            pictureBox2.BackColor = Color.Transparent;
        }
    

    Don't hesitate to use OnPaint() btw.

    0 讨论(0)
提交回复
热议问题