WINFORM学习手册——画网格和标尺(转)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformImg
{
    public partial class RulerForm : Form
    {
        int zoom = 2;//缩放比例
        int _pxwidth = 0;//每毫米包含多少个像素,这是一个粗略的估算值,用于在不同的分辨率下,画布的大小基本一致

        public int Pxwidth
        {
            get { return _pxwidth * zoom; }
            set { _pxwidth = value; }
        }
        int _canvasWidth = 210;//画布的宽度
        public int CanvasWidth
        {
            get { return _canvasWidth; }
            set { _canvasWidth = value; }
        }
        int _canvasHeight = 148;//画布的高度
        public int CanvasHeight
        {
            get { return _canvasHeight; }
            set { _canvasHeight = value; }
        }
        Panel canvas = new Panel();//画布
        TextBox lineX = new TextBox();//横坐标游标
        TextBox lineY = new TextBox();//纵坐标游标

        public RulerForm()
        {
            InitializeComponent();
            this.CanvasHeight = 148;//设置画布的高度
            this.CanvasWidth = 210;//设置画布的宽度
            Calcsale();//计算每毫米的像素数量
        }
        /// <summary>
        /// 加载画布和游标卡尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RulerForm_Load(object sender, EventArgs e)
        {
            canvas.Width = this.CanvasWidth * this.Pxwidth;//将毫米宽度转换为像素宽度
            canvas.Height = this.CanvasHeight * this.Pxwidth;//将毫米高度转换为像素高度
            canvas.Top = 20;
            canvas.Left = 20;
            canvas.BackColor = Color.White;
            canvas.Paint += Canvas_Paint;
            canvas.MouseMove += Canvas_MouseMove;
            this.Controls.Add(canvas);
            //标尺
            lineX.Width = 1;
            lineX.Height = 14;
            lineX.Top = 6;
            lineX.BackColor = Color.Red;
            lineX.Multiline = true;
            lineX.Visible = false;
            this.Controls.Add(lineX);
            //标尺
            lineY.Width = 14;
            lineY.Height = 1;
            lineY.Left = 6;
            lineY.BackColor = Color.Red;
            lineY.Multiline = true;
            lineY.Visible = false;
            this.Controls.Add(lineY);
            this.Width = canvas.Width + 40;
            this.Height = canvas.Height + 40;
        }
        /// <summary>
        /// 画作标尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RulerForm_Paint(object sender, PaintEventArgs e)
        {
            var grap = e.Graphics;
            //画布区域
            Rectangle rect = new Rectangle(20, 20, this.CanvasWidth * this.Pxwidth, this.CanvasHeight * this.Pxwidth);
            grap.DrawRectangle(new Pen(Color.Black, 1), rect);
            SetXRuler(ref grap);//画横坐标
            SetYRuler(ref grap);//画纵坐标
        }
        /// <summary>
        /// 画网格线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_Paint(object sender, PaintEventArgs e)
        {
            Graphics grap = e.Graphics;
            //画布区域
            Rectangle rect = new Rectangle(0, 0, this.CanvasWidth * this.Pxwidth, this.CanvasHeight * this.Pxwidth);
            grap.DrawRectangle(new Pen(Color.Black, 1), rect);
            //画网格,每5mm一个点
            Pen pen = new Pen(Color.Gray, 1);
            pen.DashStyle = DashStyle.Dot;
            pen.DashPattern = new float[] { 1, 5 * this.Pxwidth - 1 };//点长度1px,点间空白4mm
            for (var i = 0; i < this.CanvasWidth; i = i + 5)
            {
                grap.DrawLine(pen, i * this.Pxwidth, 0, i * this.Pxwidth, this.CanvasHeight * this.Pxwidth);
            }

        }
        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            lineX.Visible = true;
            lineY.Visible = true;
            lineX.Location = new Point(20 + e.X, 6);
            lineY.Location = new Point(6, 20 + e.Y);
        }
        #region 私有方法
        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int Index);
        /// <summary>
        /// 计算精度 确保在不同分辨率的机子上刻度的准确性
        /// </summary>
        private void Calcsale()
        {
            PictureBox p = new PictureBox();
            Graphics g = Graphics.FromHwnd(p.Handle);
            IntPtr hdc = g.GetHdc();
            //GetDeviceCaps(hdc, 4)方法中,第二个参数意义:4毫米为单位屏幕宽度,6毫米为单位屏幕高度,8像素为单位的屏幕宽度10像素为单位的屏幕高度
            int width = GetDeviceCaps(hdc, 4);
            int pix = GetDeviceCaps(hdc, 8);
            this.Pxwidth = pix / width;
        }
        /// <summary>
        /// 画横坐标
        /// </summary>
        /// <param name="grap"></param>
        private void SetXRuler(ref Graphics grap)
        {
            Pen pen = new Pen(Color.Black, 1);
            int count = 0;
            //一个单位画一格
            for (int i = 0; i <= this.CanvasWidth; i++)
            {

                if (count % 10 == 0)
                {
                    grap.DrawLine(pen, 20 + i * this.Pxwidth, 5, 20 + i * this.Pxwidth, 20);
                    grap.DrawString(count.ToString(), new Font("宋体", 8), new SolidBrush(Color.Black), 20 + i * this.Pxwidth + 2, 2, new StringFormat());
                }
                else if (count % 5 == 0)
                {
                    grap.DrawLine(pen, 20 + i * this.Pxwidth, 12, 20 + i * this.Pxwidth, 20);
                }
                else
                {
                    grap.DrawLine(pen, 20 + i * this.Pxwidth, 15, 20 + i * this.Pxwidth, 20);
                }
                count++;
            }
        }
        /// <summary>
        /// 画纵坐标
        /// </summary>
        /// <param name="grap"></param>
        private void SetYRuler(ref Graphics grap)
        {
            Pen pen = new Pen(Color.Black, 1);
            int count = 0;
            //设置刻度上面显示数字垂直显示
            StringFormat stringFormat = new StringFormat();
            stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            //一个单位画一格
            for (int i = 0; i <= this.CanvasHeight; i++)
            {
                if (count % 10 == 0)
                {
                    grap.DrawLine(pen, 5, 20 + i * this.Pxwidth, 20, 20 + i * this.Pxwidth);
                    grap.DrawString(count.ToString(), new Font("宋体", 8), new SolidBrush(Color.Black), 2, 20 + i * this.Pxwidth + 2, stringFormat);
                }
                else if (count % 5 == 0)
                {
                    grap.DrawLine(pen, 12, 20 + i * this.Pxwidth, 20, 20 + i * this.Pxwidth);
                }
                else
                {
                    grap.DrawLine(pen, 15, 20 + i * this.Pxwidth, 20, 20 + i * this.Pxwidth);
                }
                count++;
            }
        }
        #endregion



    }
}
image
posted @ 2014-01-20 20:52  争世不悔  阅读(2286)  评论(0编辑  收藏  举报