2013-07-22 3 views
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; 
} 
+2

_ "하지만 Combobox에서는 작동하지 않습니다."_ 좀 더 자세하게 설명하십시오. * 어떻게 작동하지 않습니까? – stakx

+0

전체 코드를 입력하십시오. "RoundedRectangle"메서드는 일부 매개 변수로 선언되지 않습니다. – cansik

+0

@stakx 사용자 지정 컨트롤을 만들고 싶었습니다. Button의 OnPaint() 이벤트를 재정의했습니다. 그것은 완벽하게 작동했습니다. 나는 Combobox를 사용자 정의하기 위해 동일한 코드를 사용했다. 하지만 콤보 박스의 경우 무시가 발생하지 않는 것처럼 보입니다. 필자는 WinForms를 처음 접한 이래로 ComboBox를 사용자 정의하는 방식이 올바른지 확신 할 수 없습니다. –

답변

3

나는이 문제에 대한 해결책을 찾을 수 있었다. 실제로 문제는 OnPaint 이벤트가 Combobox에 호출되지 않는 것이 었습니다. 그래서 나는 다음과 같은 변화를해야했다.

public CustomComboBox() 
{ 
    InitializeComponent(); 
    SetStyle(ControlStyles.UserPaint, true); 
} 

나는의 OnPaint() 이벤트가 호출받을 수 있도록 생성자 내부에서하는 SetStyle() 메서드를 호출했다. OnPaint override is never called

같은 솔루션은 사용자 정의 텍스트 상자에 사용할 수 있습니다

나는 helful이 게시물을 발견했다.