1
둥근 모서리와 그라데이션 색이있는 사용자 지정 콤보 상자를 만들려고합니다. OnPaint
메서드를 재정 의하여 동일한 기능을 Button
에 구현했습니다. 하지만 ComboBox
에서는 작동하지 않습니다. 어떤 도움을 주시면 감사하겠습니다.Windows Forms에서 모서리가 둥근 사용자 지정 ComboBox 만들기
내가 OnPaint
를 오버라이드 (override)를 위해 사용하고 코드는 아래와 같습니다 :
protected override void OnPaint(PaintEventArgs paintEvent)
{
Graphics graphics = paintEvent.Graphics;
SolidBrush backgroundBrush = new SolidBrush(this.BackColor);
graphics.FillRectangle(backgroundBrush, ClientRectangle);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rectangle = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
GraphicsPath graphicsPath = RoundedRectangle(rectangle, cornerRadius, 0);
Brush brush = new LinearGradientBrush(rectangle, gradientTop, gradientBottom, LinearGradientMode.Horizontal);
graphics.FillPath(brush, graphicsPath);
rectangle = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 100);
graphicsPath = RoundedRectangle(rectangle, cornerRadius, 2);
brush = new LinearGradientBrush(rectangle, gradientTop, gradientBottom, LinearGradientMode.Horizontal);
graphics.FillPath(brush, graphicsPath);
}
private GraphicsPath RoundedRectangle(Rectangle rectangle, int cornerRadius, int margin)
{
GraphicsPath roundedRectangle = new GraphicsPath();
roundedRectangle.AddArc(rectangle.X + margin, rectangle.Y + margin, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRectangle.AddArc(rectangle.X + rectangle.Width - margin - cornerRadius * 2, rectangle.Y + margin, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRectangle.AddArc(rectangle.X + rectangle.Width - margin - cornerRadius * 2, rectangle.Y + rectangle.Height - margin - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRectangle.AddArc(rectangle.X + margin, rectangle.Y + rectangle.Height - margin - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRectangle.CloseFigure();
return roundedRectangle;
}
_ "하지만 Combobox에서는 작동하지 않습니다."_ 좀 더 자세하게 설명하십시오. * 어떻게 작동하지 않습니까? – stakx
전체 코드를 입력하십시오. "RoundedRectangle"메서드는 일부 매개 변수로 선언되지 않습니다. – cansik
@stakx 사용자 지정 컨트롤을 만들고 싶었습니다. Button의 OnPaint() 이벤트를 재정의했습니다. 그것은 완벽하게 작동했습니다. 나는 Combobox를 사용자 정의하기 위해 동일한 코드를 사용했다. 하지만 콤보 박스의 경우 무시가 발생하지 않는 것처럼 보입니다. 필자는 WinForms를 처음 접한 이래로 ComboBox를 사용자 정의하는 방식이 올바른지 확신 할 수 없습니다. –