How to handle overlapping but transparent picture boxes?

Currently I want to make a small tool for my friend. The goal is to download a character from one of his favorite computer games from an Internet source and create a viewer for whom I upload image icons, etc. From the Internet.

While this is working pretty well, however, I came up with a problem right here:

enter image description here

As you can see - I drew the borders of the PictureBoxes - they overlap and blocks the display of another element (the one that is partially displayed in the second rectangular box).

Both are saved as .png files.

This is the code to create them:

PictureBox picture = new PictureBox
{
    Name = "pictureBox"+item.Key,
    Size = new Size(pictureBoxWidth, pictureBoxHeight),
    Location = new Point(pictureBoxLocationX, pictureBoxLocationY),
    Visible = true,
    Image = itemIcon,
    BackColor = Color.Transparent,
    BorderStyle = BorderStyle.FixedSingle
};

The coordinates are fine, also the image is fine. However, it is quite large, even if the icons, such as completely visible, do not even need a huge box.

:

64px 128px

, , ? - , , - - , .

Parent-, , , , .

+4
1

PictureBox, , - . , , Winforms. , . , , - PictureBoxes.

, OnPaintBackground() . , . . , .

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

class PictureBoxEx : PictureBox {
    protected override void OnPaintBackground(PaintEventArgs e) {
        base.OnPaintBackground(e);
        for (int index = this.Parent.Controls.Count - 1; index > this.Parent.Controls.GetChildIndex(this); --index) {
            var ctl = this.Parent.Controls[index] as PictureBox;
            if (ctl == null) continue;
            var clip = ctl.RectangleToClient(this.RectangleToScreen(this.DisplayRectangle));
            clip.Intersect(ctl.DisplayRectangle);
            if (clip.Width == 0 || clip.Height == 0) continue;
            var save = e.Graphics.Save();
            e.Graphics.TranslateTransform(ctl.Left - this.Left, ctl.Top - this.Top);
            using (var rgn = new Region(clip)) {
                e.Graphics.Clip = rgn;
                InvokePaintBackground(ctl, e);
                InvokePaint(ctl, e);
            }
            e.Graphics.Restore(save);
        }               
    }

    protected override CreateParams CreateParams {
        get {
            const int WS_EX_TRANSPARENT = 0x20;
            var cp = base.CreateParams;
            cp.ExStyle |= WS_EX_TRANSPARENT;
            return cp;
        }
    }
}
+4

Source: https://habr.com/ru/post/1608162/


All Articles