• C# Wiform GDI+ 模拟图层功能


      在使用GDI+绘制图形时,为了实现如AutoCAD那样分层进行绘制各种类型的线,设计了一种模拟图层实现的方法。

      实现思路:将要绘制的图形点存放在不同的链表中,根据图层的可见性,绘制不同的图形。

      具体实现:

      一、设计图层类

      BitMapLayer类

      字段设计

    private string _name; 
    private bool _isVisible;
    private bool _isActive;
    private Bitmap bitmap;
    private List<PointF> _pointsList;
    View Code

      属性设计

    /// <summary>
            /// 名称
            /// </summary>
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            /// <summary>
            /// 可见性
            /// </summary>
            public bool IsVisible
            {
                get { return _isVisible; }
                set { _isVisible = value; }
            }
    
            /// <summary>
            /// 是否可激活
            /// </summary>
            public bool IsActive
            {
                get { return _isActive; }
                set { _isActive = value; }
            }
    
            /// <summary>
            /// 背景位图
            /// </summary>
            public Bitmap Bitmap
            {
                get { return bitmap; }
                set { bitmap = value; }
            }
    
            /// <summary>
            /// 点集合
            /// </summary>
            public List<PointF> PointsList
            {
                get { return _pointsList; }
                set { _pointsList = value; }
            }
    View Code

      构造函数

    public MyBitMap()
     {
     
    }
    
     public MyBitMap(string name)
    {
           this.Name = name;
     }
    
    public MyBitMap(string name, bool IsActive)
     {
            this.Name = name;
             this.IsActive = IsActive;
     }
    View Code

      二、窗体设计

      2.1 主窗体设计

      主窗体设置两个Button按钮,一个用来调用图层管理界面,另一个用来更新界面显示(需改进,将界面刷新设置为自动刷新)。

      同时设置鼠标点击事件,用来向图层的数据链表中增加坐标点数据。

          

          图1 主窗体界面

      (注:主窗体界面为缩小后显示的界面,具体大小可根据实际需要进行调整,下同)

      2.2 图层管理界面设计

      设置三个Button按钮,分别用来增加图层,删除图层和保存图层设置。一个DataGridView控件,用来显示、设置图层属性。

    图2 图层管理窗体界面

      另在设计公共函数及属性,便于主窗体调用。

      三、实现代码

      主窗体中代码:

    private List<MyBitMap> myBitMapList=new List<MyBitMap>();
    Random r = new Random();
    Random r1 = new Random();
    // 打开图层管理界面
    private void btnLayerManager_Click(object sender, EventArgs e)
    {
          LayerDialog ld = new LayerDialog(myBitMapList);
          if (ld.ShowDialog() != DialogResult.OK)
          {
               myBitMapList=ld.GetLayerList();
               RefreshDraw();
           }
     }
    
    // 主窗体加载是操作
    private void Form1_Load(object sender, EventArgs e)
            {
                MyBitMap myBitmap = new MyBitMap("Default");
                myBitmap.Bitmap = new Bitmap(this.Width, this.Height);
                myBitmap.PointsList = new List<PointF>();
                myBitMapList.Add(myBitmap);
            }
    
    // 更新界面
    private void btnDrawBitMap_Click(object sender, EventArgs e)
    {
        RefreshDraw();
    }
    
    // 鼠标点击事件
    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        for (int i = 0; i < myBitMapList.Count; i++)
        {
            if (myBitMapList[i].IsActive)
            {
                myBitMapList[i].PointsList.Add(new PointF(e.X,e.Y));
            }
        }
    }
    
    
    // 更新界面实现代码
    private void RefreshDraw()
    {
        Graphics g = this.CreateGraphics();
    
        List<MyBitMap> noVisibleList = new List<MyBitMap>();
        List<MyBitMap> VisibleList = new List<MyBitMap>();
    
        int isvisible = 0;
        for (int i = 0; i < myBitMapList.Count; i++)
        {
            if (myBitMapList[i].IsVisible)
            {
                isvisible++;
                VisibleList.Add(myBitMapList[i]);
            }
            else
                noVisibleList.Add(myBitMapList[i]);
        }
    
        g.Clear(this.BackColor);
    
        for (int i = 0; i < VisibleList.Count; i++)
        {
            if (VisibleList[i].PointsList.Count > 0)
                g.DrawLines(new Pen(Color.Red, 1), VisibleList[i].PointsList.ToArray()); 
        }
        
        g.Dispose();
    }
    View Code

      图层管理中代码:

    List<MyBitMap> myBitMapList;
    public LayerDialog( List<MyBitMap> bitMapList)
    {
        InitializeComponent();
        myBitMapList = new List<MyBitMap>();
        myBitMapList.AddRange(bitMapList);
        SetDataGrid();
    }
    
    private void LayerDialog_Load(object sender, EventArgs e)
    {
                  
    }
    
    private static int count=1;
    
    private void btnAdd_Click(object sender, EventArgs e)
    {
        MyBitMap bitmap = new MyBitMap("图层"+count.ToString());
        bitmap.Bitmap = new Bitmap(this.Width, this.Height);
        bitmap.PointsList = new List<PointF>();
        myBitMapList.Add(bitmap);
        count++;
        dataGridView1.DataSource = null;
        SetDataGrid();
    }
    
    private void SetDataGrid()
    {
        dataGridView1.DataSource = myBitMapList;
        dataGridView1.Columns[0].HeaderText = "Layer Name";
        dataGridView1.Columns[1].HeaderText = "Visible";
        dataGridView1.Columns[2].HeaderText = "Active";
        dataGridView1.Columns[3].Visible = false;
    }
    
    private void btnDelete_Click(object sender, EventArgs e)
    {
        if (dataGridView1.DataSource != null)
        {
            myBitMapList.RemoveAt(dataGridView1.CurrentRow.Index);
            dataGridView1.DataSource = null;
            SetDataGrid();
        }
    }
    
    private void btnSave_Click(object sender, EventArgs e)
    {
        int active = 0;
        for (int i = 0; i < myBitMapList.Count; i++)
            if (myBitMapList[i].IsActive)
                active++;
        if (active > 1)
            MessageBox.Show("只能有一个图层可以被激活!");
        else
            Close();
    }
    
    public List<MyBitMap> GetLayerList()
    {
        List<MyBitMap> tmpList = myBitMapList;
        return tmpList;
    }
    View Code

      文中不足之处,敬请各位指出,感激不尽。

  • 相关阅读:
    【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
    【Leetcode】91. Decode Ways
    记一次面经
    涨知识
    B-Tree 漫谈 (从二叉树到二叉搜索树到平衡树到红黑树到B树到B+树到B*树)
    涨知识
    HDU 1754 I Hate It 【线段树单点修改 维护区间最大值】
    POJ 1632 Vase collection【状态压缩+搜索】
    POJ 1011 Sticks 【DFS 剪枝】
    POJ 1088 滑雪 【记忆化搜索经典】
  • 原文地址:https://www.cnblogs.com/braceli/p/5442475.html
Copyright © 2020-2023  润新知