Compact Framework上のGDI RoundRect:角丸長方形の外側を透明にします
-
22-07-2019 - |
質問
この例に従ってRoundRect GDI関数を使用して角丸長方形を描画しています。.NETCFカスタムコントロール: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
GDI操作をP / Invokingしているので、それを続行することもできます。透明度情報を含む画像を描画している場合は、 alphaを使用できます。ブレンド、ただしこの場合は、「ボタン」全体を描画する必要があります。別のバッファに移動してから、P / Invoke MaskBlt でコピーしますフォームのDC(カラーキー透明度を使用するときにCFが実行することです)。 ここにデスクトップの例がありますが、プロセスは同じ。
所属していません StackOverflow