문제

이 예제 : .NET CF 사용자 정의 컨트롤 : RoundedGroupbox에 이어 RoundRect GDI 함수를 사용하고 있습니다.

모든 컨트롤은 정사각형이므로 둥근 사각형 외부의 모서리를 그립니다. 이 공간이 사각형 외부에 투명하게 남겨 두도록하려면 어떻게해야합니까?

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

고맙습니다!

도움이 되었습니까?

해결책

표준 관리 작업을 사용할 때는 "외부"색상 (둥근 부품 외부)을 특정 색상으로 만들어야합니다 (Magenta는 일반적인 것입니다). setcolorkey 그 색상을 투명하게 설정합니다.

이 MSDN 기사 당신이 그것을 달성하는 방법에 대한 기본 사항이 있습니다.

편집 1

P/GDI 작업을 호출하므로 계속 해서도 계속할 수 있습니다. 투명성 정보가 포함 된 이미지를 그리는 경우 사용할 수 있습니다. 알파 블렌딩, 그러나이 경우 전체 "버튼"을 별도의 버퍼에 그린 다음 p/호출해야합니다. MaskBlt 양식의 DC에 복사하려면 (CF가 Colorkey Transparency를 사용할 때 CF가하는 일). 다음은 데스크탑 예제입니다, 그러나 프로세스는 동일합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top