0

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

Igor Meszaros
  • 2,081
  • 2
  • 22
  • 46
  • is it ok for you to use png images? since it is little difficult to achieve the desired smoothness from drawing. – pushpraj Jun 16 '14 at 13:25
  • Smoothness is not a problem in the painting, i can smooth it with AntiAliasing. It is not smooth if i cut out a region from the control. Also if i would to use a png the transparent parts would show the color of the ToolStripDropDown, also if i stretch the control the png will change the way that bottom little point looks like. So i guess its not an option. – Igor Meszaros Jun 16 '14 at 13:58
  • Actually my option was not to use a region as it will always have such issue, but to have a image with transparency to make that smooth finish. alternative to png is to draw 32bit image with transparency and use it as the background instead of png. – pushpraj Jun 16 '14 at 14:04
  • 1
    What I did in the end that I used a Form instead of a ToolStripDropDown, and set a transparent background, and its working fine. – Igor Meszaros Jul 14 '14 at 04:51
  • great find! happy coding :) – pushpraj Jul 14 '14 at 05:19

0 Answers0