亲宝软件园·资讯

展开

C#滑动开关效果 C#实现滑动开关效果

qq53716684 人气:0
想了解C#实现滑动开关效果的相关内容吗,qq53716684在本文为您仔细讲解C#滑动开关效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,滑动开关,下面大家一起来学习吧。

C#重绘checkbox生成滑动开关,供大家参考,具体内容如下

通过调用checkbox控件的paint事件,在重绘事件里判断checked属性,如果选中绘制选中图形,如果未选中绘制未选中图形。

效果图:

绘制圆角矩形方法:

 /// <summary>
        /// 填充圆角矩形
        /// </summary>
        /// <param name="g"></param>
        /// <param name="brush"></param>
        /// <param name="rect"></param>
        /// <param name="cornerRadius"></param>
        public static void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius)
        {
            using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
            {
                g.FillPath(brush, path);
            }
        }
        /// <summary>
        /// 圆角矩形路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="cornerRadius"></param>
        /// <returns></returns>
        internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
        {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
            roundedRect.CloseFigure();
            return roundedRect;
        }

重绘代码:

private void RectangleCheckBoxButton(object sender, PaintEventArgs e)
{
            CheckBox rButton = (CheckBox)sender;
            Graphics g = e.Graphics;
            g.Clear(this.BackColor);

            Rectangle RoundRect = new Rectangle(0, 0, 50, 30);            
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //FillRoundRectangle(g, Brushes.White, radioButtonrect, 15);
            

            if (rButton.Checked)
            {
 
                Color color =Color.FromArgb( 55, 197, 90);
                Brush b1 = new SolidBrush(color);
                FillRoundRectangle(g, b1, RoundRect, 15);
                
                using (Pen pen = new Pen(Color.FromArgb(255, 255, 255)))
                {             
                    FillRoundRectangle(g, Brushes.White, new Rectangle(22, 2,26, 26), 13);
                }
            }
            else
            {
                using (Pen pen = new Pen(Color.FromArgb(255, 255, 255)))
                {
                    FillRoundRectangle(g, Brushes.Silver,  RoundRect, 15);
                    FillRoundRectangle(g,Brushes.White, new Rectangle(2, 2, 26, 26), 13);
                    
                }
            }
            Font f = new Font("微软雅黑", 12);
            g.DrawString(((CheckBox)sender).Text,f ,  Brushes.White, new PointF(60, 6));
}

调用:

private void checkBox1_Paint(object sender, PaintEventArgs e)
{
 RectangleCheckBoxButton(sender, e);
}

加载全部内容

相关教程
猜你喜欢
用户评论