ToolStripDropDown透明背景和平滑区域

时间:2014-06-16 11:20:50

标签: c# winforms popup transparent toolstripdropdown

我正在尝试在winforms项目中创建一个自定义弹出窗口,如下所示:

enter image description here

这个问题是边缘光滑...... 我这样做是这样的:

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)
    {
    }
}

如果我不使用Method1注释所包含的代码,并且我将背景设置为透明并将CreateParams标志设置为支持透明度,则会得到以下结果(我也尝试重写OnPaintBackground事件以及不调用base.OnPaintBackground我得到了黑色背景然后):

enter image description here

所以我要么需要一个带有平滑区域的自定义路径的弹出窗口,或者透明背景也适合我。

此处的代码是我如何显示弹出窗口:

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);
        }

提前致谢

0 个答案:

没有答案