翼度科技»论坛 编程开发 .net 查看内容

C#自定义控件—旋转按钮

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
C#用户控件之旋转按钮

按钮功能:手自动旋转,标签文本显示、点击二次弹框确认(源码在最后边);


【制作方法】


  • 找到控件的中心坐标,画背景外环、内圆;再绘制矩形开关,进行角度旋转即可获得;
【关键节点】


  • No.1 获取中心坐标,思考要绘制图形的相对坐标、宽度、高度;
  • No.2 更改坐标系原点,以此原点为坐标,绘制矩形开关,再旋转指定角度
  1. //方法中获取原点
  2. Point centerPoint = GetCenterPoint();
  3. #region 获取中心原点
  4. private Point GetCenterPoint()
  5. {
  6.     if (this.height > this.width)
  7.     {
  8.         return new Point(this.width / 2, this.width / 2);
  9.     }
  10.     else
  11.     {
  12.         return new Point(this.height / 2, this.height / 2);
  13.     }
  14. }
  15. #endregion
复制代码
  1. //更改坐标系原点
  2. g.TranslateTransform(centerPoint.X, centerPoint.Y);
  3. //旋转指定角度
  4. if (switchStatus)
  5. {
  6.     g.RotateTransform(36.0f);
  7. }
  8. else
  9. {
  10.     g.RotateTransform(-36.0f);
  11. }
复制代码
【1】按钮的背景(外环、内圆)绘制方法与指示灯的方法一样;
注意:此坐标系以控件左上角为准
  1. //绘制外环—(Pen)-DrawEllipse
  2. p = new Pen(this.cirInColor, this.cirOutWidth);
  3. RectangleF rec = new RectangleF(this.cirOutGap, this.cirOutGap, (centerPoint.X - this.cirOutGap) * 2, (centerPoint.X - this.cirOutGap) * 2);
  4. g.DrawEllipse(p, rec);
  5. //填充内圆—(SolidBrush)-FillEllipse
  6. sb = new SolidBrush(this.cirInColor);
  7. rec = new RectangleF(this.cirInGap, this.cirInGap, (centerPoint.X - this.cirInGap) * 2, (centerPoint.X - this.cirInGap) * 2);
  8. g.FillEllipse(sb, rec);
复制代码
【2】绘制中间矩形及圆点,画刷填充指定区域(g.FillRectangle、g.FillEllipse)
注意:此坐标系以中心点为准
  1. //更改坐标系原点
  2. g.TranslateTransform(centerPoint.X, centerPoint.Y);
  3. //填充矩形开关
  4. rec = new RectangleF(-this.togWidth * 0.5f, this.togGap - centerPoint.Y, togWidth, (centerPoint.Y - togGap) * 2);
  5. g.FillRectangle(new SolidBrush(this.togColor), rec);
  6. //填充矩形开关圆点
  7. rec = new RectangleF(-this.togWidth * 0.5f + togForeGap, this.togGap - centerPoint.Y + togForeGap, togWidth - 2 * togForeGap, togForeHeight);
  8. g.FillEllipse(new SolidBrush(this.togForeColor), rec);
复制代码
【3】绘制文本,在指定的矩形中绘制指定的字符串(g.DrawString)
  1. //指定字符串
  2. rec = new RectangleF(this.width * 0.05f, 1, this.width, 20);
  3. g.DrawString(this.textLeft, this.textFont, new SolidBrush(this.textColor), rec, sf);
  4. rec = new RectangleF(this.width * 0.63f, 1, this.width, 20);
  5. g.DrawString(this.textRight, this.textFont, new SolidBrush(this.textColor), rec, sf);
复制代码
【4】创建鼠标点击事件,添加鼠标点击事件处理,在属性中触发事件(Event)
  1. #region 添加事件
  2. [Browsable(true)]
  3. [Category("操作_G")]
  4. [Description("双击进入事件")]
  5. public event EventHandler MouseDown_G;   //事件声明
复制代码
  1. //初始化函数添加鼠标点击事件处理
  2. this.MouseDown += Switch_MouseDown; ;
复制代码
  1. //鼠标点击事件处理逻辑
  2. private void Switch_MouseDown(object sender, MouseEventArgs e)
  3. {
  4.     DialogResult dr = MessageBox.Show("二次确认操作?", "提示您", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  5.     if (dr == DialogResult.OK)
  6.     {
  7.         SwitchStatus = !SwitchStatus; //此处属性值,不是字段
  8.     }
  9.     else return;
  10. }
  11. #endregion
复制代码
  1. //开关状态属性
  2. private bool switchStatus = false;
  3. [Browsable(true)]
  4. [Category("布局_G")]
  5. [Description("开关状态")]
  6. public bool SwitchStatus
  7. {
  8.      get { return switchStatus; }
  9.      set
  10.      {
  11.          switchStatus = value; this.Invalidate();
  12.          //激活触发事件
  13.          this.MouseDown_G?.Invoke(this, null);
  14.      }
  15. }
复制代码
备忘:指定默认事件(在应用时点击鼠标即可进入自定义事件,否则进入‘load’事件)
  1. [DefaultEvent("MouseDown_G")]
复制代码
最后生成


下一个:一个标题面板,方便用户界面的布局


【1】新建用户组件

【2】更改组件继承为Panel

【3】定义属性(标题的颜色、字体、高度;抬头背景色;边框颜色)
  1. private Font titleFont = new Font("微软雅黑", 12);
  2. [Browsable(true)]
  3. [Category("布局_G")]
  4. [Description("标题字体")]
  5. public Font TitleFont
  6. {
  7.     get { return titleFont; }
  8.     set
  9.     {
  10.         titleFont = value;
  11.         this.Invalidate();
  12.     }
  13. }
复制代码
【4】重绘画布
  1. //画外边框
  2. g.DrawRectangle(new Pen(this.colorBorder), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
  3. //填充抬头矩形
  4. RectangleF rec = new RectangleF(0.5f, 0.5f, this.Width - 2, this.titleHeight);
  5. g.FillRectangle(new SolidBrush(this.colorBack), rec);
  6. //文本绘制
  7. g.DrawString(this.titleText, this.titleFont, new SolidBrush(this.colorTitle), rec, sf);
复制代码
【5】备注说明

  • 初始化字体格式-需要再两个方法中定义文本对齐格式
  1. //字体对齐格式
  2. this.sf = new StringFormat();
  3. this.sf.Alignment = StringAlignment.Center;
  4. this.sf.LineAlignment = StringAlignment.Center;
  5. //指定控件大小
  6. this.Size = new System.Drawing.Size(300, 150);
复制代码
最后生成并应用

源码链接


(不想折腾的直接用Dll,如有更好的记得留言分享哦!代码有不足的也请大神指教)别忘点赞哦
https://pan.baidu.com/s/1QM_iZ-UMksPqwWo2ssS5Ow?pwd=ju01
End


来源:https://www.cnblogs.com/guoenshuo/p/18401032
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具