GDI RoundRect on Compact Framework: make rounded rectangle outside of transparent
I am using GDI's RoundRect function to draw a rounded rectangle in the following example: .NET CF Custom Control: RoundedGroupBox
Since all controls are square, it also draws corners outside the rounded rectangle. How can I make this space outside the transparent rectangle?
OnPaint method:
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 has:
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!
source to share
With standard guided operations, you need to make the "outer" color (outside the rounded portion) of a specific color (magenta is common), then use SetColorKey to set that color to transparent.
This MSDN article has the basics on how you will achieve it.
EDIT 1
Since you are using P / Invoking for your GDI operations, you can proceed with this as well. If you are drawing an image with transparency information, you can use alpha blending , but in that case, you need to draw all the "button" text in a separate buffer and then P / Invoke MaskBlt to copy it to the DC shape (which is what CF does when you using colorkey transparency) Here is a desktop example but the process is the same.
source to share