Click here to Skip to main content
15,909,324 members
Articles / Desktop Programming / MFC
Article

Programmable Vector Graphics

Rate me:
Please Sign up or sign in to vote.
1.10/5 (16 votes)
29 Dec 20041 min read 90K   2.6K   43   18
Creating vector graphics editor, implementing vector graphics ActiveX.

Sample Image - ProgrammVectorGraphics.jpg

Introduction

What is Vector Graphics ActiveX?

Vector Graphics ActiveX is the free and easy-to-use graphics solution for creating technical drawings, illustrations, charts, diagrams and much more. It offers a wide range of new features and enhancements designed to increase productivity and extend possibilities of your applications.

Why use Vector Graphics ActiveX?

Vector Graphics ActiveX allows developers to easily create and manage 2D drawing projects and reduce development time. Also it offers a visual development environment that can be either graphical user interface (GUI) or code driven. It provides over 1000 methods, properties and events and is fully object oriented.

Implementation

First of all, create an empty MFC project with ATL support. Create an empty form, put Vector Graphics ActiveX on it and generate the generated IDispatch wrapper class.

Don't forget to include the .tlb file:

# import "PGFTLB\prographer.tlb" no_namespace named_guids

The next step is to add member variables to our form class:

CPGAX          m_PGAX;
IDocumentPtr   m_spDocument;
IWindowPtr     m_spWindow;

To handle events from the control you should create the class CComInnerObject and add two variables:

template<class T> class CComInnerObject : public CComObjectNoLock<T>
{
 STDMETHOD_(ULONG, Release)()
 {
  return InternalRelease();
 }
};

CComInnerObject<CDocEventSink> m_DocEventSink;
CComInnerObject<CWndEventSink> m_WndEventSink;

The class that describes the form should look like this:

// CPGAXTESTDlg dialog
class CPGAXTESTDlg : public CDialog
{
 DECLARE_DYNAMIC(CPGAXTESTDlg)
public:
 CPGAXTESTDlg(CWnd* pParent = NULL);
 virtual ~CPGAXTESTDlg();
 // Dialog Data
 enum { IDD = IDD_PGAXDLG };
protected:
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
 DECLARE_MESSAGE_MAP()
private:
 class CDocEventSink :
  public CComObjectRootEx<CComSingleThreadModel>,
  public IDispEventImpl<1, CDocEventSink, &DIID_IDocumentEvents, 
             &LIBID_ProGrapherFoundation, 1, 0>
 {
 public:
  CDocEventSink()
  {
   m_pDlg = NULL;
  }
 public:
  CPGAXTESTDlg* m_pDlg;
 private:
  // Interface map
  BEGIN_COM_MAP(CDocEventSink)
  END_COM_MAP()
  BEGIN_SINK_MAP(CDocEventSink)
   SINK_ENTRY_EX(1, DIID_IDocumentEvents, 1, OnOpen)
  END_SINK_MAP()
  void _stdcall OnOpen();
 };
 class CWndEventSink :
  public CComObjectRootEx<CComSingleThreadModel>,
  public IDispEventImpl<1, CWndEventSink, &DIID_IWindowEvents, 
                               &LIBID_ProGrapherFoundation, 1, 0>
 {
 public:
   CWndEventSink()
   {
    m_pDlg = NULL;
   }
 public:
   CPGAXTESTDlg* m_pDlg;
   double   ptX, ptY;
 private:
   // Interface map
   BEGIN_COM_MAP(CWndEventSink)
   END_COM_MAP()
   BEGIN_SINK_MAP(CWndEventSink)
    SINK_ENTRY_EX(1, DIID_IWindowEvents, 1, OnMouseMove)
    SINK_ENTRY_EX(1, DIID_IWindowEvents, 2, OnLButtonDown)
    SINK_ENTRY_EX(1, DIID_IWindowEvents, 3, OnLButtonUp)
   END_SINK_MAP()
   void _stdcall OnMouseMove(long Flags, double X, double Y);
   void _stdcall OnLButtonDown(long Flags, double X, double Y);
   void _stdcall OnLButtonUp(long Flags, double X, double Y);
 };
 public:
 CComInnerObject<CDocEventSink> m_DocEventSink;
 CComInnerObject<CWndEventSink> m_WndEventSink;
 CPGAX       m_PGAX;
 IDocumentPtr     m_spDocument;
 IWindowPtr      m_spWindow;
 IShapePtr      m_spShape;
private:
 virtual BOOL OnInitDialog();
public:
 afx_msg void OnDestroy();
 afx_msg void OnBnClickedLoad();
 afx_msg void OnBnClickedNew();
 afx_msg void OnBnClickedZoomin();
 afx_msg void OnBnClickedZoomout();
 afx_msg void OnBnClickedOk();
 afx_msg void OnBnClickedGenbtn();
};

Initialization is very simple and looks like:

IDispatchPtr spDispatch;
 spDispatch.Attach(m_PGAX.get_Document(), true);
 m_spDocument = spDispatch;
 m_spWindow = m_spDocument->Window;
 m_DocEventSink.m_pDlg = this;
 m_WndEventSink.m_pDlg = this;
 m_DocEventSink.DispEventAdvise(m_spDocument);
 m_WndEventSink.DispEventAdvise(m_spWindow);

Now we are ready to work with graphics. Let's create the layer and text:

 ILayerPtr spLayer;
 spLayer = m_spDocument->CreateLayer(-1, L"Layer");
 spLayer->Active = VARIANT_TRUE;
 ITextClassPtr spTextClass;
 spTextClass = spLayer->CreateClass(ciText, NULL, 0, 0);
 spTextClass->Styles->Alignment = taCenter;
 spTextClass->Create(0.01, 0.01, _bstr_t("Move this text" 
    " using\r\nmouse or open any\r\ndocument and move\r\nany shapes."));

Conclusion

Vector Graphics ActiveX is the easiest way to add vector graphics support to your applications and improve their power. Moreover developers get full control of all objects at run-time so they can not only process graphic documents, but analyze and visualize any kind of processes and much more.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNeed source. Pin
JimD.999927-Jan-08 5:12
JimD.999927-Jan-08 5:12 
QuestionHow to use AddRef and QueryInterface Pin
theMirror200613-Feb-07 17:58
theMirror200613-Feb-07 17:58 
Questionhow to create namespace in VC++ Pin
lovemaggic12-Feb-06 20:08
lovemaggic12-Feb-06 20:08 
GeneralI need the source of the OCX Pin
What's my name?5-Nov-05 14:54
What's my name?5-Nov-05 14:54 
QuestionIs the source of the OCX free? Pin
songzd26-Jul-06 16:45
songzd26-Jul-06 16:45 
GeneralCan&#180;t find CPGAX wrapper under VC6.0 Pin
kiukeluke23-Mar-05 10:57
kiukeluke23-Mar-05 10:57 
GeneralExcellent Library Pin
kanstey8-Feb-05 15:39
kanstey8-Feb-05 15:39 
GeneralRe: Excellent Library Pin
willibillibon8-Feb-05 17:13
willibillibon8-Feb-05 17:13 
GeneralCan't creat Wrapper class CPGAX Pin
FriedhelmEichin8-Feb-05 4:22
FriedhelmEichin8-Feb-05 4:22 
GeneralRe: Can't creat Wrapper class CPGAX Pin
StasSV8-Feb-05 17:20
StasSV8-Feb-05 17:20 
GeneralRe: Can't creat Wrapper class CPGAX Pin
FriedhelmEichin8-Feb-05 20:43
FriedhelmEichin8-Feb-05 20:43 
Generaldisplay bitmap Pin
HansBys4-Feb-05 4:52
HansBys4-Feb-05 4:52 
GeneralRe: display bitmap Pin
Anonymous7-Feb-05 0:58
Anonymous7-Feb-05 0:58 
GeneralProGrapher.exe crashed! Pin
hzx30-Dec-04 23:00
hzx30-Dec-04 23:00 
GeneralRe: ProGrapher.exe crashed! Pin
StasSV31-Dec-04 0:23
StasSV31-Dec-04 0:23 
GeneralRe: ProGrapher.exe crashed! Pin
hzx31-Dec-04 16:10
hzx31-Dec-04 16:10 
Questionwhere's the demo? Pin
Lee Middle30-Dec-04 14:33
Lee Middle30-Dec-04 14:33 
I get only a 1k file when I try to download the demo project.
AnswerRe: where's the demo? Pin
StasSV30-Dec-04 18:36
StasSV30-Dec-04 18:36 

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.