国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

C#功能強大的WinForm標(biāo)尺與網(wǎng)格(版權(quán)所有,嚴(yán)禁轉(zhuǎn)載)

2019-11-17 04:02:10
字體:
供稿:網(wǎng)友
這段時間忙于研究WinForm的標(biāo)尺與網(wǎng)格,主管要我做的跟PowerDesigner一樣,經(jīng)過一段時間的研究、實踐、修改,最終達(dá)標(biāo)了,代碼如下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.PRinting;
using System.Drawing.Drawing2D;
//計算屏幕像素大小和mm大小引用
using System.Runtime.InteropServices;

namespace UntilityControl
{
    public partial class RuleAndGrid : UserControl
    {
        public Control useControl;
        private TextBox lineX=new TextBox();
        private TextBox lineY=new TextBox();
        private TextBox rectangleX=new TextBox();
        private TextBox rectangleY=new TextBox();
        Pen penG = new Pen(Color.Black);
        Pen penL = new Pen(Color.Red);
        Font font = new Font("宋體", 8);
        public RuleAndGrid()
        {
            InitializeComponent();
        }
        public Panel panel1 = new Panel();
        //每毫米占的像素數(shù)
        float pxwidth;

        private void RulerAndGrid_Paint(object sender, PaintEventArgs e)
        {
            Graphics PenGraphics = e.Graphics;
            InitializeGraph(PenGraphics);
        }

        private void RulerAndGrid_Load(object sender, EventArgs e)
        {
            panel1.Left = 21;
            panel1.Top = 21;
            panel1.Width = RulerWidth - 42;
            panel1.Height = RulerHeight - 42;
            panel1.BackColor = Color.White;
            panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
            panel1.MouseUp+=new MouseEventHandler(panel1_MouseUp);
            panel1.Paint+=new PaintEventHandler(panel1_Paint);
            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);
            rectangleX.Height = 6;
            rectangleX.Top =14;
            rectangleX.BackColor = Color.Black;
            rectangleX.Multiline = true;
            rectangleX.Visible = false;
            this.Controls.Add(rectangleX);
            rectangleY.Width = 6;
            rectangleY.Left = 14;
            rectangleY.BackColor = Color.Black;
            rectangleY.Multiline = true;
            rectangleY.Visible = false;
            this.Controls.Add(rectangleY);
            this.Controls.Add(panel1);
            Calcsale();
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            DrawGrid();
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                SetProperty sp = new SetProperty();
                sp.rad = this;
                sp.ShowDialog();
            }
            else
            {
                if (useControl != null)
                {
                    useControl.MouseClick +=new MouseEventHandler(useControl_MouseClick);
                    useControl.Top = e.Y;
                    useControl.Left = e.X;
                    panel1.Controls.Add(useControl);
                    panel1.Cursor = Cursors.Default;
                    rectangleX.Left = e.X+21;
                    rectangleX.Width = useControl.Width;
                    rectangleX.Visible = true;
                    rectangleY.Top = e.Y+21;
                    rectangleY.Height = useControl.Height;
                    rectangleY.Visible = true;
                    useControl = null;
                }
            }
        }
        private void useControl_MouseClick(object sender, MouseEventArgs e)
        {
            
        }
        private void ChangePanelSize()
        {
            panel1.Width = RulerWidth - 42;
            panel1.Height = RulerHeight - 42;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (useControl != null)
            {
                panel1.Cursor = Cursors.Cross;
            }
            else
            {
                panel1.Cursor = Cursors.Default;
            }
            lineX.Left = e.X+21;
            lineX.Visible = true;
            lineY.Top = e.Y+21;
            lineY.Visible = true;
        }
        [Browsable(true),Category("直尺尺寸"),Description("直尺長")]
        public int RulerWidth
        {
            get
            {
                return this.Width;
            }
            set
            {
                this.Width = value;
                this.Refresh();
                ChangePanelSize();
            }
        }
        [Browsable(true), Category("直尺尺寸"), Description("直尺寬")]
        public int RulerHeight
        {
            get
            {
                return this.Height;
            }
            set
            {
                this.Height = value;
                this.Refresh();
                ChangePanelSize();
            }
        }

        //定義一個顯示刻度的單位的屬性,單位是毫米或英寸
        public enum scaletype
        {
            centimeter = 0, inch = 1
        }
        scaletype usescale = scaletype.centimeter;
        [Browsable(true), Category("用戶屬性"), Description("設(shè)置顯示單位,厘米或英寸")]
        public scaletype Unit
        {
            get
            {
                return usescale;
            }
            set
            {
                usescale = value;
                this.Refresh();
            }
        }
        //定義網(wǎng)格顯示多少刻度的值
        public enum GridUnitValue
        {
            One = 1, Two = 2, Three = 3, Four = 4, Five = 5
        }
        GridUnitValue useGirdUnit = GridUnitValue.Five;
        [Browsable(true), Category("用戶屬性"), Description("設(shè)置每格網(wǎng)格所占的刻度數(shù)")]
        public GridUnitValue GridUnit
        {
            get
            {
                return useGirdUnit;
            }
            set
            {
                useGirdUnit = value;
                this.Refresh();
            }
        }
        //定義一個按比例放大/縮小刻度的屬性,該屬性是縮放的系數(shù)
        float c = 1.0f;
        [Browsable(true), Category("用戶屬性"), Description("設(shè)置顯示比例")]
        public float Coefficient
        {
            get
            {
                return c;
            }
            set
            {
                c = value;
                this.Refresh();
            }
        }

        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int Index);
        /// <summary>
        /// 計算精度 確保在不同分辨率的機子上刻度的準(zhǔn)確性
        /// </summary>
        private void Calcsale()
        {
            PictureBox p = new PictureBox();
            Graphics g = Graphics.FromHwnd(p.Handle);
            IntPtr hdc = g.GetHdc();
            //GetDeviceCaps(hdc, 4)方法中,第二個參數(shù)意義:4毫米為單位屏幕寬度,6毫米為單位屏幕高度,8像素為單位的屏幕寬度10像素為單位的屏幕高度
            int width = GetDeviceCaps(hdc, 4);
            int pix = GetDeviceCaps(hdc, 8);
            pxwidth = pix / width;
        }

        //定義一個設(shè)置橫軸起始刻度值的屬性
        private double checkPaperWidth = 0;
        [Browsable(true), Category("用戶屬性"), Description("設(shè)置刻度尺橫軸刻度的起始值")]
        public double StartX
        {
            get
            {
                return checkPaperWidth;
            }
            set
            {
                checkPaperWidth = value;
                this.Refresh();
            }
        }

        //定義一個設(shè)置縱軸起始刻度值的屬性
        private double checkPaperHeight = 0;
        [Browsable(true), Category("用戶屬性"), Description("設(shè)置刻度尺縱軸刻度的起始值")]
        public double StartY
        {
            get
            {
                return checkPaperHeight;
            }
            set
            {
                checkPaperHeight = value;
                this.Refresh();
            }
        }

        //定義一個當(dāng)控件內(nèi)部圖形發(fā)生變化的時候觸發(fā)的事件
        public delegate void PaperChangeHandle();
        private PaperChangeHandle PaperChanged;
        [Browsable(true), Category("用戶事件"), Description("當(dāng)控件內(nèi)部圖形發(fā)生變化的時候觸發(fā)")]
        public event PaperChangeHandle OnPaperChanged
        {
            add
            {
                PaperChanged += value;
            }
            remove
            {
                PaperChanged -= value;
            }
        }

        private void InitializeGraph(Graphics Gph)
        {
            //// 畫豎線
            //Gph.DrawLine(penG, 20, 20, 20, height - 20);
            //Gph.DrawLine(penG, height - 20, 20, height - 20, height - 20);
            //// 畫橫線
            //Gph.DrawLine(penG, 20, 20, width - 20, 20);
            //Gph.DrawLine(penG, 20, width - 20, width - 20, height - 20);

            Rectangle curRect = new Rectangle(20, 20, RulerWidth - 40, RulerHeight - 40);
            Gph.DrawRectangle(penG, curRect);

            SetXAxis(ref Gph);
            SetYAxis(ref Gph);
        }

        ///<summary>
        /// 設(shè)置畫圖板橫軸的刻度。
        ///</summary>
        ///<param name="objGraphics"></param>
        private void SetXAxis(ref Graphics objGraphics)
        {
            int x1 = 20;
            int y1 = 5;
            int x2 = 20;
            int y2 = 20;
            //刻度步長
            float scale = 0;
            float Scale = scale;
            // 判斷當(dāng)前的刻度數(shù)目標(biāo)記
            int iCount = 0;
            double X = checkPaperWidth;
            // 設(shè)置橫軸的刻度
            for (int i = 0; Scale <= RulerWidth - 40; i++)
            {
                if (iCount == 10)
                {
                    // 畫出長刻度 長 15象素
                    objGraphics.DrawLine(penG, (x1 + Scale), y1, (x2 + Scale), y2);
                    objGraphics.DrawLine(penG, (x1 + Scale), RulerHeight - 5, (x2 + Scale), RulerHeight - 20);
                    iCount = 0;

                    // 標(biāo)出刻度上的數(shù)字
                    X += 10;
                    objGraphics.DrawString(X.ToString(), font, new SolidBrush(Color.Black), (x1 + Scale - 15), 0);
                    objGraphics.DrawString(X.ToString(), font, new SolidBrush(Color.Black), (x1 + Scale - 15), RulerHeight - 10);
                }
                else if (iCount == 5)
                {
                    // 畫中間刻度 長 9象素
                    objGraphics.DrawLine(penG, (x1 + Scale), y1 + 6, (x2 + Scale), y2);
                    objGraphics.DrawLine(penG, (x1 + Scale), RulerHeight - 11, (x2 + Scale), RulerHeight - 20);
                }
                else
                {
                    // 畫短刻度 長 6象素
                    objGraphics.DrawLine(penG, (x1 + Scale), (y1 + 9), (x2 + Scale), y2);
                    objGraphics.DrawLine(penG, (x1 + Scale), RulerHeight - 14, (x2 + Scale), RulerHeight - 20);
                }

                iCount++;
                if (usescale == scaletype.centimeter)
                {
                    scale += pxwidth;
                    Scale = scale * c;
                }
                else
                {
                    scale += 2.54f * pxwidth;
                    Scale = scale * c;
                }
            }
        }
        /// <summary>
        /// 設(shè)置畫圖板縱軸的刻度。
        /// </summary>
        /// <param name="objGraphics"></param>
        private void SetYAxis(ref Graphics objGraphics)
        {
            int x1 = 5;
            int y1 = 20;
            int x2 = 20;
            int y2 = 20;
            int iCount = 0;
            float scale = 0;
            float Scale = scale;
            double Y = checkPaperHeight;
            //設(shè)置刻度上面顯示數(shù)字垂直顯示
            StringFormat stringFormat = new StringFormat();
            stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

            for (int i = 0; Scale <= RulerHeight - 40; i++)
            {
                if (iCount == 10)
                {
                    // 畫出長刻度 長 15象素
                    objGraphics.DrawLine(penG, x1, (y1 + Scale), x2, (y2 + Scale));
                    objGraphics.DrawLine(penG, RulerWidth - 20, (y1 + Scale), RulerWidth - 5, (y2 + Scale));
                    iCount = 0;

                    //  標(biāo)出刻度上的數(shù)字
                    Y += 10;
                    objGraphics.DrawString(Y.ToString(), font, new SolidBrush(Color.Black), 0, Scale + 8, stringFormat);
                    objGraphics.DrawString(Y.ToString(), font, new SolidBrush(Color.Black), RulerWidth - 14, Scale + 8, stringFormat);
                }
                else if (iCount == 5)
                {
                    // 畫出短刻度 長 9象素
                    objGraphics.DrawLine(penG, (x1 + 6), (y1 + Scale), x2, (y2 + Scale));
                    objGraphics.DrawLine(penG, RulerWidth - 20, (y1 + Scale), RulerWidth - 11, (y2 + Scale));
                }
                else
                {
                    // 畫出短刻度 長 6象素
                    objGraphics.DrawLine(penG, (x1 + 9), (y1 + Scale), x2, (y2 + Scale));
                    objGraphics.DrawLine(penG, RulerWidth - 20, (y1 + Scale), RulerWidth - 14, (y2 + Scale));
                }
                iCount++;
                if (usescale == scaletype.centimeter)
                {
                    scale += pxwidth;
                    Scale = scale * c;
                }
                else
                {
                    scale += 2.54f * pxwidth;
                    Scale = scale * c;
                }
            }
        }
        private void DrawGrid()
        {
            Pen p = new Pen(Color.Gray, 1);
            p.DashStyle = DashStyle.Dot;
            p.DashPattern = new float[] { 1, pxwidth * Convert.ToInt32(GridUnit) - 1 };
            Graphics gg = panel1.CreateGraphics();
            for (int i = 0; i < (RulerWidth - 40) / pxwidth; i++)
            {
                gg.DrawLine(p, -1, (pxwidth * i * (float)(Convert.ToInt32(GridUnit))-1), (panel1.Width), (pxwidth * i * (float)(Convert.ToInt32(GridUnit))-1));
            }
        }
    }
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永胜县| 平乐县| 孝昌县| 德钦县| 屏南县| 咸阳市| 宁德市| 临漳县| 阆中市| 虎林市| 章丘市| 通道| 图木舒克市| 韶山市| 安宁市| 梓潼县| 大理市| 林周县| 莱州市| 兰坪| 巴塘县| 龙口市| 汉阴县| 志丹县| 姜堰市| 洪雅县| 西乌| 湖州市| 福州市| 香河县| 崇州市| 霍山县| 望江县| 巴南区| 正阳县| 龙山县| 西乡县| 红桥区| 苍山县| 清涧县| 双鸭山市|