2016-08-12 129 views
-1

我想創建類似於thisthis的切換按鈕。動畫對我無關緊要。我試着用下面的代碼創建一個切換按鈕。但是我無法讓它變得光滑的曲線和邊緣。我正在使用Windows窗體應用程序和C#UI設計中的初學者。按鈕中的平滑曲線

我的問題是可以使曲線平滑,還是會保持這樣?我在第二個鏈接中導入了該代碼,但該按鈕仍然不平滑。

我也用 -

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

這裏是按鈕全碼 -

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

請發表您的解決方案是什麼樣的形象,但是從你的描述我猜您沒有啓用抗鋸齒功能。有關如何啓用抗鋸齒的Here is a MSDN article。如果您要改變應用程序中許多控件的外觀,您可能需要查看Windows Presentation Foundation或新的Universal Windows Platform。 與Windows窗體相比,WPF和UWP都是使用XAML設計的基於矢量的界面層。由於WPF和UWP實現其設計系統的方式,因此完全自定義應用程序的外觀和感覺非常容易。另外這兩個平臺都內置了動畫功能。