Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC

2D Drawing - Doc/View architecture

Rate me:
Please Sign up or sign in to vote.
3.79/5 (7 votes)
20 Jul 2012CPOL3 min read 44.6K   4.2K   32   7
This article is a simple geometry library for drawing lines, circles, arcs, text, etc.

Introduction

GeoIO lib is a simple geometry library that encapsulates the drawing of various geometrical entities (like lines, circle, arc, text etc), storing them in a list with support for undo/redo operations. There is a sample application called Cad2D attached which exposes some of the basic drawing and serilaization functionality of the GeoIO library.  

Background  

Couple of developers/programmers have come across a situation to develop simple application to draw 2-Dimensional geometry entities. However they are blocked due to many reasons like lack of time, unwilling to explore OpenGL, develop the module from base etc. The MFC set of classes (DeviceContext) gives some good functionality to draw simple entities like lines, circles, rectangles etc, but a first timer may take sometime to learn, understand and experiment it. GeoIO library has encapsulated the complete functionality of MFC into set of core geometry classes. All that you have to do is add this module into your project and start with it. The module also supports drawing, dragging operation OnMouseMove handler and serialization.

Using the code  

GeoIO is simple MFC DLL. Add this as dynamic-linked or static-linked to your project. Initially the usage of this module might look difficult, but once you read the below fundamental concepts you should sail through easily. Open the sample project Cad2D (SDI MFC application) which uses GeoIO to draw 2D basic geometry objects. Please explore this sample project to learn and understand the usage of GeoIO. 

 Image 1

NOTE: The project has .vcproj and .vcxproj files so it should open in VS2010 and earlier versions of Visual Studio. 

Geometry classes in GeoIO: 

  • CPrimitive - the principle base class for all the below mentioned classes.
  • CGeoArc - class to draw and serialize an arc 
  • CGeoCircle - class to draw and serialize a circle
  • CGeoLine - class to draw and serialize a line
  • CGeoPolybezier - class to draw and serialize a polybezier
  • CGeoPolygon  - class to draw and serialize a polygon
  • CGeoRectangle - class to draw and serialize a rectangle
  • CGeoText - class to draw and serialize a string

Note: Some drawing logic have been inspired from various online articles. 

Collection classes:

  • CEntityList - class to hold objects of CPrimitive (like CGeoArc, CGeoLine, CGeoRectangle, etc).
  • CLayerList - class to hold objects of CLayer 
  • CLayer - class stores an instance of CEntityList. A CLayer object is identified by a layer ID.

So, in your application follow the below procedure:

C++
CLayer *pLayer = new CLayer(); // member variable of your window class
CPrimitive *pEntity = new CGeoLine(); // create an entity of time line
pEntity->m_StPnt = CPoint(10, 10); 
// set the start point of the line 
pEntity->m_EndPoint =  CPoint(100, 100); // set the end point of the line 
pEntity->m_clr = RGB(255, 255, 255); // set the color of the line
pLayer->AddEntityToLayer(pEntity); // store it to the layer class 

In the OnDraw(CDC *pDC) method of your window class, call the

C++
pLayer->DrawLayer(pDC); // NOTE: pLayer should be a member of your window class that is created in the constructor.

Also note that in the example above I have hard-coded the pEntity->m_StPnt and pEntity->m_EndPnt to some random CPoint values. Ideally this should be done in the OnLButtonDown handler which gives you the CPoint data as a parameter.  

Concept of layers 

In your application, you can create 'N' number layers and maintain them in the CLayerList class. In the OnDraw() method of your window class, you can select which CLayer to be drawn. If you wish to draw all the layers then iterate each layer in the CLayerList and then call DrawLayer() method of the CLayer

Image 2

History 

Initial upload: 14 July 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
India India
Have great experience in design, architecture & delivery of services built using Microservices architecture, Enterprise Server applications using scaled RDBMS and NoSQL databases. Have used a variety open-source softwares to build solutions like - Apache Ignite, REDIS Distributed Cache, Apache Kafka as Service BUS, Elasticsearch (ELK stack for log aggregation, queries and visualisation), MongoDB and MySQL databases. In the past, I have developed multiple applications & services for Windows & Android platforms as well.

Interested in high availability and scalability articles.

Comments and Discussions

 
QuestionCan I get part of code used for dragging operation OnMouseMove handler and serialization. Pin
Member 935377622-Aug-12 22:47
Member 935377622-Aug-12 22:47 
AnswerRe: Can I get part of code used for dragging operation OnMouseMove handler and serialization. Pin
Member 935377622-Aug-12 23:13
Member 935377622-Aug-12 23:13 
GeneralRe: Can I get part of code used for dragging operation OnMouseMove handler and serialization. Pin
Sunil P V24-Aug-12 1:27
Sunil P V24-Aug-12 1:27 
AnswerRe: Can I get part of code used for dragging operation OnMouseMove handler and serialization. Pin
Sunil P V24-Aug-12 1:29
Sunil P V24-Aug-12 1:29 
QuestionNeeds work Pin
Dave Kreskowiak20-Jul-12 2:16
mveDave Kreskowiak20-Jul-12 2:16 
AnswerRe: Needs work Pin
Sunil P V20-Jul-12 6:55
Sunil P V20-Jul-12 6:55 
QuestionScope for improvements Pin
Sunil P V19-Jul-12 20:48
Sunil P V19-Jul-12 20:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.