Article | 41023350 |
Type | HowTo |
Product | Engine |
Version | 4.1.6.1028 |
Date Added | 12/23/2003 |
Submitted by | itzik |
Keywords | |
Subject
add VectorDraw in a CView class and draw simple rectangles using CRect class in VC++ 6.0
Summary
How to add VectorDraw in a CView class and draw simple rectangles using CRect
class in VC++ 6.0
Solution
To add VectorDraw in a CView class:
you must derive the CView class from
CFormView as follow:
1.Create a Single Document
project:
2.Open the resources dialogs and
add a dialog IDD_FORMVIEW:
3.Right click on form and select
insert activex control->VectorDraw Standard 4.x
4.From Menu select
View->ClassWizard and add a class for IDD_FORMVIEW named CvdrawForm with base
class CFormView
5.On the MFC CLassWizard select
Member Variables tab add double-click on IDC_VDStd1,Select ok to automatically
generate C++ classes for VectorDraw
Select ok to all next dialogs and
finally add a member variable named m_vdraw.Select ok to exit from
ClassWizard.
6.In the ClassView explore the
CSimpleViewApp and double click on the InitInstance method to change the
code.
7.Change the following
code:
pDocTemplate = new
CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSimpleViewDoc),
RUNTIME_CLASS(CMainFrame), //
main SDI frame window
RUNTIME_CLASS(CSimpleViewView));
as follow:
pDocTemplate = new
CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSimpleViewDoc),
RUNTIME_CLASS(CMainFrame), //
main SDI frame window
RUNTIME_CLASS(CvdrawForm
));
8.Go to the top of this
file(simpleview.cpp) and add the
#include
"vdrawForm.h"
#include "vddocument.h"
#include "vdutility.h"
#include "vdCommand.h"
9.In the ClassView right click on
CvdrawForm and select Add Windows Message Handler and double click on WM_SIZE
message.
10.Select Edit existing to go in
the created method and modify it as follow:
void CvdrawForm::OnSize(UINT
nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
// TODO: Add your
message handler code here
if(::IsWindow(m_vdraw.GetSafeHwnd()))
m_vdraw.MoveWindow(0,0,cx,cy);
}
10.In the ClassView right click on
CSimpleViewApp and select add virtual function.Select OpenDocumentFile from the
list.Select Edit Existing and
modify the code as
follow:
CDocument*
CSimpleViewApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
// TODO: Add your specialized code here
and/or call the base class
CDocument* pDoc =
CWinApp::OpenDocumentFile(lpszFileName);
CMainFrame
*pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
// Get the active view
CvdrawForm *pView = (CvdrawForm *) pFrame->GetActiveView();
if(!pView->m_vdraw.GetActiveDocument().Open(lpszFileName,0,0)) return
NULL;
pView->m_vdraw.GetCommandAction().Zoom(COleVariant(_T("E")),COleVariant(_T("")),COleVariant(_T("")));
return pDoc;
}
11.From menu select
View->ClassWizard.In the Class name select CSimpleViewApp .On the Object Ids
list select
ID_FILE_NEW and on the Messages double
click on COMMAND.A new function OnFileNew will be added.
On the Object Ids list select
ID_FILE_OPEN and on the Messages
double click on COMMAND.A new function OnFileOpen will be added.
Modify the code as follow:
void
CSimpleViewApp::OnFileOpen()
{
// TODO:
Add your command handler code here
CMainFrame *pFrame =
(CMainFrame*)AfxGetApp()->m_pMainWnd;
//
Get the active view
CvdrawForm *pView =
(CvdrawForm *) pFrame->GetActiveView();
COleVariant
filename =
pView->m_vdraw.GetUtility().GetOpenFileName(0,_T(""),0);
if(filename.vt == VT_EMPTY) return;
OpenDocumentFile(CString(filename.bstrVal));
}
void CSimpleViewApp::OnFileNew()
{
// TODO: Add your command handler code
here
CWinApp::OnFileNew();
CMainFrame
*pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
// Get
the active view
CvdrawForm *pView = (CvdrawForm *)
pFrame->GetActiveView();
pView->m_vdraw.GetActiveDocument().New();
pView->m_vdraw.Redraw();
}
12.You can add other commands like
CmdLine Handled from CvdrawForm class directly see OnDrawLine
example:
#include
"vdcommand.h"
void CvdrawForm::OnDrawLine()
{
// TODO: Add your command handler
code here
m_vdraw.GetCommandAction().CmdLine(COleVariant(_T("USER")));
}
13.Build and run your
project
draw simple rectangles using CRect
class
1.In the Resources Dialog section
Double-Click on IDD_FORMVIEW
2.RightClick on VectorDraw control
and select Events...
3.From the EventList select -
double click on Draw event and add the
OnDrawVdstd1 member function.
Select Edit Existing to modify the code as
follow:
#include "vdRender.h"
void
CvdrawForm::OnDrawVdstd1(LPDISPATCH Render, short FAR* Cancel)
{
//
TODO: Add your control notification handler code here
CvdRender
rend;
rend.AttachDispatch(Render,FALSE);
CDC *dc =
CDC::FromHandle((HDC)rend.GetHdc());
CRect
rc(0,0,100,200);
CPen pen(0,0,RGB(255,0,0));
CPen *oldpen =
dc->SelectObject(&pen);
CBrush
brush(RGB(0,255,0));
CBrush *oldbrush =
dc->SelectObject(&brush);
dc->Rectangle(rc);
dc->SelectObject(oldpen);
dc->SelectObject(oldbrush);
}
notice:To convert from pixel to View
coordinates and the opposite use the following VectorDraw methods:
PixelToVdView and
VdViewToPixel