我使用的RoundRect GDI功能画一个圆形的矩形下这个例子:.净CF定控制:RoundedGroupBox

因为所有的控制方,它也提请角外的圆形。我怎么能让这一空间留在外面的矩形透明?

该OnPaint方法是:

protected override void OnPaint(PaintEventArgs e)
        {
            int outerBrushColor = HelperMethods.ColorToWin32(m_outerColor);
            int innerBrushColor = HelperMethods.ColorToWin32(this.BackColor);

            IntPtr hdc = e.Graphics.GetHdc();
            try
            {
                IntPtr hbrOuter = NativeMethods.CreateSolidBrush(outerBrushColor);
                IntPtr hOldBrush = NativeMethods.SelectObject(hdc, hbrOuter);
                NativeMethods.RoundRect(hdc, 0, 0, this.Width, this.Height, m_diametro, m_diametro);
                IntPtr hbrInner = NativeMethods.CreateSolidBrush(innerBrushColor);
                NativeMethods.SelectObject(hdc, hbrInner);
                NativeMethods.RoundRect(hdc, 0, 18, this.Width, this.Height, m_diametro, m_diametro);
                NativeMethods.SelectObject(hdc, hOldBrush);
                NativeMethods.DeleteObject(hbrOuter);
                NativeMethods.DeleteObject(hbrInner);
            }
            finally
            {
                e.Graphics.ReleaseHdc(hdc);
            }

            if (!string.IsNullOrEmpty(m_roundedGroupBoxText))
            {
                Font titleFont = new Font("Tahoma", 9.0F, FontStyle.Bold);
                Brush titleBrush = new SolidBrush(this.BackColor);
                try
                {
                    e.Graphics.DrawString(m_roundedGroupBoxText, titleFont, titleBrush, 14.0F, 2.0F);
                }
                finally
                {
                    titleFont.Dispose();
                    titleBrush.Dispose();
                }
            }

            base.OnPaint(e);
        }

一的OnPaintBackground是:

protected override void OnPaintBackground(PaintEventArgs e)
{
   if (this.Parent != null)
   {
        SolidBrush backBrush = new SolidBrush(this.Parent.BackColor);
        try
        {
            e.Graphics.FillRectangle(backBrush, 0, 0, this.Width, this.Height);
        }
        finally
        {
            backBrush.Dispose();
        }
    }
}

谢谢你!

有帮助吗?

解决方案

在使用标准管理操作,你需要做的"外"的色彩(外的圆润的一部分)一个特定颜色(深红色是一个常见的一种),那么使用 SetColorKey 设置这颜色为透明。

这MSDN文章 已经基本上你如何实现这一目标。

编辑1

因为你是P/调用你的GDI行动,你还可以继续。如果你画的图像与透明度信息,可以使用 alpha blending, 但在这种情况下你需要借你的整个"按键"的单独的缓冲区,然后P/调用 MaskBlt 要复制它的形式是直流(它是什么CF是这样做的时候你用透明色的透明度). 这里有一个桌面例子, 但该进程也是同样的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top