loading, creating, saving vector drawings to Word DOC or RTF file from code?

M

mad.scientist.jr

I'm interested in coding a simple, easy to use, open source vector
drawing program similar to MS Word's drawing tool bar or MS Paint (but
vector-based) to run on PocketPCs (using the .NET compact framework).

It would let the user draw & fill boxes, ellipses, polygons, lines,
add text, etc, and then save their work to a Word DOC or RTF.

Eventually it might let the user make an animated flipbook (frame-by-
frame) or define tweening and save their animations to files that can
be edited or played in Flash (FLA or SWF) or Silverlight.

Can anyone suggest where to go to find code samples (either C# or
VB.NET or VB6) that do anything similar?

Any pointers much appreciated...
 
M

Mike D Sutton

I'm interested in coding a simple, easy to use, open source vector
drawing program similar to MS Word's drawing tool bar or MS Paint (but
vector-based) to run on PocketPCs (using the .NET compact framework).

It would let the user draw & fill boxes, ellipses, polygons, lines,
add text, etc, and then save their work to a Word DOC or RTF.

Eventually it might let the user make an animated flipbook (frame-by-
frame) or define tweening and save their animations to files that can
be edited or played in Flash (FLA or SWF) or Silverlight.

Can anyone suggest where to go to find code samples (either C# or
VB.NET or VB6) that do anything similar?

Watch the cross-posting, you're including lots of groups which don't cater for .NET development so you're unlikely to
get particularly relevant answers there.
Here's a very simple demo though demonstrating how you could create a simple system to draw, store and save shapes:

***
public partial class Form1 : Form {
CShapeList Shapes = new CShapeList();
CLine DrawLine;

public Form1() {
InitializeComponent();

// Draw some lines to get us started
Shapes.Add(new CLine(new Pen(Color.Green),
new Point(10, 10), new Point(110, 110)));
Shapes.Add(new CLine(new Pen(Color.Red, 2.0f),
new Point(110, 10), new Point(10, 110)));
}

private void Form1_Paint(object sender, PaintEventArgs e) {
using (Graphics g = e.Graphics)
foreach (CShape Shape in Shapes)
Shape.Draw(g);
}

private void Form1_MouseDown(object sender, MouseEventArgs e) {
DrawLine = new CLine(new Point(e.X, e.Y), new Point(e.X, e.Y));
Shapes.Add(DrawLine);
}

private void Form1_MouseMove(object sender, MouseEventArgs e) {
DrawLine.End = new Point(e.X, e.Y);
Refresh();
}

private void Form1_MouseUp(object sender, MouseEventArgs e) {
DrawLine = null;
}
}

public class CShapeList : List<CShape> {
public void SaveToXML(string inPath) {
// Create new document and root node
XmlDocument Doc = new XmlDocument();
XmlElement Root = Doc.CreateElement("shapes");

// Append processing instruction to document
Doc.AppendChild(Doc.CreateProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"UTF-8\""));
Doc.AppendChild(Root);

// Output shapes
foreach (CShape Shape in this)
Shape.SaveToXML(Root);

// Save document
Doc.Save(inPath);
}
}

public abstract class CShape {
protected Pen m_Pen;

public Pen pen {
get { return m_Pen; }
set { m_Pen = value; }
}

public abstract string NodeName { get; }

public CShape() {
m_Pen = new Pen(Color.Black, 1.0f);
}

public CShape(Pen inPen) {
m_Pen = inPen;
}

public abstract void Draw(Graphics g);

private XmlElement MakePenNode(XmlElement inParent) {
// Create XML node and populate with pen information
XmlElement PenNode = inParent.OwnerDocument.CreateElement("Pen");
PenNode.SetAttribute("Colour", m_Pen.Color.ToArgb().ToString("x"));
PenNode.SetAttribute("Width", m_Pen.Width.ToString());
return PenNode;
}

public void SaveToXML(XmlElement inParentNode) {
// Get the shape class to selialise itself
XmlElement ShapeNode = DoSaveToXML(inParentNode);

if (ShapeNode != null) {
// Append current pen node and add to parent
ShapeNode.AppendChild(MakePenNode(inParentNode));
inParentNode.AppendChild(ShapeNode);
}
}

protected abstract XmlElement DoSaveToXML(XmlElement inParentNode);
}

public class CLine : CShape {
protected Point m_Start;
protected Point m_End;

const string m_NodeName = "Line";

public Point Start {
get { return m_Start; }
set { m_Start = value; }
}

public Point End {
get { return m_End; }
set { m_End = value; }
}

public override string NodeName {
get { return m_NodeName; }
}

public CLine(Point inStart, Point inEnd) {
m_Start = inStart;
m_End = inEnd;
}

public CLine(Pen inPen, Point inStart, Point inEnd) : base(inPen) {
m_Start = inStart;
m_End = inEnd;
}

public override void Draw(Graphics g) {
// Draw line
g.DrawLine(m_Pen, m_Start.X, m_Start.Y, m_End.X, m_End.Y);
}

protected override XmlElement DoSaveToXML(XmlElement inParentNode) {
// Create XML node
XmlElement ShapeNode = inParentNode.OwnerDocument.CreateElement(NodeName);

// Append attributes
ShapeNode.SetAttribute("StartX", m_Start.X.ToString());
ShapeNode.SetAttribute("StartY", m_Start.Y.ToString());
ShapeNode.SetAttribute("EndX", m_End.X.ToString());
ShapeNode.SetAttribute("EndY", m_End.Y.ToString());

// Return completed node
return ShapeNode;
}
}
***

Only lines are implemented in this demo, however you can create additional classes off CShape which support other
drawing primitives.
Editing shapes can be achieved by adding in the ability to hit-test each shape type, then pass mouse events down to
those shapes so they can handle their own editing etc.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top