【转】WinForm窗体刻度尺

`using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace IntegrationAPP
{
public partial class Form2 : Form
{
public static float MonitorDPI = 96f ;
private int originLocation = 0; //坐标原地起始位置
private int maxScaleX = 1000; //X轴最大刻度
private int maxScaleY = 1000; //Y轴最大刻度
private float scaling = 1.0F; //缩放比例
private int offSetX = 0; //X轴偏移位置
private int offSetY = 0; //Y轴偏移位置
private Font font = new Font("Arial", 9); //刻度值显示字体
private TextureBrush textureBrush;
private Bitmap bit;
int x = 0;
int y = 0;

    public Form2()
    {

        InitializeComponent();
      


    }
    private void button1_Click(object sender, EventArgs e)
    {
       // bit = new Bitmap((int)(MonitorDPI / 25.4 * 1 * scaling) - offSetX, (int)(MonitorDPI / 25.4 * 1 * scaling) - offSetX);
        Graphics g1 = CreateGraphics();
        PaintEventArgs pe = new PaintEventArgs(g1, this.ClientRectangle);
        RulerControl_Paint(g1,pe );
       // g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
       // g1.Dispose();
       // textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效减少窗体拉伸时的闪烁 
    }
    private void RulerControl_Paint(object sender, PaintEventArgs e)
      {
          Graphics g = e.Graphics;
          int widthInmm = maxScaleX;
          int heightInmm = maxScaleY;

          //绘制X轴           
          for (int i = 0; i <= widthInmm; i++)
          {
              SizeF size = g.MeasureString(Convert.ToString(i), font);
              float x = originLocation + (float)(MonitorDPI / 25.4 * i * scaling) - offSetX;
              if (x >= originLocation)
              {
                  PointF start = new PointF(x, originLocation);
                  PointF end = new PointF(x, 3);
                  if (i % 5 == 0)
                  {
                      end = new PointF(x, 6);
                  }
                  if (i % 10 == 0 && i != 0)
                  {
                      end = new PointF(x, 12);
                      g.DrawString(Convert.ToString(i), font, Brushes.Black, new PointF(x - (float)(MonitorDPI / 25.4 * Convert.ToString(i).Length * scaling), 12));
                  }
                  g.DrawLine(Pens.Black, start, end);
              }
          }
          g.DrawLine(Pens.Black, new PointF(originLocation, originLocation), new PointF(this.Width, originLocation));

          //绘制Y轴
          for (int i = 0; i <= heightInmm; i++)
          {
              SizeF size = g.MeasureString(Convert.ToString(i), font);
              float y = originLocation + (float)(MonitorDPI / 25.4 * i * scaling) - offSetY;
              if (y >= originLocation)
              {
                  PointF start = new PointF(originLocation, y);
                  PointF end = new PointF(3, y);
                  if (i % 5 == 0)
                  {
                      end = new PointF(6, y);
                  }
                  if (i % 10 == 0 && i != 0)
                  {
                      end = new PointF(12, y);
                      g.DrawString(Convert.ToString(i), font, Brushes.Black, new PointF(12, y - (float)(MonitorDPI / 25.4 * Convert.ToString(i).Length * scaling)));
                  }
                  g.DrawLine(Pens.Black, start, end);
              }
          }
          g.DrawLine(Pens.Black, new PointF(originLocation, originLocation), new PointF(originLocation, this.Height));

          Pen p = new Pen(Color.Gray, 1);
          p.DashStyle = DashStyle.Solid;
          p.DashPattern = new float[] { 1, (float)(MonitorDPI / 25.4 * 5 * scaling) - 1 };

          for (int i = 0; i <= heightInmm; i += 5)
          {
              g.DrawLine(p, 0, (float)(MonitorDPI / 25.4 * i * scaling), 1000, (float)(MonitorDPI / 25.4 * i * scaling));
          }
      }
}

}
【转】WinForm窗体刻度尺

`

https://blog.csdn.net/zhangsongshan/article/details/4797698?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242

上一篇:关于 WinDoAdmin


下一篇:winform小技巧