Igor Meszaros
Igor Meszaros

Reputation: 2127

ToolStripDropDown transparent background and smooth Region

I'm trying to create a custom popup window in my winforms project, which looks like so:

enter image description here

The problem with this is that the edges are smooth... I'm achieving this like so:

public partial class BubblePopup : ToolStripDropDown
{
    private const int BORDERWIDTH = 6;
    private SolidBrush _backgroundBrush;
    private int _borderRadius = 20;
    public BubblePopup()
    {
        this.BackColor = Color.Transparent;
        InitializeComponent();
        //Method 1
        Region = new Region(ControlUtilities.CreateBubblePath(new Rectangle(BORDERWIDTH - 1, BORDERWIDTH - 1, ClientSize.Width - (BORDERWIDTH * 2), ClientSize.Height - (BORDERWIDTH * 2)), _borderRadius));
        /////////////
        _backgroundBrush = new SolidBrush(Color.Blue);
    }

    //Method 1
    protected override void OnSizeChanged(EventArgs e)
    {
        Region = new Region(ControlUtilities.CreateBubblePath(new Rectangle(BORDERWIDTH - 1, BORDERWIDTH - 1, ClientSize.Width - (BORDERWIDTH * 2), ClientSize.Height - (BORDERWIDTH * 2)), _borderRadius));
        base.OnSizeChanged(e);
    }
    ////////

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | 0x20;
            return cp;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    }
}

If I don't use the code enclosed by Method1 comments and I'm setting the background to transparent and setting the CreateParams flag to support transparency im getting the following result(I also tried overriding the OnPaintBackground event as well not to call the base.OnPaintBackground I got a black background then):

enter image description here

So I would either need a popup with my custom path with smooth region, or a transparent background would also work for me.

Also here's the code how I'm showing the popup:

private GeneralPopup _popupItem = new GeneralPopup();
        private ToolStripControlHost _popupControlHost;
        private BubblePopup _popup;
        public Form1()
        {
            InitializeComponent();
            _popupControlHost = new ToolStripControlHost(_popupItem);
            _popupControlHost.Padding = new Padding(0);
            _popupControlHost.Margin = new Padding(0);
            _popupControlHost.AutoSize = false;
            _popupControlHost.BackColor = Color.Transparent;

            _popup = new BubblePopup();
            _popup.Padding = new Padding(0);
            _popup.Margin = new Padding(0);
            _popup.AutoSize = true;
            _popup.DropShadowEnabled = false;
            _popup.Items.Add(_popupControlHost);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            _popup.Show(button1,10,10);
        }

Thanks in advance

Upvotes: 0

Views: 619

Answers (0)

Related Questions