c#如何获取字符串长度的像素长度+绘制RadioButton控件

实习工作是要我们自行绘制一个RadioButton控件,不能用继承,所以得自己用Graphics画,之前没接触过,给我人整麻了,哈哈哈,不过还好有大佬分享,弄了一个,可以根据选择对齐方式,连着Text显示文本一起改变位置的radio控件

这是获取字符串像素宽度的代码:

 1    /// <summary>
 2           /// 测量用指定的 System.Drawing.Font 绘制的指定字符串。
 3          /// </summary>
 4           /// <param name="g">GDI+</param>
 5         /// <param name="str">字符串</param>
 6           /// <param name="family">新 System.Drawing.Font 的 System.Drawing.FontFamily。(我也不知道是啥意思,但是我给了个Font.Family可以运行)</param>
 7           /// <param name="emSize">字体的大小(比如宋体,9pt,9就是要给的emSize,可以通过Font.Size获取)</param>
 8           /// <returns>此方法返回 System.Drawing.SizeF 结构,该结构表示 text 参数指定的、使用 font 参数绘制的字符串的大小,单位由 System.Drawing.Graphics.PageUnit属性指定。</returns>
 9           public SizeF GetStringSize(Graphics g, string str, FontFamily family, float emSize)
10          {
11              return g.MeasureString($"{str}", new Font(family, emSize, GraphicsUnit.Point)); //最后一个参数我也不知道是啥意思哈哈,反正能用就行
12          }

原出处(https://blog.csdn.net/zuoziqiang520/article/details/91388102)

这是绘制RadioButton控件的代码:

 

  1  /// <summary>
  2         /// 重绘
  3         /// </summary>
  4         /// <param name="e"></param>
  5         /// <param name="Text"></param>
  6         /// <param name="f"></param>
  7         public void P_Radio(PaintEventArgs e, string Text, Font f, int width, int height, ContentAlignment ca)
  8         {
  9             //起始横纵坐标
 10             int x = 0, y = 0;
 11             SizeF z = default(SizeF);
 12 
 13             try
 14             {
 15 
 16                 z = GetStringSize(e.Graphics, Text, f.FontFamily, f.Size);
 17             }
 18             catch (Exception ex)
 19             {
 20                 ex.Message.ToString();
 21             }
 22             //控件总宽度-圆和字符串长度➗2 获得总体离两端的距离
 23             int around = Convert.ToInt32(width - z.Width - 12) / 2;
 24             #region 获取xy轴坐标(已淘汰大家直接忽视这段吧)
 25             //判断选择的对齐方式
 26             //switch (ca)
 27             //{
 28             //    case ContentAlignment.TopLeft:
 29             //        x = 0; y = 0;
 30             //        break;
 31             //    case ContentAlignment.TopCenter:
 32 
 33             //        x = Convert.ToInt32(width / 2 - z.Width-12); y = 0;
 34             //        break;
 35             //    case ContentAlignment.TopRight:
 36             //        x = Convert.ToInt32(width - z.Width-12); y = 0;
 37             //        break;
 38             //    case ContentAlignment.MiddleLeft:
 39             //        x = 0; y =Convert.ToInt32(height / 2-12);
 40             //        break;
 41             //    case ContentAlignment.MiddleCenter:
 42             //        x = Convert.ToInt32(width / 2 - z.Width-12); y = Convert.ToInt32(height / 2 - 12);
 43             //        break;
 44             //    case ContentAlignment.MiddleRight:
 45             //        x = Convert.ToInt32(width - z.Width-12); y = Convert.ToInt32(height / 2 - 12);
 46             //        break;
 47             //    case ContentAlignment.BottomLeft:
 48             //        x = 0; y = Convert.ToInt32(height - 12);
 49             //        break;
 50             //    case ContentAlignment.BottomCenter:
 51             //        x = Convert.ToInt32(width / 2 - z.Width-12); y = Convert.ToInt32(height-12);
 52             //        break;
 53             //    case ContentAlignment.BottomRight:
 54             //        x = Convert.ToInt32(width - z.Width-12); y = Convert.ToInt32(height - 12);
 55             //        break;
 56             //}
 57 
 58             #endregion
 59             #region 确定x轴
 60             if (ca.ToString().Contains("Left"))
 61             {
 62                 x = 0;
 63             }
 64             else if (ca.ToString().Contains("Center"))
 65             {
 66                 x = around;
 67             }
 68             else
 69                 x = around*2;
 70             #endregion
 71             #region 确定y轴
 72             
 73             if (ca.ToString().Contains("Top"))
 74             {
 75                 y = 0;
 76             }
 77             else if (ca.ToString().Contains("Middle"))
 78             {
 79                 //减去圆高的一半
 80                 y = Convert.ToInt32(height / 2 - 6);
 81             }
 82             else
 83                 y = height-12;
 84             #endregion
 85             Graphics g = e.Graphics;
 86             Rectangle radioButtonrect = new Rectangle(x, y, 12, 12);
 87 
 88             g.SmoothingMode = SmoothingMode.AntiAlias;//抗锯齿处理 
 89 
 90             //圆饼背景 
 91             using (SolidBrush brush = new SolidBrush(Color.White))
 92             {
 93                 g.FillEllipse(brush, radioButtonrect);
 94             }
 95             if (JVSUConctrol_GDI_RadioBtn.Ischeck == true)
 96             {
 97                 radioButtonrect.Inflate(-2, -2);//矩形内缩2单位 
 98                 g.FillEllipse(Brushes.Red, radioButtonrect);
 99                 radioButtonrect.Inflate(2, 2);//还原 
100             }
101 
102             //圆形边框 
103             using (Pen pen = new Pen(Color.Red))
104             {
105                 g.DrawEllipse(pen, radioButtonrect);
106             }
107             //绘制字符串
108             using (SolidBrush drawBrush = new SolidBrush(Color.Black))
109             {
110                 g.DrawString(Text, f, drawBrush, x+12, y);
111             }
112         }

这最后一段绘制圆形的代码,是copy的同事的哈哈哈,也是网上某个大佬的代码不知道出处,还请原谅。

 

posted @ 2021-12-22 14:01  吕小不小迷弟  阅读(354)  评论(0编辑  收藏  举报