GDI RoundRect on Compact Framework: make rounded rectangle's outside transparent.

Posted by VansFannel on Stack Overflow See other posts from Stack Overflow or by VansFannel
Published on 2009-09-18T15:39:32Z Indexed on 2010/03/28 2:23 UTC
Read the original article Hit count: 555

Hello!

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!

© Stack Overflow or respective owner

Related posts about compact-framework

Related posts about transparency