2016-08-12 2 views
-1

this 또는 this과 유사한 토글 버튼을 만들고 싶습니다. 애니메이션은 나에게 중요하지 않습니다. 아래 코드로 토글 버튼을 만들려고했습니다. 그러나 나는 부드러운 곡선과 가장자리가 될 수 없습니다. 저는 C# UI 디자인에서 Windows 양식 응용 프로그램과 전체 초보자를 사용하고 있습니다.버튼의 매끄러운 곡선

제 질문은 커브를 부드럽게 만들 수 있습니까? 아니면 여전히 이와 같이 유지할 수 있습니까? 두 번째 링크에서 코드를 가져 왔지만 여전히 버튼이 부드럽게 보이지 않았습니다.

또한 사용 -

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Drawing.Drawing2D; 

namespace MyWorkspace 
{ 
    public class ToggleButton : Control 
    { 
     #region variables   
     public enum ButtonState { ON, OFF }; 
     private ButtonState toggleState = ButtonState.OFF; 
     private Rectangle contentRectangle = Rectangle.Empty; 
     #endregion 

     #region properties 
     public ButtonState ToggleState 
     { 
      get 
      { 
       return toggleState; 
      } 
      set 
      { 
       if (toggleState != value) 
       {      
        toggleState = value; 
        Invalidate(); 
        this.Refresh(); 
       } 
      } 
     } 
     #endregion 

     public ToggleButton() : base() 
     { 
      this.MinimumSize = new Size(50, 25); 
      this.MaximumSize = new Size(50, 25); 
      contentRectangle = new Rectangle(0, 0, this.Width, this.Height); 
      this.BackColor = Application.RenderWithVisualStyles ? Color.Azure : this.Parent.BackColor; 
     }   

     // Draw the large or small button, depending on the current state. 
     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 

      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
      e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;    

      Rectangle rect = new Rectangle(0, contentRectangle.Y, contentRectangle.Height, contentRectangle.Height);    

      GraphicsPath gp = new GraphicsPath(); 
      int d = this.Height; 
      gp.AddArc(contentRectangle.X, contentRectangle.Y, d, d, 180, 90); 
      gp.AddArc(contentRectangle.X + contentRectangle.Width - d, contentRectangle.Y, d, d, 270, 90); 
      gp.AddArc(contentRectangle.X + contentRectangle.Width - d, contentRectangle.Y + contentRectangle.Height - d, d, d, 0, 90); 
      gp.AddArc(contentRectangle.X, contentRectangle.Y + contentRectangle.Height - d, d, d, 90, 90); 

      this.Region = new Region(gp); 

      Rectangle ar2 = new Rectangle(rect.X, contentRectangle.Y, (rect.X + rect.Width/2) + contentRectangle.Right, contentRectangle.Height); 

      LinearGradientBrush br; 
      Rectangle ellipse_rect; 
      if (toggleState == ButtonState.ON) 
      { 
       br = new LinearGradientBrush(ar2, Color.FromArgb(0, 127, 234), Color.FromArgb(96, 174, 241), LinearGradientMode.Vertical);     
       ellipse_rect = new Rectangle(contentRectangle.Right - (contentRectangle.Height -2), 
        contentRectangle.Y, contentRectangle.Height - 4, contentRectangle.Height);     
      } 
      else 
      {     
       br = new LinearGradientBrush(ar2, Color.FromArgb(120, 120, 120), Color.Silver, LinearGradientMode.Vertical);     
       ellipse_rect = rect; 
      } 

      e.Graphics.FillRectangle(br, ar2); 
      e.Graphics.DrawEllipse(new Pen(Color.Gray, 2f), ellipse_rect); 
      LinearGradientBrush br2 = new LinearGradientBrush(rect, Color.White, Color.Silver, LinearGradientMode.Vertical); 
      e.Graphics.FillEllipse(br2, ellipse_rect); 
      Color c = this.Parent != null ? this.Parent.BackColor : Color.White; 
      e.Graphics.DrawPath(new Pen(c, 2f), gp);    
     }   

     protected override void OnClick(EventArgs e) 
     { 
      if (toggleState == ButtonState.ON) 
       toggleState = ButtonState.OFF;        
      else 
       toggleState = ButtonState.ON; 

      Invalidate(); 
     }   
    } 
} 

답변

1

이 솔루션의 모습의 이미지를 게시하시기 바랍니다,하지만 당신은 설명했다 것과에게 내가 추측하는 것 -

여기
e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 

버튼에 대한 전체 코드입니다 앤티 앨리어싱을 사용할 수 없습니다. 앤티 앨리어싱을 사용하는 방법에 대한 Here is a MSDN article 응용 프로그램에서 많은 컨트롤의 모양을 변경하려는 경우 Windows Presentation Foundation 또는 Universal Windows Platform을 살펴볼 수 있습니다. Windows Forms와 달리 WPF와 UWP는 모두 XAML을 사용하여 디자인 된 벡터 기반 인터페이스 레이어입니다. WPF 및 UWP가 설계 시스템을 구현하는 방식 때문에 응용 프로그램의 모양과 느낌을 완전히 사용자 정의하는 것이 매우 쉽습니다. 또한 두 플랫폼 모두 애니메이션 기능을 내장하고 있습니다.