当前位置:   article > 正文

C#中添加自制控件_c# 自定义控件

c# 自定义控件

先看最终成品为一个可以在XY坐标系下,进行点位显示的控件

制作过程:

1.添加用户控件类

2.进入代码编辑页面

套路式开发:继承 UserControl     public partial class PointCurve : UserControl

初始化过程:

  1. public PointCurve()
  2. {
  3. InitializeComponent();
  4. //设置控件样式
  5. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); //减少闪烁
  6. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); //启用双缓冲技术
  7. this.SetStyle(ControlStyles.ResizeRedraw, true); //根据窗口大小的重新绘制指令
  8. this.SetStyle(ControlStyles.Selectable, true);//激活与用户的交互响应
  9. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景色
  10. this.SetStyle(ControlStyles.UserPaint, true); //用户自己控制绘制
  11. }

画图类绘制对象的三大件:

       //画布
        private Graphics g;
        //画笔
        private Pen p;
        //画刷
        private SolidBrush sb;

控件属性:(显示在加载进入工具箱后的属性界面)

  1. /****************原点****************************************/
  2. private int orginGap = 20; //内部调用数值
  3. [Browsable(true)] //在属性界面显示
  4. [Category("自定义属性")] //分组类别
  5. [Description("原点坐标")] //显示名称
  6. public int OriginGap //外部用户交互变量
  7. {
  8. get { return orginGap; }
  9. set
  10. {
  11. if (value <= 0)
  12. {
  13. return;
  14. }
  15. orginGap = value;
  16. this.Invalidate(); //外部改变后重新绘制
  17. }
  18. }
  19. /****************X轴最大值****************************************/
  20. private float maxXAxis = 70000.0f;
  21. [Browsable(true)]
  22. [Category("自定义属性")]
  23. [Description("X轴最大值")]
  24. public float MaxXAxis
  25. {
  26. get { return maxXAxis; }
  27. set { maxXAxis = value;
  28. this.Invalidate(); //重新绘制
  29. }
  30. }
  31. /****************Y轴最大值****************************************/
  32. private float maxYAxis = 70000.0f;
  33. [Browsable(true)]
  34. [Category("自定义属性")]
  35. [Description("Y轴最大坐标值")]
  36. public float MaxYAxis
  37. {
  38. get { return maxYAxis; }
  39. set { maxYAxis = value;
  40. this.Invalidate(); //重新绘制
  41. }
  42. }
  43. /****************位置坐标1X轴坐标****************************************/
  44. [Browsable(true)]
  45. [Category("自定义属性")]
  46. [Description("位置坐标1X轴坐标")]
  47. public int ReclaimerXAxis
  48. {
  49. get { return reclaimerXAxis; }
  50. set
  51. {
  52. reclaimerXAxis = value;
  53. this.Invalidate();
  54. }
  55. }
  56. /****************位置坐标1Y轴坐标****************************************/
  57. private int reclaimerYAxis = 0;
  58. [Browsable(true)]
  59. [Category("自定义属性")]
  60. [Description("位置坐标1Y轴坐标")]
  61. public int ReclaimerYAxis
  62. {
  63. get { return reclaimerYAxis; }
  64. set
  65. {
  66. reclaimerYAxis = value;
  67. this.Invalidate();
  68. }
  69. }
  70. ……想显示多少个坐标点可以自由添加
  71. /****************颜色属性添加****************************************/
  72. private Color reclaimerColor = Color.Blue;
  73. [Browsable(true)]
  74. [Category("自定义属性")]
  75. [Description("坐标点颜色")]
  76. public Color ReclaimerColor
  77. {
  78. get { return reclaimerColor; }
  79. set
  80. {
  81. reclaimerColor = value;
  82. this.Invalidate();
  83. }
  84. }
  85. /****************字符串属性添加****************************************/
  86. private string reclaimerStr = "位置1";
  87. [Browsable(true)]
  88. [Category("自定义属性")]
  89. [Description("位置1字符串")]
  90. public string ReclaimerStr
  91. {
  92. get { return reclaimerStr; }
  93. set
  94. {
  95. reclaimerStr = value;
  96. this.Invalidate();
  97. }
  98. }
  99. /****************绘制点直径属性添加****************************************/
  100. private int pointdiameter = 5;
  101. [Browsable(true)]
  102. [Category("自定义属性")]
  103. [Description("绘制点直径")]
  104. public int Pointdiameter
  105. {
  106. get { return pointdiameter; }
  107. set
  108. {
  109. pointdiameter = value;
  110. this.Invalidate();
  111. }
  112. }

以上信息都是属性信息,不涉及逻辑,属于为了实现自己绘制目的而定义的成员变量

接下来重写的OnPaint函数是真正的绘制过程,每次用户改变属性信息时,由于执行了  this.Invalidate();,都会后台去调用OnPaint函数,从而实现位置更新。

下面函数中涉及一些计算过程,其实是由于窗体绘图默认原点是左上角,而我们习惯的坐标系原点的左下角,里面的计算过程是为了将我们输入的位置坐标进行转换的过程,无需过分纠结,理解原理,this.Height.,this.with为实际的绘图画布大小

  1. protected override void OnPaint(PaintEventArgs e) //执行重绘指令时会被调用
  2. {
  3. base.OnPaint(e);
  4. //绘制过程
  5. //绘制画布
  6. g = e.Graphics;
  7. //设置画布
  8. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
  9. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  10. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  11. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  12. //正式绘制
  13. //设置画笔
  14. p = new Pen(Color.Black, 1.5f);
  15. p.CustomEndCap = new AdjustableArrowCap(p.Width * 3, p.Width * 4, true); //结束端采用箭头样式
  16. //绘制X轴
  17. g.DrawLine(p, new Point(orginGap, this.Height - orginGap), new Point(this.Width - orginGap, this.Height - orginGap));
  18. //绘制Y轴
  19. g.DrawLine(p, new Point(orginGap, this.Height - orginGap), new Point(orginGap, orginGap));
  20. //绘制原点
  21. StringFormat sf = new StringFormat();
  22. sf.Alignment = StringAlignment.Center;//字符中心居中
  23. sf.LineAlignment = StringAlignment.Center;//线中心居中
  24. g.DrawString("0", this.Font, new SolidBrush(Color.Black), new Rectangle(0, this.Height - orginGap, orginGap, orginGap), sf);
  25. //绘制X轴最大
  26. g.DrawString(maxXAxis.ToString(), this.Font, new SolidBrush(Color.Black), new Rectangle(this.Width -50, this.Height -orginGap+5, 50, orginGap), sf);
  27. //绘制Y轴最大
  28. g.DrawString(maxYAxis.ToString(), this.Font, new SolidBrush(Color.Black), new Rectangle(0, 0, 50, orginGap), sf);
  29. //绘制取料点
  30. float reclaimerX = (this.Width - 2 * orginGap) / maxXAxis * reclaimerXAxis + orginGap;
  31. float reclaimerY = this.Height - ((this.Height - 2 * orginGap) / maxYAxis * reclaimerYAxis + orginGap);
  32. g.FillEllipse(new SolidBrush(reclaimerColor), new RectangleF(reclaimerX - 0.5f * pointdiameter, reclaimerY - 0.5f * pointdiameter, pointdiameter, pointdiameter));
  33. g.DrawString(reclaimerStr, this.Font, new SolidBrush(reclaimerColor), new RectangleF(reclaimerX - 25.0f, reclaimerY - 20.0f, 50.0f, 20.0f), sf);
  34. //绘制加工点
  35. float processX = (this.Width - 2 * orginGap) / maxXAxis * processXAxis + orginGap;
  36. float processY = this.Height - ((this.Height - 2 * orginGap) / maxYAxis * processYAxis + orginGap);
  37. g.FillEllipse(new SolidBrush(processColor), new RectangleF(processX - 0.5f * pointdiameter, processY - 0.5f * pointdiameter, pointdiameter, pointdiameter));
  38. g.DrawString(processStr, this.Font, new SolidBrush(processColor), new RectangleF(processX - 25.0f, processY - 20.0f, 50.0f, 20.0f), sf);
  39. //绘制出料点
  40. float outletX = (this.Width - 2 * orginGap) / maxXAxis * outletXAxis + orginGap;
  41. float outletY = this.Height - ((this.Height - 2 * orginGap) / maxYAxis * outletYAxis + orginGap);
  42. g.FillEllipse(new SolidBrush(outletColor), new RectangleF(outletX - 0.5f * pointdiameter, outletY - 0.5f * pointdiameter, pointdiameter, pointdiameter));
  43. g.DrawString(outletStr, this.Font, new SolidBrush(outletColor), new RectangleF(outletX - 25.0f, outletY - 20.0f, 50.0f, 20.0f), sf);
  44. }

下面附完整代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Drawing2D;
  11. namespace WindowsFormsApp1
  12. {
  13. public partial class PointCurve : UserControl
  14. {
  15. public PointCurve()
  16. {
  17. InitializeComponent();
  18. //设置控件样式
  19. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); //减少闪烁
  20. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); //启用双缓冲技术
  21. this.SetStyle(ControlStyles.ResizeRedraw, true); //根据窗口大小的重新绘制指令
  22. this.SetStyle(ControlStyles.Selectable, true);//激活与用户的交互响应
  23. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景色
  24. this.SetStyle(ControlStyles.UserPaint, true); //用户自己控制绘制
  25. }
  26. #region 绘制对象的创建
  27. //画布
  28. private Graphics g;
  29. //画笔
  30. private Pen p;
  31. //画刷
  32. private SolidBrush sb;
  33. #endregion
  34. #region 控件属性
  35. private int orginGap = 20;
  36. [Browsable(true)]
  37. [Category("自定义属性")]
  38. [Description("原点坐标")]
  39. public int OriginGap
  40. {
  41. get { return orginGap; }
  42. set
  43. {
  44. if (value <= 0)
  45. {
  46. return;
  47. }
  48. orginGap = value;
  49. this.Invalidate(); //重新绘制
  50. }
  51. }
  52. private float maxXAxis = 70000.0f;
  53. [Browsable(true)]
  54. [Category("自定义属性")]
  55. [Description("X轴最大值")]
  56. public float MaxXAxis
  57. {
  58. get { return maxXAxis; }
  59. set { maxXAxis = value;
  60. this.Invalidate(); //重新绘制
  61. }
  62. }
  63. private float maxYAxis = 70000.0f;
  64. [Browsable(true)]
  65. [Category("自定义属性")]
  66. [Description("Y轴最大坐标值")]
  67. public float MaxYAxis
  68. {
  69. get { return maxYAxis; }
  70. set { maxYAxis = value;
  71. this.Invalidate(); //重新绘制
  72. }
  73. }
  74. private int reclaimerXAxis = 0;
  75. [Browsable(true)]
  76. [Category("自定义属性")]
  77. [Description("取料口X轴坐标")]
  78. public int ReclaimerXAxis
  79. {
  80. get { return reclaimerXAxis; }
  81. set
  82. {
  83. reclaimerXAxis = value;
  84. this.Invalidate();
  85. }
  86. }
  87. private int reclaimerYAxis = 0;
  88. [Browsable(true)]
  89. [Category("自定义属性")]
  90. [Description("取料口Y轴坐标")]
  91. public int ReclaimerYAxis
  92. {
  93. get { return reclaimerYAxis; }
  94. set
  95. {
  96. reclaimerYAxis = value;
  97. this.Invalidate();
  98. }
  99. }
  100. private int processXAxis = 0;
  101. [Browsable(true)]
  102. [Category("自定义属性")]
  103. [Description("加工处X轴坐标")]
  104. public int ProcessXAxis
  105. {
  106. get { return processXAxis; }
  107. set
  108. {
  109. processXAxis = value;
  110. this.Invalidate();
  111. }
  112. }
  113. private int processYAxis = 0;
  114. [Browsable(true)]
  115. [Category("自定义属性")]
  116. [Description("加工处Y轴坐标")]
  117. public int ProcessYAxis
  118. {
  119. get { return processYAxis; }
  120. set
  121. {
  122. processYAxis = value;
  123. this.Invalidate();
  124. }
  125. }
  126. private int outletXAxis = 0;
  127. [Browsable(true)]
  128. [Category("自定义属性")]
  129. [Description("出料口X轴坐标")]
  130. public int OutletXAxis
  131. {
  132. get { return outletXAxis; }
  133. set
  134. {
  135. outletXAxis = value;
  136. this.Invalidate();
  137. }
  138. }
  139. private int outletYAxis = 0;
  140. [Browsable(true)]
  141. [Category("自定义属性")]
  142. [Description("出料口Y轴坐标")]
  143. public int OutletYAxis
  144. {
  145. get { return outletYAxis; }
  146. set
  147. {
  148. outletYAxis = value;
  149. this.Invalidate();
  150. }
  151. }
  152. private Color reclaimerColor = Color.Blue;
  153. [Browsable(true)]
  154. [Category("自定义属性")]
  155. [Description("取料口点颜色")]
  156. public Color ReclaimerColor
  157. {
  158. get { return reclaimerColor; }
  159. set
  160. {
  161. reclaimerColor = value;
  162. this.Invalidate();
  163. }
  164. }
  165. private Color processColor = Color.Green;
  166. [Browsable(true)]
  167. [Category("自定义属性")]
  168. [Description("加工处点颜色")]
  169. public Color ProcessColor
  170. {
  171. get { return processColor; }
  172. set
  173. {
  174. processColor = value;
  175. this.Invalidate();
  176. }
  177. }
  178. private Color outletColor = Color.Red;
  179. [Browsable(true)]
  180. [Category("自定义属性")]
  181. [Description("出料口点颜色")]
  182. public Color OutletColor
  183. {
  184. get { return outletColor; }
  185. set
  186. {
  187. outletColor = value;
  188. this.Invalidate();
  189. }
  190. }
  191. private string reclaimerStr = "取料口";
  192. [Browsable(true)]
  193. [Category("自定义属性")]
  194. [Description("取料口字符串")]
  195. public string ReclaimerStr
  196. {
  197. get { return reclaimerStr; }
  198. set
  199. {
  200. reclaimerStr = value;
  201. this.Invalidate();
  202. }
  203. }
  204. private string processStr = "加工处";
  205. [Browsable(true)]
  206. [Category("自定义属性")]
  207. [Description("加工处字符串")]
  208. public string ProcessStr
  209. {
  210. get { return processStr; }
  211. set
  212. {
  213. processStr = value;
  214. this.Invalidate();
  215. }
  216. }
  217. private string outletStr = "出料口";
  218. [Browsable(true)]
  219. [Category("自定义属性")]
  220. [Description("出料口字符串")]
  221. public string OutletStr
  222. {
  223. get { return outletStr; }
  224. set
  225. {
  226. outletStr = value;
  227. this.Invalidate();
  228. }
  229. }
  230. private int pointdiameter = 5;
  231. [Browsable(true)]
  232. [Category("自定义属性")]
  233. [Description("绘制点直径")]
  234. public int Pointdiameter
  235. {
  236. get { return pointdiameter; }
  237. set
  238. {
  239. pointdiameter = value;
  240. this.Invalidate();
  241. }
  242. }
  243. #endregion
  244. protected override void OnPaint(PaintEventArgs e) //执行重绘指令时会被调用
  245. {
  246. base.OnPaint(e);
  247. //绘制过程
  248. //绘制画布
  249. g = e.Graphics;
  250. //设置画布
  251. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
  252. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  253. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  254. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  255. //正式绘制
  256. //设置画笔
  257. p = new Pen(Color.Black, 1.5f);
  258. p.CustomEndCap = new AdjustableArrowCap(p.Width * 3, p.Width * 4, true); //结束端采用箭头样式
  259. //绘制X轴
  260. g.DrawLine(p, new Point(orginGap, this.Height - orginGap), new Point(this.Width - orginGap, this.Height - orginGap));
  261. //绘制Y轴
  262. g.DrawLine(p, new Point(orginGap, this.Height - orginGap), new Point(orginGap, orginGap));
  263. //绘制原点
  264. StringFormat sf = new StringFormat();
  265. sf.Alignment = StringAlignment.Center;//字符中心居中
  266. sf.LineAlignment = StringAlignment.Center;//线中心居中
  267. g.DrawString("0", this.Font, new SolidBrush(Color.Black), new Rectangle(0, this.Height - orginGap, orginGap, orginGap), sf);
  268. //绘制X轴最大
  269. g.DrawString(maxXAxis.ToString(), this.Font, new SolidBrush(Color.Black), new Rectangle(this.Width -50, this.Height -orginGap+5, 50, orginGap), sf);
  270. //绘制Y轴最大
  271. g.DrawString(maxYAxis.ToString(), this.Font, new SolidBrush(Color.Black), new Rectangle(0, 0, 50, orginGap), sf);
  272. //绘制取料点
  273. float reclaimerX = (this.Width - 2 * orginGap) / maxXAxis * reclaimerXAxis + orginGap;
  274. float reclaimerY = this.Height - ((this.Height - 2 * orginGap) / maxYAxis * reclaimerYAxis + orginGap);
  275. g.FillEllipse(new SolidBrush(reclaimerColor), new RectangleF(reclaimerX - 0.5f * pointdiameter, reclaimerY - 0.5f * pointdiameter, pointdiameter, pointdiameter));
  276. g.DrawString(reclaimerStr, this.Font, new SolidBrush(reclaimerColor), new RectangleF(reclaimerX - 25.0f, reclaimerY - 20.0f, 50.0f, 20.0f), sf);
  277. //绘制加工点
  278. float processX = (this.Width - 2 * orginGap) / maxXAxis * processXAxis + orginGap;
  279. float processY = this.Height - ((this.Height - 2 * orginGap) / maxYAxis * processYAxis + orginGap);
  280. g.FillEllipse(new SolidBrush(processColor), new RectangleF(processX - 0.5f * pointdiameter, processY - 0.5f * pointdiameter, pointdiameter, pointdiameter));
  281. g.DrawString(processStr, this.Font, new SolidBrush(processColor), new RectangleF(processX - 25.0f, processY - 20.0f, 50.0f, 20.0f), sf);
  282. //绘制出料点
  283. float outletX = (this.Width - 2 * orginGap) / maxXAxis * outletXAxis + orginGap;
  284. float outletY = this.Height - ((this.Height - 2 * orginGap) / maxYAxis * outletYAxis + orginGap);
  285. g.FillEllipse(new SolidBrush(outletColor), new RectangleF(outletX - 0.5f * pointdiameter, outletY - 0.5f * pointdiameter, pointdiameter, pointdiameter));
  286. g.DrawString(outletStr, this.Font, new SolidBrush(outletColor), new RectangleF(outletX - 25.0f, outletY - 20.0f, 50.0f, 20.0f), sf);
  287. }
  288. private void PointCurve_Load(object sender, EventArgs e)
  289. {
  290. }
  291. }
  292. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/345389
推荐阅读
相关标签
  

闽ICP备14008679号