WinForm 标尺和网格 改良版

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Barcode.UserControls
{
    public partial class RulerControl : UserControl
    {
        public static float MonitorDPI = 96;
        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 Bitmap bit;
        int x = 0;
        int y = 0;
        public RulerControl()
        {
            InitializeComponent();
        }

        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.Dot;
            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));
            }     
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值