Browse DevX
Sign up for e-mail newsletters from DevX


GDI+ Drawing Page, Part I : Page 7

With the .NET Forms and Drawing class libraries you can create a drawing application C! la Visio that allows users to drag and draw shapes, select those shapes, and move them about the form.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Saving and Opening a Document
The last requirement of this control's library was that it be able to save a document to disk and re-open it at a later time. To give users this ability, you will add both a Save and Open method to the PageControl. These methods save and open instances of the PageControl's Document class to create and open binary versions of the Document class.

To manage this you will first mark the Document class (and its associated classes) as Serializeable. This allows the .NET Framework to serialize and de-serialize an object into one of multiple formats (in our case binary). You mark the class via an Attribute class as follows:

   public class Document
Next, create the Save routine. This routine uses BinaryMessageFormatter to serialize the Document class out to a file as follows:

   public void Save(string fileName)
     Formatters.Binary.BinaryFormatter bf = 
       new Formatters.Binary.BinaryFormatter();
       new FileStream(fileName, 
       FileMode.Create), this.m_doc);   
To open the serialized class you need to create another binary formatter. This time you call the Deserialize method and cast the results into a new Document instance that gets stored as the PageControl's associated Document instance. The code looks like the following:

   Formatters.Binary.BinaryFormatter bf = 
   new Formatters.Binary.BinaryFormatter();
   m_doc = new Document();
   m_doc = (Document)bf.Deserialize(
     new FileStream(fileName, FileMode.Open));
Surprisingly, you've done everything necessary for saving and opening instances of the custom Document class. Users can save their documents and open them at a later time. Of course, a developer that consumes the PageControl will still have to wire up the actual interaction with the file system.

The Application Container (Main)
You now have a fully functioning, version 1 PageControl. However, to see it in action (and to test it) you need to create an application that consumes the control. Keeping in mind the Visio paradigm, it makes sense to create a container for the application that could host multiple new documents. To handle this requirement you simply create a standard Windows Form and set its IsMdiContainer property to true. An MDI container also offers a very familiar paradigm to users.

You can add a number of controls to this main form to make it more useful. To handle basic navigation, add a menu bar and toolbar. Next, add the open and save common dialog controls. These controls will help you quickly and consistently write the code for interacting with the file system during opening and saving a Document. Finally, let's add the color dialog control to allow users to choose a fill color for drawing elements that they'll draw on a page. Figure 3 shows an example of this MDI container.

Lars Powers, MCSD has over 10 years of experience analyzing business problems and developing software solutions. Most of his experience centers on leading development teams and writing software in Microsoft development environments. Lars & Mike have formed brilliantStorm, a partnership focused on providing developers with .NET mentoring and consulting. Their latest book is now available: Visual Basic Programmer's Guide to the .NET Framework Class Library. lars@brilliantSTORM.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date