基本信息
源码名称:C#画图(可保存cad文件,并加载cad文件)
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2019-09-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace CAD
{
    public partial class CADFrame : Form
    {
        private BaseTool currentTool = null;
        private ArrayList currentShapes = null;
        private Hashtable registerToolMap = null;
        private ArrayList historyShapes = null;

        public const String LINETOOL_REGISTERNAME = "LINETOOL_REGISTERNAME";
        public const String HANDTOOL_REGISTERNAME = "HANDTOOL_REGISTERNAME";
        public CADFrame()
        {
            InitializeComponent();
            currentShapes = new ArrayList();
            registerToolMap = new Hashtable();
            historyShapes = new ArrayList();
            this.registerTool(LINETOOL_REGISTERNAME, new LineTool());
            this.registerTool(HANDTOOL_REGISTERNAME, new HandTool());
            this.record();
        }

        public ArrayList getCurrentShapes()
        {
            return currentShapes;
        }

        public void setCurrentShapes(ArrayList currentShapes)
        {
            this.currentShapes = currentShapes;
        }

        public BaseTool getCurrentTool()
        {
            return currentTool;
        }

        public void setCurrentTool(BaseTool currentTool)
        {
            this.currentTool = currentTool;
        }

        public Hashtable getRegisterToolMap()
        {
            return registerToolMap;
        }

        public void setRegisterToolMap(Hashtable registerToolMap)
        {
            this.registerToolMap = registerToolMap;
        }

        public ArrayList getHistoryShapes()
        {
            return historyShapes;
        }

        public void setHistoryShapes(ArrayList historyShapes)
        {
            this.historyShapes = historyShapes;
        }

        public void registerTool(string registerName, BaseTool registerTool)
        {
            this.getRegisterToolMap().Add(registerName, registerTool);
            registerTool.setRefCADPanel(this);
        }

        public void useTool(string registerName)
        {
            if (this.getCurrentTool() != null) this.getCurrentTool().unSet();
            BaseTool setTool = (BaseTool)this.getRegisterToolMap()[registerName];
            if (setTool != null)
            {
                setTool.set();
                this.setCurrentTool(setTool);
            }
        }

        int undoIndex = 0;
        public void record()
        {
            if (undoIndex > 0)
            {
                while (undoIndex != 0)
                {
                    this.getHistoryShapes().RemoveAt(this.getHistoryShapes().Count - 1);
                    undoIndex--;
                }
            }
            this.getHistoryShapes().Add(this.cloneShapArray(this.getCurrentShapes()));
        }

        public void redo()
        {
            if (undoIndex > 0)
            {
                undoIndex--;
                this.setCurrentShapes(this.cloneShapArray((ArrayList)this.getHistoryShapes()[this.getHistoryShapes().Count - 1 - undoIndex]));
            }
            this.Refresh();

        }
        public void undo()
        {
            if ((this.getHistoryShapes().Count - 1 - undoIndex) > 0)
            {
                undoIndex  ;
                this.setCurrentShapes(this.cloneShapArray((ArrayList)this.getHistoryShapes()[this.getHistoryShapes().Count - 1 - undoIndex]));
            }
            this.Refresh();
        }

        public ArrayList cloneShapArray(ArrayList shapeArrayList)
        {
            ArrayList returnShapeArrayList = new ArrayList();
            for (int i = 0; i < shapeArrayList.Count; i  )
            {
                returnShapeArrayList.Add(((BaseShape)shapeArrayList[i]).copySelf());
            }
            return returnShapeArrayList;
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            this.useTool(LINETOOL_REGISTERNAME);
        }

        private void btnHand_Click(object sender, EventArgs e)
        {
            this.useTool(HANDTOOL_REGISTERNAME);
        }

        private void btnUndoButton_Click(object sender, EventArgs e)
        {
            this.undo();
        }

        private void btnRedoButton_Click(object sender, EventArgs e)
        {
            this.redo();
        }

        public void clear()
        {
            undoIndex = 0;
            this.setHistoryShapes(new ArrayList());
            this.setCurrentShapes(new ArrayList());
            this.record();
            this.pictureBox1.Refresh();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            clear();
        }

        public void save(String filePath)
        {
            Stream s = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite);
            BinaryFormatter b = new BinaryFormatter();
            for (int i = 0; i < this.getCurrentShapes().Count; i  )
            {
                b.Serialize(s,this.getCurrentShapes()[i]);
            }
            s.Close();
        }

        public void load(string filepath)
        {
            Stream s = File.Open(filepath, FileMode.Open, FileAccess.Read);
            BinaryFormatter c = new BinaryFormatter();
            ArrayList newShapes = new ArrayList();
            bool forFlat = true;
            for (int i = 0; forFlat; i  )
            {
                try
                {
                    newShapes.Add(c.Deserialize(s));
                }
                catch (Exception e)
                {
                    forFlat = false;
                }
            }
            s.Close();
            this.setCurrentShapes(newShapes);
            this.setHistoryShapes(new ArrayList());
            this.record();
            undoIndex = 0;
            this.pictureBox1.Refresh();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                load(openFileDialog1.FileName);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                save(saveFileDialog1.FileName);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            for (int i = 0; i < currentShapes.Count; i  )
            {
                ((BaseShape)currentShapes[i]).superDraw(g);
            }
        }

        bool isMouseDown = false;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            if(this.getCurrentTool()!=null) this.getCurrentTool().superMouseDown(sender,e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                if (this.getCurrentTool() != null) this.getCurrentTool().superMouseDrag(sender, e);
            }
            else
            {
                if (this.getCurrentTool() != null) this.getCurrentTool().superMouseMove(sender, e);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            if (this.getCurrentTool() != null) this.getCurrentTool().superMouseUp(sender, e);
        }
    }
}