模仿MFC实现简单画图程序

 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;
        }

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值