Question

I'm using the RoundRect GDI function to draw a rounded rectangle following this example: .NET CF Custom Control: RoundedGroupBox

Because all controls are square, it also draw the corners outside of the rounded rectangle. How can I make this space left outside the rectangle transparent?

The OnPaint method is:

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

An the OnPaintBackground is:

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

Thank you!

Was it helpful?

Solution

When using standard managed operations, you need to make the "outer" color (that outside the rounded part) a specific color (magenta is a common one) then use SetColorKey to set that color as transparent.

This MSDN article has the basics on how you'd achieve it.

EDIT 1

Since you are P/Invoking your GDI operations, you could also continue with that. If you were drawing an image with transparency info, you could use alpha blending, but in this case you need to draw your entire "button" to a separate buffer and then P/Invoke MaskBlt to copy it to the Form's DC (which is what the CF is doing when you use colorkey transparency). Here's a desktop example, but the process is the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top