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

namespace FYEDrawDemo_CSharp
{
    public partial class mainForm : Form
    {
        #region 浮云绘图DLL 接口
        //组件接口
        [DllImport("FYEDC.dll", EntryPoint = "NewFYEDC", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr NewFYEDC();
        [DllImport("FYEDC.dll", EntryPoint = "CreateFYEDC", CallingConvention = CallingConvention.Cdecl)]
        public static extern void CreateFYEDC(IntPtr canvas, IntPtr hParentWnd, int x, int y, int w, int h);
        [DllImport("FYEDC.dll", EntryPoint = "LoadShapes", CallingConvention = CallingConvention.Cdecl)]
        public static extern void LoadShapes(IntPtr canvas, string fileName);
        [DllImport("FYEDC.dll", EntryPoint = "DeleteFYEDC", CallingConvention = CallingConvention.Cdecl)]
        public static extern void DeleteFYEDC(IntPtr canvas);

        // 图元对象
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetShapeCount(IntPtr canvas);

        [DllImport("FYEDC.dll", EntryPoint = "GetShape", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr GetShape(IntPtr canvas, string type, string name);
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeAt", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr GetShapeAt(IntPtr canvas, int index);

        [DllImport("FYEDC.dll", EntryPoint = "GetmCompElementCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetmCompElementCount(IntPtr editor, string type, string name);
        [DllImport("FYEDC.dll", EntryPoint = "GetCompElement", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetCompElement(ref IntPtr diagrams, IntPtr editor, string type, string name);

        [DllImport("FYEDC.dll", EntryPoint = "GetShapeTypeCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetShapeTypeCount(IntPtr canvas, string type);
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeTitleCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetShapeTitleCount(IntPtr canvas, string title);
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeNameCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetShapeNameCount(IntPtr canvas, string name);

        // 图元属性
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeType", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetShapeType(IntPtr shape, ref byte type);
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeTitle", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetShapeTitle(IntPtr shape, ref byte title);
        [DllImport("FYEDC.dll", EntryPoint = "SetShapeTitle", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetShapeTitle(IntPtr canvas, IntPtr shape, string title);
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeName", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetShapeName(IntPtr shape, ref byte type);
        [DllImport("FYEDC.dll", EntryPoint = "SetShapeName", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetShapeName(IntPtr canvas, IntPtr shape, string name);

        [DllImport("FYEDC.dll", EntryPoint = "GetShapeGroupId", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetShapeGroupId(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetBackColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetBackColor(IntPtr canvas, int color);
        [DllImport("FYEDC.dll", EntryPoint = "GetBackColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetBackColor(IntPtr canvas);
        [DllImport("FYEDC.dll", EntryPoint = "SetNoClientColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetNoClientColor(IntPtr canvas, int color);

        [DllImport("FYEDC.dll", EntryPoint = "SetLineColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetLineColor(IntPtr canvas, IntPtr shape, int color);
        [DllImport("FYEDC.dll", EntryPoint = "GetLineColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetLineColor(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetLineStyle", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetLineStyle(IntPtr canvas, IntPtr shape, int style);
        [DllImport("FYEDC.dll", EntryPoint = "GetLineStyle", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetLineStyle(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetLineWidth", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetLineWidth(IntPtr canvas, IntPtr shape, int width);
        [DllImport("FYEDC.dll", EntryPoint = "GetLineWidth", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetLineWidth(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetLineEndMode", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetLineEndMode(IntPtr canvas, IntPtr shape, int mode);
        [DllImport("FYEDC.dll", EntryPoint = "GetLineEndMode", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetLineEndMode(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetFillColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetFillColor(IntPtr canvas, IntPtr shape, int color);
        [DllImport("FYEDC.dll", EntryPoint = "GetFillColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetFillColor(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetIsFill", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetIsFill(IntPtr canvas, IntPtr shape, int isFill);
        [DllImport("FYEDC.dll", EntryPoint = "GetIsFill", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetIsFill(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetFontColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetFontColor(IntPtr canvas, IntPtr shape, int color);
        [DllImport("FYEDC.dll", EntryPoint = "GetFontColor", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetFontColor(IntPtr shape);
        [DllImport("FYEDC.dll", EntryPoint = "SetFontType", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetFontType(IntPtr canvas, IntPtr shape, string type);
        [DllImport("FYEDC.dll", EntryPoint = "GetFontType", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetFontType(IntPtr shape, ref byte type);
        [DllImport("FYEDC.dll", EntryPoint = "SetFontSize", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetFontSize(IntPtr canvas, IntPtr shape, int size);
        [DllImport("FYEDC.dll", EntryPoint = "GetFontSize", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetFontSize(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "SetShapeText", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetShapeText(IntPtr canvas, IntPtr shape, string text);
        [DllImport("FYEDC.dll", EntryPoint = "GetShapeText", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetShapeText(IntPtr shape, ref byte text);
        [DllImport("FYEDC.dll", EntryPoint = "SetTextVisible", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetTextVisible(IntPtr canvas, IntPtr shape, int isShow);
        [DllImport("FYEDC.dll", EntryPoint = "GetTextVisible", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetTextVisible(IntPtr shape);

        [DllImport("FYEDC.dll", EntryPoint = "GetImageStatus", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetImageStatus(IntPtr shape);
        [DllImport("FYEDC.dll", EntryPoint = "SetImageStatus", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetImageStatus(IntPtr canvas, IntPtr shape, int status);

        // 区域
        [DllImport("FYEDC.dll", EntryPoint = "SetCanvasRect", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetCanvasRect(IntPtr canvas, int left, int top, int right, int bottom);
        [DllImport("FYEDC.dll", EntryPoint = "GetCanvasRect", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetCanvasRect(IntPtr canvas, ref int left, ref int top, ref int right, ref int bottom);
        [DllImport("FYEDC.dll", EntryPoint = "SetVirtualSize", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetVirtualSize(IntPtr canvas, int widht, int height);
        [DllImport("FYEDC.dll", EntryPoint = "GetVirtualSize", CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetVirtualSize(IntPtr canvas, ref int width, ref int height);

        // 缩放
        [DllImport("FYEDC.dll", EntryPoint = "SetZoom", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetZoom(IntPtr canvas, double zoom);
        [DllImport("FYEDC.dll", EntryPoint = "GetZoom", CallingConvention = CallingConvention.Cdecl)]
        public static extern double GetZoom(IntPtr canvas);
        [DllImport("FYEDC.dll", EntryPoint = "SetZoomFactor", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetZoomFactor(IntPtr canvas, double factor);
        [DllImport("FYEDC.dll", EntryPoint = "GetZoomFactor", CallingConvention = CallingConvention.Cdecl)]
        public static extern double GetZoomFactor(IntPtr canvas);

        // 网格
        [DllImport("FYEDC.dll", EntryPoint = "SetGridShow", CallingConvention = CallingConvention.Cdecl)]
        public static extern void SetGridShow(IntPtr canvas, int bShow);

        // 存为图片
        [DllImport("FYEDC.dll", EntryPoint = "SaveToPicture", CallingConvention = CallingConvention.Cdecl)]
        public static extern int SaveToPicture(IntPtr canvas, string fileName);
        #endregion 浮云绘图DLL 接口

        IntPtr m_shapeCanvas;
        public mainForm()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            m_shapeCanvas = NewFYEDC();
            CreateFYEDC(m_shapeCanvas, this.Handle, 10, 50, this.ClientSize.Width - 20, this.ClientSize.Height - 70);
        }

        private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DeleteFYEDC(m_shapeCanvas);
        }

        private void mainForm_SizeChanged(object sender, EventArgs e)
        {
            SetCanvasRect(m_shapeCanvas, 10, 50, this.ClientSize.Width - 20, this.ClientSize.Height - 70);
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            //string fl = "C:\\Users\\ldong\\Desktop\\111.txt";
            //LoadShapes(m_shapeCanvas, fl);
            //return;

            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
            fileDialog.Filter = "文本(*.txt;.ini)|*.txt;*.ini";
            fileDialog.FilterIndex = 0;                 //当前使用第二个过滤字符串
            fileDialog.RestoreDirectory = false;         //对话框关闭时恢复原目录
            fileDialog.Title = "选择文件";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                LoadShapes(m_shapeCanvas, fileDialog.FileName);
            }

        }

        private void btnSaveImage_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.SaveFileDialog fileDialog = new System.Windows.Forms.SaveFileDialog();
            fileDialog.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
            fileDialog.Filter = "图片(*.jpg;.bmp)|*.jpg;*.bmp";
            fileDialog.FilterIndex = 0;                 //当前使用第二个过滤字符串
                                                        //        fileDialog.RestoreDirectory = true;         //对话框关闭时恢复原目录
            fileDialog.Title = "另存为";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SaveToPicture(m_shapeCanvas, fileDialog.FileName);
            }
        }

        int m_shapeSeqNum = 0;
        private void btnLogicPorp_Click(object sender, EventArgs e)
        {
            string sTitle = "", sName = "";

            IntPtr shape = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);    //获得图元对象
            if (shape != IntPtr.Zero)
            {
                // 获取数据
                byte[] s = new byte[200];
                lock (s)
                {
                    GetShapeTitle(shape, ref s[0]);     //获取图元Title
                }
                sTitle = ByteToString(s);

                Array.Clear(s, 0, s.Length);
               byte[] s2 = new byte[200];
                lock (s2)
                {
                    GetShapeName(shape, ref s2[0]);      //获取图元Name
                }
                sName = ByteToString(s2);

                ShapePropForm frm = new ShapePropForm();
                frm.m_iSeqNum = m_shapeSeqNum;
                frm.m_sTitle = sTitle;
                frm.m_sName = sName;

                // 设置数据
                if (DialogResult.OK == frm.ShowDialog())
                {
                    m_shapeSeqNum = frm.m_iSeqNum;      // 可以修改默认的图元序号
                    if (GetShapeCount(m_shapeCanvas) - 1 < m_shapeSeqNum)
                        m_shapeSeqNum = GetShapeCount(m_shapeCanvas) - 1;
                    if (m_shapeSeqNum < 0)
                        m_shapeSeqNum = 0;

                    IntPtr shape2 = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);
                    if (shape2 != IntPtr.Zero)
                    {
                        SetShapeTitle(m_shapeCanvas, shape2, frm.m_sTitle);      //设置图元Title
                        SetShapeName(m_shapeCanvas, shape2, frm.m_sName);        //设置图元Name
                    }
                }
            }
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            IntPtr shape = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);    //获得图元对象
            if (shape != IntPtr.Zero)
            {
                // 获取数据
                LinePropForm frm = new LinePropForm();
                frm.m_iSeqNum = m_shapeSeqNum;
                frm.m_iLineColor = GetLineColor(shape);
                frm.m_iLineWidth = GetLineWidth(shape);
                frm.m_iLineStyle = GetLineStyle(shape);
                frm.m_iEndMode = GetLineEndMode(shape);

                // 设置数据
                if (DialogResult.OK == frm.ShowDialog())
                {
                    m_shapeSeqNum = frm.m_iSeqNum;      // 可以修改默认的图元序号
                    if (GetShapeCount(m_shapeCanvas) - 1 < m_shapeSeqNum)
                        m_shapeSeqNum = GetShapeCount(m_shapeCanvas) - 1;
                    if (m_shapeSeqNum < 0)
                        m_shapeSeqNum = 0;

                    IntPtr shape2 = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);
                    if (shape2 != IntPtr.Zero)
                    {
                        SetLineColor(m_shapeCanvas, shape2, frm.m_iLineColor);
                        SetLineWidth(m_shapeCanvas, shape2, frm.m_iLineWidth);
                        SetLineStyle(m_shapeCanvas, shape2, frm.m_iLineStyle);
                        SetLineEndMode(m_shapeCanvas, shape2, frm.m_iEndMode);
                    }
                }
            }
        }

        private void btnArea_Click(object sender, EventArgs e)
        {
            IntPtr shape = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);    //获得图元对象
            if (shape != IntPtr.Zero)
            {
                // 获取数据
                AreaPropForm frm = new AreaPropForm();
                frm.m_iSeqNum = m_shapeSeqNum;
                frm.m_iFillColor = GetFillColor(shape);
                frm.m_isFill = GetIsFill(shape);

                // 设置数据
                if (DialogResult.OK == frm.ShowDialog())
                {
                    m_shapeSeqNum = frm.m_iSeqNum;      // 可以修改默认的图元序号
                    if (GetShapeCount(m_shapeCanvas) - 1 < m_shapeSeqNum)
                        m_shapeSeqNum = GetShapeCount(m_shapeCanvas) - 1;
                    if (m_shapeSeqNum < 0)
                        m_shapeSeqNum = 0;

                    IntPtr shape2 = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);
                    if (shape2 != IntPtr.Zero)
                    {
                        SetFillColor(m_shapeCanvas, shape2, frm.m_iFillColor);
                        SetIsFill(m_shapeCanvas, shape2, frm.m_isFill);
                    }
                }
            }
        }
        private void btnText_Click(object sender, EventArgs e)
        {
            IntPtr shape = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);    //获得图元对象
            if (shape != IntPtr.Zero)
            {
                // 获取数据
                TextPropForm frm = new TextPropForm();
                frm.m_iSeqNum = m_shapeSeqNum;

                byte[] bys = new byte[200];
                lock (bys)
                {
                    GetShapeText(shape, ref bys[0]);     //获取图元Title
                }
                String sTxt = ByteToString(bys);

                frm.m_sContent = sTxt;
                frm.m_isDisplay = GetTextVisible(shape);

                // 设置数据
                if (DialogResult.OK == frm.ShowDialog())
                {
                    m_shapeSeqNum = frm.m_iSeqNum;      // 可以修改默认的图元序号
                    if (GetShapeCount(m_shapeCanvas) - 1 < m_shapeSeqNum)
                        m_shapeSeqNum = GetShapeCount(m_shapeCanvas) - 1;
                    if (m_shapeSeqNum < 0)
                        m_shapeSeqNum = 0;

                    IntPtr shape2 = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);
                    if (shape2 != IntPtr.Zero)
                    {
                        SetShapeText(m_shapeCanvas, shape2, frm.m_sContent);
                        SetTextVisible(m_shapeCanvas, shape2, frm.m_isDisplay);
                    }
                }
            }
        }
        private void btnFont_Click(object sender, EventArgs e)
        {
            IntPtr shape = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);    //获得图元对象
            if (shape != IntPtr.Zero)
            {
                // 获取数据
                FontPropForm frm = new FontPropForm();
                frm.m_iSeqNum = m_shapeSeqNum;               
                frm.m_iFontColor = GetFontColor(shape);
                frm.m_iFontSize = GetFontSize(shape);

                byte[] bys = new byte[200];
                lock (bys)
                {
                    GetFontType(shape, ref bys[0]);     
                }
                String sTxt = ByteToString(bys);
                frm.m_sFontStyle = sTxt;

                // 设置数据
                if (DialogResult.OK == frm.ShowDialog())
                {
                    m_shapeSeqNum = frm.m_iSeqNum;      // 可以修改默认的图元序号
                    if (GetShapeCount(m_shapeCanvas) - 1 < m_shapeSeqNum)
                        m_shapeSeqNum = GetShapeCount(m_shapeCanvas) - 1;
                    if (m_shapeSeqNum < 0)
                        m_shapeSeqNum = 0;

                    IntPtr shape2 = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);
                    if (shape2 != IntPtr.Zero)
                    {
                        SetFontType(m_shapeCanvas, shape2, frm.m_sFontStyle);
                        SetFontColor(m_shapeCanvas, shape2, frm.m_iFontColor);
                        SetFontSize(m_shapeCanvas, shape2, frm.m_iFontSize);
                    }
                }
            }
        }

        private void btnImg_Click(object sender, EventArgs e)
        {
            m_shapeSeqNum = 4;
            IntPtr shape = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);    //获得图元对象
            if (shape != IntPtr.Zero)
            {
                // 获取图元类型
                byte[] sT = new byte[200];
                lock (sT)
                {
                    GetShapeType(shape, ref sT[0]);     //获取图元Type
                }
                string sType = ByteToString(sT);
                if(sType != "image")
                {
                    MessageBox.Show(string.Format("第{0}个图元不是图片类型，请选择图片图元测试", m_shapeSeqNum+1));
                    return;
                }

                // 获取数据
                ImgPropForm frm = new ImgPropForm();
                frm.m_iSeqNum = m_shapeSeqNum;
                frm.m_iStatus = GetImageStatus(shape);

                // 设置数据
                if (DialogResult.OK == frm.ShowDialog())
                {
                    m_shapeSeqNum = frm.m_iSeqNum;      // 可以修改默认的图元序号
                    if (GetShapeCount(m_shapeCanvas) - 1 < m_shapeSeqNum)
                        m_shapeSeqNum = GetShapeCount(m_shapeCanvas) - 1;
                    if (m_shapeSeqNum < 0)
                        m_shapeSeqNum = 0;

                    IntPtr shape2 = GetShapeAt(m_shapeCanvas, m_shapeSeqNum);
                    if (shape2 != IntPtr.Zero)
                    {
                        // 获取图元类型
                        byte[] sT2 = new byte[200];
                        lock (sT2)
                        {
                            GetShapeType(shape2, ref sT2[0]);     //获取图元Type
                        }
                        string sType2 = ByteToString(sT2);
                        if (sType2 != "image")
                        {
                            MessageBox.Show(string.Format("第{0}个图元不是图片类型，请选择图片图元测试", m_shapeSeqNum + 1));
                            return;
                        }

                        SetImageStatus(m_shapeCanvas, shape2, frm.m_iStatus);
                    }
                }
            }
        }

        private void btnCanvas_Click(object sender, EventArgs e)
        {
            // 获取数据
            CanvasPropForm frm = new CanvasPropForm();

            frm.m_iVirBackColor = GetBackColor(m_shapeCanvas);

            int l=0, r = 0, t = 0, b = 0, w = 0, h = 0;
            GetCanvasRect(m_shapeCanvas, ref l, ref t, ref r, ref b);
            t = 70;
            frm.m_iCanWidth = r - l;
            frm.m_iCanHeight = b - t;
            GetVirtualSize(m_shapeCanvas, ref w, ref h);
            frm.m_iVirWidth = w;
            frm.m_iVirHeight = h;

            // 设置数据
            if (DialogResult.OK == frm.ShowDialog())
            {

                SetCanvasRect(m_shapeCanvas, 10, 50, 10 + frm.m_iCanWidth, 50 + frm.m_iCanHeight);
 
                SetVirtualSize(m_shapeCanvas, frm.m_iVirWidth, frm.m_iVirHeight);

                SetGridShow(m_shapeCanvas, frm.m_iDisplayGrid);
                SetBackColor(m_shapeCanvas, frm.m_iVirBackColor);
          //      SetNoClientColor(m_shapeCanvas, frm.m_iNoClientColor);
            }
        }

        private void btnZoom_Click(object sender, EventArgs e)
        {
            // 获取数据
            ZoomPropForm frm = new ZoomPropForm();
            frm.m_fZoom = (float)GetZoom(m_shapeCanvas);
            frm.m_fZoomFactor = (float)GetZoomFactor(m_shapeCanvas);

            // 设置数据
            if (DialogResult.OK == frm.ShowDialog())
            {
                SetZoom(m_shapeCanvas, frm.m_fZoom);
                SetZoomFactor(m_shapeCanvas, frm.m_fZoomFactor);
            }
        }

        private static string ByteToString(byte[] s1)
        {
        //    string sType = System.Text.Encoding.Default.GetString(s1, 0, s1.Length);
            string sType = System.Text.Encoding.Unicode.GetString(s1, 0, s1.Length);
            sType = sType.Substring(0, sType.IndexOf('\0'));
            return sType;
        }

        byte[] gbys = new byte[200];
        private void btnLightFlash_Click(object sender, EventArgs e)
        {
            if(btnLightFlash.Text == "灯闪")
            {
                this.timer1.Interval = 1000;
                this.timer1.Start();

                btnLightFlash.Text = "停止";
            }
            else
            {
                this.timer1.Stop();
                btnLightFlash.Text = "灯闪";
            }
            
            
        }

        private void btnLineWhite_Click(object sender, EventArgs e)
        {
            int color;
            if (btnLineWhite.Text == "白光带")
            {
               color = 0xFFFFFF;
               btnLineWhite.Text = "红光带";
            } 
            else if(btnLineWhite.Text == "红光带")
            {
                color = 0xFF;
                btnLineWhite.Text = "黑色";
            }
            else if (btnLineWhite.Text == "黑色")
            {
                color = 0;
                btnLineWhite.Text = "浅蓝";
            }
            else
            {
                color = 16755370;
                btnLineWhite.Text = "白光带";
            }


            int count = GetShapeCount(m_shapeCanvas);
            for (int i = 0; i < count; i++)
            {
                IntPtr shape = GetShapeAt(m_shapeCanvas, i);
                if (shape != IntPtr.Zero)
                {
                    lock (gbys)
                    {
                        GetShapeType(shape, ref gbys[0]);
                    }
                    String sType = ByteToString(gbys);

                    if (sType == "line" )
                    {
                        SetLineColor(m_shapeCanvas, shape, color);
                    }
                }

            }
        }

        private bool m_isLighting = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            int ll = 0;
            int color = 0;
            int count = GetShapeCount(m_shapeCanvas);
            Random rand = new Random() ;
            for (int i = 0; i < count; i++)
            {
                IntPtr shape = GetShapeAt(m_shapeCanvas, i);
                if (shape != IntPtr.Zero)
                {
                    //TODO1: 实际情况是根据业务对象（如红绿灯）构建内存对象，并维护其业务数据（如红灯填充red色，绿灯填充green色）
                    //TODO2: 通过图元的title或name属性，与业务对象关联上，根据业务状态值，更新图元的显示效果

                    //此处示例逻辑：仅找出title==flash的区域图元红闪
                    //        byte[] bys = new byte[200];
                    lock (gbys)
                    {
                        GetShapeType(shape, ref gbys[0]);
                    }
                    String sType = ByteToString(gbys);

                    Array.Clear(gbys, 0, gbys.Length);
                    //  byte[] bys2 = new byte[200];
                    lock (gbys)
                    {
                        GetShapeTitle(shape, ref gbys[0]);
                    }
                    String sTitle = ByteToString(gbys);

                    if ((sType == "ellipse" || sType == "rectangle") /* (sTitle == "flash")*/)      
                    {
                        if (m_isLighting)
                        {
                            color = rand.Next(17789504);
                        }
                        else
                        {
                            color = GetBackColor(m_shapeCanvas);
                        }
                        SetFillColor(m_shapeCanvas, shape, color);
                        m_isLighting = !m_isLighting;
                        ll++;
                    }
                }

            }
        }
    }
}
