【问题标题】:Smooth curves in button按钮中的平滑曲线
【发布时间】:2016-12-19 15:49:39
【问题描述】:

我想创建一个类似于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();
        }        
    }
}

【问题讨论】:

    标签: c# user-interface button


    【解决方案1】:

    请发布您的解决方案的图片,但根据您的描述,我猜您没有启用抗锯齿功能。 Here is a MSDN article 关于如何启用抗锯齿。如果您要更改应用程序中许多控件的外观,您可能需要查看Windows Presentation Foundation 或新的Universal Windows Platform。 与 Windows 窗体相比,WPF 和 UWP 都是使用 XAML 设计的基于矢量的界面层。由于 WPF 和 UWP 实现其设计系统的方式,完全自定义应用程序的外观和感觉非常容易。此外,这两个平台都内置了动画功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多