c# WinForm标尺与网格
来源:网络来源TAG:WinForm标尺与网格浏览数: 日期:2010-8-2评论:
折叠C# Code复制内容到剪贴板
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Drawing;   
  5. using System.Data;   
  6. using System.Text;   
  7. using System.Windows.Forms;   
  8. using System.Drawing.Printing;   
  9. using System.Drawing.Drawing2D;   
  10. //计算屏幕像素大小和mm大小引用   
  11. using System.Runtime.InteropServices;   
  12.   
  13. namespace UntilityControl   
  14. {   
  15.     public partial class RuleAndGrid : UserControl   
  16.     {   
  17.         public Control useControl;   
  18.         private TextBox lineX=new TextBox();   
  19.         private TextBox lineY=new TextBox();   
  20.         private TextBox rectangleX=new TextBox();   
  21.         private TextBox rectangleY=new TextBox();   
  22.         Pen penG = new Pen(Color.Black);   
  23.         Pen penL = new Pen(Color.Red);   
  24.         Font font = new Font("宋体", 8);   
  25.         public RuleAndGrid()   
  26.         {   
  27.             InitializeComponent();   
  28.         }   
  29.         public Panel panel1 = new Panel();   
  30.         //每毫米占的像素数   
  31.         float pxwidth;   
  32.   
  33.         private void RulerAndGrid_Paint(object sender, PaintEventArgs e)   
  34.         {   
  35.             Graphics PenGraphics = e.Graphics;   
  36.             InitializeGraph(PenGraphics);   
  37.         }   
  38.   
  39.         private void RulerAndGrid_Load(object sender, EventArgs e)   
  40.         {   
  41.             panel1.Left = 21;   
  42.             panel1.Top = 21;   
  43.             panel1.Width = RulerWidth - 42;   
  44.             panel1.Height = RulerHeight - 42;   
  45.             panel1.BackColor = Color.White;   
  46.             panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);   
  47.             panel1.MouseUp+=new MouseEventHandler(panel1_MouseUp);   
  48.             panel1.Paint+=new PaintEventHandler(panel1_Paint);   
  49.             lineX.Width = 1;   
  50.             lineX.Height = 14;   
  51.             lineX.Top = 6;   
  52.             lineX.BackColor = Color.Red;   
  53.             lineX.Multiline = true;   
  54.             lineX.Visible = false;   
  55.             this.Controls.Add(lineX);   
  56.             lineY.Width = 14;   
  57.             lineY.Height = 1;   
  58.             lineY.Left = 6;   
  59.             lineY.BackColor = Color.Red;   
  60.             lineY.Multiline = true;   
  61.             lineY.Visible = false;   
  62.             this.Controls.Add(lineY);   
  63.             rectangleX.Height = 6;   
  64.             rectangleX.Top =14;   
  65.             rectangleX.BackColor = Color.Black;   
  66.             rectangleX.Multiline = true;   
  67.             rectangleX.Visible = false;   
  68.             this.Controls.Add(rectangleX);   
  69.             rectangleY.Width = 6;   
  70.             rectangleY.Left = 14;   
  71.             rectangleY.BackColor = Color.Black;   
  72.             rectangleY.Multiline = true;   
  73.             rectangleY.Visible = false;   
  74.             this.Controls.Add(rectangleY);   
  75.             this.Controls.Add(panel1);   
  76.             Calcsale();   
  77.         }   
  78.         private void panel1_Paint(object sender, PaintEventArgs e)   
  79.         {   
  80.             DrawGrid();   
  81.         }   
  82.         private void panel1_MouseUp(object sender, MouseEventArgs e)   
  83.         {   
  84.             if (e.Button == MouseButtons.Right)   
  85.             {   
  86.                 SetProperty sp = new SetProperty();   
  87.                 sp.rad = this;   
  88.                 sp.ShowDialog();   
  89.             }   
  90.             else  
  91.             {   
  92.                 if (useControl != null)   
  93.                 {   
  94.                     useControl.MouseClick +=new MouseEventHandler(useControl_MouseClick);   
  95.                     useControl.Top = e.Y;   
  96.                     useControl.Left = e.X;   
  97.                     panel1.Controls.Add(useControl);   
  98.                     panel1.Cursor = Cursors.Default;   
  99.                     rectangleX.Left = e.X+21;   
  100.                     rectangleX.Width = useControl.Width;   
  101.                     rectangleX.Visible = true;   
  102.                     rectangleY.Top = e.Y+21;   
  103.                     rectangleY.Height = useControl.Height;   
  104.                     rectangleY.Visible = true;   
  105.                     useControl = null;   
  106.                 }   
  107.             }   
  108.         }   
  109.         private void useControl_MouseClick(object sender, MouseEventArgs e)   
  110.         {   
  111.                
  112.         }   
  113.         private void ChangePanelSize()   
  114.         {   
  115.             panel1.Width = RulerWidth - 42;   
  116.             panel1.Height = RulerHeight - 42;   
  117.         }   
  118.         private void panel1_MouseMove(object sender, MouseEventArgs e)   
  119.         {   
  120.             if (useControl != null)   
  121.             {   
  122.                 panel1.Cursor = Cursors.Cross;   
  123.             }   
  124.             else  
  125.             {   
  126.                 panel1.Cursor = Cursors.Default;   
  127.             }   
  128.             lineX.Left = e.X+21;   
  129.             lineX.Visible = true;   
  130.             lineY.Top = e.Y+21;   
  131.             lineY.Visible = true;   
  132.         }   
  133.         [Browsable(true),Category("直尺尺寸"),Description("直尺长")]   
  134.         public int RulerWidth   
  135.         {   
  136.             get  
  137.             {   
  138.                 return this.Width;   
  139.             }   
  140.             set  
  141.             {   
  142.                 this.Width = value;   
  143.                 this.Refresh();   
  144.                 ChangePanelSize();   
  145.             }   
  146.         }   
  147.         [Browsable(true), Category("直尺尺寸"), Description("直尺宽")]   
  148.         public int RulerHeight   
  149.         {   
  150.             get  
  151.             {   
  152.                 return this.Height;   
  153.             }   
  154.             set  
  155.             {   
  156.                 this.Height = value;   
  157.                 this.Refresh();   
  158.                 ChangePanelSize();   
  159.             }   
  160.         }   
  161.   
  162.         //定义一个显示刻度的单位的属性,单位是毫米或英寸   
  163.         public enum scaletype   
  164.         {   
  165.             centimeter = 0, inch = 1   
  166.         }   
  167.         scaletype usescale = scaletype.centimeter;   
  168.         [Browsable(true), Category("用户属性"), Description("设置显示单位,厘米或英寸")]   
  169.         public scaletype Unit   
  170.         {   
  171.             get  
  172.             {   
  173.                 return usescale;   
  174.             }   
  175.             set  
  176.             {   
  177.                 usescale = value;   
  178.                 this.Refresh();   
  179.             }   
  180.         }   
  181.         //定义网格显示多少刻度的值   
  182.         public enum GridUnitValue   
  183.         {   
  184.             One = 1, Two = 2, Three = 3, Four = 4, Five = 5   
  185.         }   
  186.         GridUnitValue useGirdUnit = GridUnitValue.Five;   
  187.         [Browsable(true), Category("用户属性"), Description("设置每格网格所占的刻度数")]   
  188.         public GridUnitValue GridUnit   
  189.         {   
  190.             get  
  191.             {   
  192.                 return useGirdUnit;   
  193.             }   
  194.             set  
  195.             {   
  196.                 useGirdUnit = value;   
  197.                 this.Refresh();   
  198.             }   
  199.         }   
  200.         //定义一个按比例放大/缩小刻度的属性,该属性是缩放的系数   
  201.         float c = 1.0f;   
  202.         [Browsable(true), Category("用户属性"), Description("设置显示比例")]   
  203.         public float Coefficient   
  204.         {   
  205.             get  
  206.             {   
  207.                 return c;   
  208.             }   
  209.             set  
  210.             {   
  211.                 c = value;   
  212.                 this.Refresh();   
  213.             }   
  214.         }   
  215.   
  216.         [DllImport("gdi32.dll")]   
  217.         public static extern int GetDeviceCaps(IntPtr hdc, int Index);   
  218.         ///    
  219.         /// 计算精度 确保在不同分辨率的机子上刻度的准确性   
  220.         ///    
  221.         private void Calcsale()   
  222.         {   
  223.             PictureBox p = new PictureBox();   
  224.             Graphics g = Graphics.FromHwnd(p.Handle);   
  225.             IntPtr hdc = g.GetHdc();   
  226.             //GetDeviceCaps(hdc, 4)方法中,第二个参数意义:4毫米为单位屏幕宽度,6毫米为单位屏幕高度,8像素为单位的屏幕宽度10像素为单位的屏幕高度   
  227.             int width = GetDeviceCaps(hdc, 4);   
  228.             int pix = GetDeviceCaps(hdc, 8);   
  229.             pxwidth = pix / width;   
  230.         }   
  231.   
  232.         //定义一个设置横轴起始刻度值的属性   
  233.         private double checkPaperWidth = 0;   
  234.         [Browsable(true), Category("用户属性"), Description("设置刻度尺横轴刻度的起始值")]   
  235.         public double StartX   
  236.         {   
  237.             get  
  238.             {   
  239.                 return checkPaperWidth;   
  240.             }   
  241.             set  
  242.             {   
  243.                 checkPaperWidth = value;   
  244.                 this.Refresh();   
  245.             }   
  246.         }   
  247.   
  248.         //定义一个设置纵轴起始刻度值的属性   
  249.         private double checkPaperHeight = 0;   
  250.         [Browsable(true), Category("用户属性"), Description("设置刻度尺纵轴刻度的起始值")]   
  251.         public double StartY   
  252.         {   
  253.             get  
  254.             {   
  255.                 return checkPaperHeight;   
  256.             }   
  257.             set  
  258.             {   
  259.                 checkPaperHeight = value;   
  260.                 this.Refresh();   
  261.             }   
  262.         }   
  263.   
  264.         //定义一个当控件内部图形发生变化的时候触发的事件   
  265.         public delegate void PaperChangeHandle();   
  266.         private PaperChangeHandle PaperChanged;   
  267.         [Browsable(true), Category("用户事件"), Description("当控件内部图形发生变化的时候触发")]   
  268.         public event PaperChangeHandle OnPaperChanged   
  269.         {   
  270.             add   
  271.             {   
  272.                 PaperChanged += value;   
  273.             }   
  274.             remove   
  275.             {   
  276.                 PaperChanged -= value;   
  277.             }   
  278.         }   
  279.   
  280.         private void InitializeGraph(Graphics Gph)   
  281.         {   
  282.             //// 画竖线   
  283.             //Gph.DrawLine(penG, 20, 20, 20, height - 20);   
  284.             //Gph.DrawLine(penG, height - 20, 20, height - 20, height - 20);   
  285.             //// 画横线   
  286.             //Gph.DrawLine(penG, 20, 20, width - 20, 20);   
  287.             //Gph.DrawLine(penG, 20, width - 20, width - 20, height - 20);   
  288.   
  289.             Rectangle curRect = new Rectangle(20, 20, RulerWidth - 40, RulerHeight - 40);   
  290.             Gph.DrawRectangle(penG, curRect);   
  291.   
  292.             SetXAxis(ref Gph);   
  293.             SetYAxis(ref Gph);   
  294.         }   
  295.   
  296.         ///   
  297.         /// 设置画图板横轴的刻度。   
  298.         ///   
  299.         ///   
  300.         private void SetXAxis(ref Graphics objGraphics)   
  301.         {   
  302.             int x1 = 20;   
  303.             int y1 = 5;   
  304.             int x2 = 20;   
  305.             int y2 = 20;   
  306.             //刻度步长   
  307.             float scale = 0;   
  308.             float Scale = scale;   
  309.             // 判断当前的刻度数目标记   
  310.             int iCount = 0;   
  311.             double X = checkPaperWidth;   
  312.             // 设置横轴的刻度   
  313.             for (int i = 0; Scale <= RulerWidth - 40; i++)   
  314.             {   
  315.                 if (iCount == 10)   
  316.                 {   
  317.                     // 画出长刻度 长 15象素   
  318.                     objGraphics.DrawLine(penG, (x1 + Scale), y1, (x2 + Scale), y2);   
  319.                     objGraphics.DrawLine(penG, (x1 + Scale), RulerHeight - 5, (x2 + Scale), RulerHeight - 20);   
  320.                     iCount = 0;   
  321.   
  322.                     // 标出刻度上的数字   
  323.                     X += 10;   
  324.                     objGraphics.DrawString(X.ToString(), font, new SolidBrush(Color.Black), (x1 + Scale - 15), 0);   
  325.                     objGraphics.DrawString(X.ToString(), font, new SolidBrush(Color.Black), (x1 + Scale - 15), RulerHeight - 10);   
  326.                 }   
  327.                 else if (iCount == 5)   
  328.                 {   
  329.                     // 画中间刻度 长 9象素   
  330.                     objGraphics.DrawLine(penG, (x1 + Scale), y1 + 6, (x2 + Scale), y2);   
  331.                     objGraphics.DrawLine(penG, (x1 + Scale), RulerHeight - 11, (x2 + Scale), RulerHeight - 20);   
  332.                 }   
  333.                 else  
  334.                 {   
  335.                     // 画短刻度 长 6象素   
  336.                     objGraphics.DrawLine(penG, (x1 + Scale), (y1 + 9), (x2 + Scale), y2);   
  337.                     objGraphics.DrawLine(penG, (x1 + Scale), RulerHeight - 14, (x2 + Scale), RulerHeight - 20);   
  338.                 }   
  339.   
  340.                 iCount++;   
  341.                 if (usescale == scaletype.centimeter)   
  342.                 {   
  343.                     scale += pxwidth;   
  344.                     Scale = scale * c;   
  345.                 }   
  346.                 else  
  347.                 {   
  348.                     scale += 2.54f * pxwidth;   
  349.                     Scale = scale * c;   
  350.                 }   
  351.             }   
  352.         }   
  353.         ///    
  354.         /// 设置画图板纵轴的刻度。   
  355.         ///    
  356.         ///    
  357.         private void SetYAxis(ref Graphics objGraphics)   
  358.         {   
  359.             int x1 = 5;   
  360.             int y1 = 20;   
  361.             int x2 = 20;   
  362.             int y2 = 20;   
  363.             int iCount = 0;   
  364.             float scale = 0;   
  365.             float Scale = scale;   
  366.             double Y = checkPaperHeight;   
  367.             //设置刻度上面显示数字垂直显示   
  368.             StringFormat stringFormat = new StringFormat();   
  369.             stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;   
  370.   
  371.             for (int i = 0; Scale <= RulerHeight - 40; i++)   
  372.             {   
  373.                 if (iCount == 10)   
  374.                 {   
  375.                     // 画出长刻度 长 15象素   
  376.                     objGraphics.DrawLine(penG, x1, (y1 + Scale), x2, (y2 + Scale));   
  377.                     objGraphics.DrawLine(penG, RulerWidth - 20, (y1 + Scale), RulerWidth - 5, (y2 + Scale));   
  378.                     iCount = 0;   
  379.   
  380.                     //  标出刻度上的数字   
  381.   
  382.   
  383.   
  384.                     Y += 10;   
  385.   
  386.   
  387.   
  388.                     objGraphics.DrawString(Y.ToString(), font, new SolidBrush(Color.Black), 0, Scale + 8, stringFormat);   
  389.   
  390.   
  391.   
  392.                     objGraphics.DrawString(Y.ToString(), font, new SolidBrush(Color.Black), RulerWidth - 14, Scale + 8, stringFormat);   
  393.   
  394.   
  395.   
  396.                 }   
  397.   
  398.   
  399.   
  400.                 else if (iCount == 5)   
  401.   
  402.   
  403.   
  404.                 {   
  405.   
  406.   
  407.   
  408.                     // 画出短刻度 长 9象素   
  409.   
  410.   
  411.   
  412.                     objGraphics.DrawLine(penG, (x1 + 6), (y1 + Scale), x2, (y2 + Scale));   
  413.   
  414.   
  415.   
  416.                     objGraphics.DrawLine(penG, RulerWidth - 20, (y1 + Scale), RulerWidth - 11, (y2 + Scale));   
  417.   
  418.   
  419.   
  420.                 }   
  421.   
  422.   
  423.   
  424.                 else  
  425.   
  426.   
  427.   
  428.                 {   
  429.   
  430.   
  431.   
  432.                     // 画出短刻度 长 6象素   
  433.   
  434.   
  435.   
  436.                     objGraphics.DrawLine(penG, (x1 + 9), (y1 + Scale), x2, (y2 + Scale));   
  437.   
  438.   
  439.   
  440.                     objGraphics.DrawLine(penG, RulerWidth - 20, (y1 + Scale), RulerWidth - 14, (y2 + Scale));   
  441.   
  442.   
  443.   
  444.                 }   
  445.   
  446.   
  447.   
  448.                 iCount++;   
  449.   
  450.   
  451.   
  452.                 if (usescale == scaletype.centimeter)   
  453.   
  454.   
  455.   
  456.                 {   
  457.   
  458.   
  459.   
  460.                     scale += pxwidth;   
  461.   
  462.   
  463.   
  464.                     Scale = scale * c;   
  465.   
  466.   
  467.   
  468.                 }   
  469.   
  470.   
  471.   
  472.                 else  
  473.   
  474.   
  475.   
  476.                 {   
  477.   
  478.   
  479.   
  480.                     scale += 2.54f * pxwidth;   
  481.   
  482.   
  483.   
  484.                     Scale = scale * c;   
  485.   
  486.   
  487.   
  488.                 }   
  489.   
  490.   
  491.   
  492.             }   
  493.   
  494.   
  495.   
  496.         }   
  497.   
  498.   
  499.   
  500.         private void DrawGrid()   
  501.   
  502.   
  503.   
  504.         {   
  505.   
  506.   
  507.   
  508.             Pen p = new Pen(Color.Gray, 1);   
  509.   
  510.   
  511.   
  512.             p.DashStyle = DashStyle.Dot;   
  513.   
  514.   
  515.   
  516.             p.DashPattern = new float[] { 1, pxwidth * Convert.ToInt32(GridUnit) - 1 };   
  517.   
  518.   
  519.   
  520.             Graphics gg = panel1.CreateGraphics();   
  521.   
  522.   
  523.   
  524.             for (int i = 0; i < (RulerWidth - 40) / pxwidth; i++)   
  525.   
  526.   
  527.   
  528.             {   
  529.   
  530.   
  531.   
  532.                 gg.DrawLine(p, -1, (pxwidth * i * (float)(Convert.ToInt32(GridUnit))-1), (panel1.Width), (pxwidth * i * (float)(Convert.ToInt32(GridUnit))-1));   
  533.   
  534.             }   
  535.         }   
  536.     }   
  537. }   
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
相关评论
正在加载评论列表...
评论表单加载中...