模仿MFC实现简单画图程序

来源:互联网 发布:linux elementary os 编辑:程序博客网 时间:2024/06/06 10:54
 CDocument.cs
using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;

namespace CsharpWinTest
{
    
public class CDocument
    
{
        ArrayList _myGlyphs 
= new ArrayList();

        
public ArrayList MyGlyphs
        
{
            
get return _myGlyphs; }
        }


        
public void AddGlyph(GraphicsObject.Glyph glyph)
        
{
            _myGlyphs.Add(glyph);
        }

    }

}

CWnd.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using GraphicsObject;

namespace CsharpWinTest
{
    
public partial class CWnd : Form
    
{
        
private CFrameWnd myFram;
        
private CWnd()
        
{
            InitializeComponent();
        }


        
public CWnd(CFrameWnd fram)
        
{
            InitializeComponent();
            myFram 
= fram;
            MdiParent 
= fram;
        }


        
private void OnPaint(object sender, PaintEventArgs e)
        
{
            Graphics g 
= e.Graphics;
            
foreach(Glyph o in myFram.TheDoc.MyGlyphs)
                o.Render(g);
        }


        
private void CWnd_FormClosed(object sender, FormClosedEventArgs e)
        
{
            myFram.MyForms.Remove(
this);
        }

    }

}

Glyph.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GraphicsObject
{
    
public abstract class Glyph
    
{
        
public abstract void Render(Graphics g);
    }


    
public class Line : Glyph
    
{
        
private Pen pen;
        
private Point p1, p2;

        
public Line(Pen p, Point p1, Point p2)
        
{
            pen 
= p;
            
this.p1 = p1;
            
this.p2 = p2;
        }


        
public override void Render(Graphics g)
        
{
            g.DrawLine(pen, p1, p2);
        }

    }


    
public class Triangle : Glyph
    
{
        
private Pen pen;
        
private Point p1, p2, p3;
        
public Triangle(Pen p, Point p1, Point p2, Point p3)
        
{
            pen 
= p;
            
this.p1 = p1;
            
this.p2 = p2;
            
this.p3 = p3;
        }


        
public override void Render(Graphics g)
        
{
            g.DrawLine(pen, p1, p2);
            g.DrawLine(pen, p1, p3);
            g.DrawLine(pen, p2, p3);
        }

    }


    
public class Circle : Glyph
    
{
        
private Pen pen;
        
private Point center;
        
private float radius;

        
public Circle(Pen p, Point c, float r)
        
{
            pen 
= p;
            center 
= c;
            radius 
= r;
        }


        
public override void Render(Graphics g)
        
{
            g.DrawEllipse(pen, center.X 
- (int)radius, center.Y - (int)radius, (int)radius * 2, (int)radius * 2);
        }

    }

}

CFrameWnd.cs
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 GraphicsObject;

namespace CsharpWinTest
{
    
public partial class CFrameWnd : Form
    
{
        
private CDocument theDoc;

        ArrayList _myForms 
= new ArrayList();
        
public ArrayList MyForms
        
{
            
get return _myForms; }
        }


        
public void AddForm(CWnd theForm)
        
{
            _myForms.Add(theForm);
        }


        
public CDocument TheDoc
        
{
            
get return theDoc; }
            
set { theDoc = value; }
        }


        
public CFrameWnd()
        
{
            InitializeComponent();
            
this.IsMdiContainer = true;

            theDoc 
= new CDocument();
        }


        
private void NewForm_Click(object sender, EventArgs e)
        
{
            CWnd theForm 
= new CWnd(this);
            
//theForm.Parent = this;
            
//theForm.MdiParent = this;
            theForm.Show();
        }


        
private void 画线LToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            Pen p 
= Pens.Red;
            Line theLine 
= new Line(p, new Point(1020), new Point(40100));
            theDoc.MyGlyphs.Add(theLine);
        }


        
private void 画三角形TToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            Pen p 
= Pens.Green;
            Triangle triangle 
= new Triangle(p, new Point(10010), new Point(10100), new Point(20080));
            theDoc.MyGlyphs.Add(triangle);
        }


        
private void 画圆CToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            Pen p 
= Pens.Blue;
        }

    }

}

原创粉丝点击