How To Create Graphics Application Using VC++
Rate this article
0 out of 5
In this tutorial we will show you how to use the graphics libraries in VC++. The the graphics library concept is explained using an example MatrixGrid which will use some of the common graphics functions like Line drawing, pen, fornt, paint etc.  After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Developing Graphics Application Part1



Developing Graphics Application Part2


Source Code
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MatrixGrid.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
ID_SEPARATOR,           // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar ");
return -1;      // fail to create
}

if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
 sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar ");
return -1;      // fail to create
}

// TODO: Delete these three lines if you don't want the toolbar to
//  be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);

return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs

cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
| WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_MAXIMIZE;

return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

MatrixGrid.cpp
 
// MatrixGrid.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MatrixGrid.h"

#include "MainFrm.h"
#include "MatrixGridDoc.h"
#include "MatrixGridView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridApp

BEGIN_MESSAGE_MAP(CMatrixGridApp, CWinApp)
//{{AFX_MSG_MAP(CMatrixGridApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridApp construction

CMatrixGridApp::CMatrixGridApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMatrixGridApp object

CMatrixGridApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridApp initialization

BOOL CMatrixGridApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));

LoadStdProfileSettings();  // Load standard INI file options (including MRU)

// Register the application's document templates.  Document templates
//  serve as the connection between documents, frame windows and views.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMatrixGridDoc),
RUNTIME_CLASS(CMainFrame),       // main SDI frame window
RUNTIME_CLASS(CMatrixGridView));
AddDocTemplate(pDocTemplate);

// Enable DDE Execute open
EnableShellOpen();
RegisterShellFileTypes(TRUE);

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;

// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
m_pMainWnd->UpdateWindow();

// Enable drag/drop open
m_pMainWnd->DragAcceptFiles();

return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CMatrixGridApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridApp message handlers

MatrixGridDoc.cpp

// MatrixGridDoc.cpp : implementation of the CMatrixGridDoc class
//

#include "stdafx.h"
#include "MatrixGrid.h"

#include "MatrixGridDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc

IMPLEMENT_DYNCREATE(CMatrixGridDoc, CDocument)

BEGIN_MESSAGE_MAP(CMatrixGridDoc, CDocument)
//{{AFX_MSG_MAP(CMatrixGridDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc construction/destruction

CMatrixGridDoc::CMatrixGridDoc()
{
// TODO: add one-time construction code here
m_iColoums=COL;
m_iHeight=HEIGHT ;
m_iRow=ROW;
m_iWidth=WIDTH;
m_iXoffset=XOFFSET;
m_iYoffset=YOFFSET;
}

CMatrixGridDoc::~CMatrixGridDoc()
{
}

BOOL CMatrixGridDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;

// TODO: add reinitialization code here
// (SDI documents will reuse this document)

return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc serialization

void CMatrixGridDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc diagnostics

#ifdef _DEBUG
void CMatrixGridDoc::AssertValid() const
{
CDocument::AssertValid();
}

void CMatrixGridDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc commands

MatrixGridView.cpp

// MatrixGridView.cpp : implementation of the CMatrixGridView class
//

#include "stdafx.h"
#include "MatrixGrid.h"

#include "MatrixGridDoc.h"
#include "MatrixGridView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView

IMPLEMENT_DYNCREATE(CMatrixGridView, CScrollView)

BEGIN_MESSAGE_MAP(CMatrixGridView, CScrollView)
//{{AFX_MSG_MAP(CMatrixGridView)
ON_COMMAND(ID_ZOOM_25, OnZoom25)
ON_COMMAND(ID_ZOOM_50, OnZoom50)
ON_COMMAND(ID_ZOOM_200, OnZoom200)
ON_COMMAND(ID_ZOOM_100, OnZoom100)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView construction/destruction

CMatrixGridView::CMatrixGridView()
{
// TODO: add construction code here
m_iColIndex=1;
m_iRowIndex=1;
}

CMatrixGridView::~CMatrixGridView()
{
}

BOOL CMatrixGridView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs

return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView drawing

void CMatrixGridView::OnDraw(CDC* pDC)
{
CMatrixGridDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
InitializeCell();
FillColor(pDC);
SetScrollPosition();
DrawGrid(pDC);
}


void CMatrixGridView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();

CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView printing

BOOL CMatrixGridView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CMatrixGridView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CMatrixGridView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView diagnostics

#ifdef _DEBUG
void CMatrixGridView::AssertValid() const
{
CScrollView::AssertValid();
}

void CMatrixGridView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}

CMatrixGridDoc* CMatrixGridView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatrixGridDoc)));
return (CMatrixGridDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView message handlers

void CMatrixGridView::DrawGrid(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
    int iIndex;
for(iIndex=0;iIndex<=pDoc->m_iColoums;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset);
pDC->LineTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset+(pDoc->m_iHeight*pDoc->m_iRow));
}
for(iIndex=0;iIndex<=pDoc->m_iRow;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset,pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
pDC->LineTo(pDoc->m_iXoffset+(pDoc->m_iWidth*pDoc->m_iColoums),pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
}
}

void CMatrixGridView::OnZoom25() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.25;
pDoc->m_iWidth=WIDTH*.25;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnZoom50() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.50;
pDoc->m_iWidth=WIDTH*.50;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnZoom200() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*2;
pDoc->m_iWidth=WIDTH*2;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnZoom100() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*1;
pDoc->m_iWidth=WIDTH*1;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnEditUndo() 
{
AfxMessageBox("Add code for Undo Operation here");
}

void CMatrixGridView::OnEditPaste() 
{
AfxMessageBox("Add code for Paste Operation here");
}

void CMatrixGridView::OnEditCut() 
{
AfxMessageBox("Add code for Cut Operation here");
}

void CMatrixGridView::OnEditCopy() 
{
AfxMessageBox("Add code for Copy Operation here");
}


/*-------------------------------------------- Function Header ---------------------------------------------*
Function Name  : CMatrixGridView::SetScrollPosition
Description      : Set the scroll sizes here depending on the zoom Factor
Return Type      : void 
Warning           : 
------------------------------------------------------------------------------------------------------------*/

void CMatrixGridView::SetScrollPosition()
{
CMatrixGridDoc *pDoc=GetDocument();
int iHorizontalPosition=pDoc->m_iXoffset+pDoc->m_iColoums*pDoc->m_iWidth+100;
int iVerticalPosition=pDoc->m_iYoffset+pDoc->m_iRow*pDoc->m_iHeight+100;
SetScrollSizes(MM_TEXT,CSize(iHorizontalPosition,iVerticalPosition));
}

void CMatrixGridView::InitializeCell()
{
CMatrixGridDoc *pDoc=GetDocument();
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.SetRect(0,0,pDoc->m_iWidth,pDoc->m_iHeight);
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.OffsetRect(pDoc->m_iXoffset+(pDoc->m_iWidth*iColIndex),pDoc->m_iYoffset+(pDoc->m_iHeight*iRowIndex));
        }
}
}

void CMatrixGridView::FillColor(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);

CBrush WhiteBrush(RGB(255,255,255));
CBrush RedBrush(RGB(255,0,0));
CBrush BlueBrush(RGB(0,0,255));
CBrush GreenBrush(RGB(0,255,0));
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{   
switch(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor)
{
case WHITE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&WhiteBrush);
break;
}
case RED:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&RedBrush);
break;
}
case GREEN:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&GreenBrush);
break;
}
case BLUE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&BlueBrush);
break;
}
case CUSTOM:
{  
CBrush CustomBrush(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_cColor);
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&CustomBrush);
break;
}
}
pDC->DrawText(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_strText,pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,DT_LEFT);
}
}

void CMatrixGridView::OnLButtonDown(UINT nFlags, CPoint point) 
{
CMatrixGridDoc *pDoc=GetDocument();
CPoint point1;

point1=GetScrollPosition();
point+=point1;
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{   
   if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.PtInRect(point))
{
m_iColIndex=iColIndex;
m_iRowIndex=iRowIndex;

if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor>BLUE )
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor=WHITE;
}
else
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor++;
}
}
}
}
Invalidate();

CScrollView::OnLButtonDown(nFlags, point);
}

Cell.cpp

// Cell.cpp: implementation of the CCell class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MatrixGrid.h"
#include "Cell.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCell::CCell()
{
   m_iColor=1;
   m_strText="";
  
}

CCell::~CCell()
{

}

MatrixGridView.cpp

// MatrixGridView.cpp : implementation of the CMatrixGridView class
//

#include "stdafx.h"
#include "MatrixGrid.h"

#include "MatrixGridDoc.h"
#include "MatrixGridView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView

IMPLEMENT_DYNCREATE(CMatrixGridView, CScrollView)

BEGIN_MESSAGE_MAP(CMatrixGridView, CScrollView)
//{{AFX_MSG_MAP(CMatrixGridView)
ON_COMMAND(ID_ZOOM_25, OnZoom25)
ON_COMMAND(ID_ZOOM_50, OnZoom50)
ON_COMMAND(ID_ZOOM_200, OnZoom200)
ON_COMMAND(ID_ZOOM_100, OnZoom100)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView construction/destruction

CMatrixGridView::CMatrixGridView()
{
// TODO: add construction code here
m_iColIndex=1;
m_iRowIndex=1;
}

CMatrixGridView::~CMatrixGridView()
{
}

BOOL CMatrixGridView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs

return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView drawing

void CMatrixGridView::OnDraw(CDC* pDC)
{
CMatrixGridDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
InitializeCell();
FillColor(pDC);
SetScrollPosition();
DrawGrid(pDC);
}


void CMatrixGridView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();

CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView printing

BOOL CMatrixGridView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CMatrixGridView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CMatrixGridView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView diagnostics

#ifdef _DEBUG
void CMatrixGridView::AssertValid() const
{
CScrollView::AssertValid();
}

void CMatrixGridView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}

CMatrixGridDoc* CMatrixGridView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatrixGridDoc)));
return (CMatrixGridDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView message handlers

void CMatrixGridView::DrawGrid(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
    int iIndex;
for(iIndex=0;iIndex<=pDoc->m_iColoums;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset);
pDC->LineTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset+(pDoc->m_iHeight*pDoc->m_iRow));
}
for(iIndex=0;iIndex<=pDoc->m_iRow;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset,pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
pDC->LineTo(pDoc->m_iXoffset+(pDoc->m_iWidth*pDoc->m_iColoums),pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
}
}

void CMatrixGridView::OnZoom25() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.25;
pDoc->m_iWidth=WIDTH*.25;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnZoom50() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.50;
pDoc->m_iWidth=WIDTH*.50;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnZoom200() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*2;
pDoc->m_iWidth=WIDTH*2;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnZoom100() 
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*1;
pDoc->m_iWidth=WIDTH*1;
SetScrollPosition();
Invalidate();
}

void CMatrixGridView::OnEditUndo() 
{
AfxMessageBox("Add code for Undo Operation here");
}

void CMatrixGridView::OnEditPaste() 
{
AfxMessageBox("Add code for Paste Operation here");
}

void CMatrixGridView::OnEditCut() 
{
AfxMessageBox("Add code for Cut Operation here");
}

void CMatrixGridView::OnEditCopy() 
{
AfxMessageBox("Add code for Copy Operation here");
}


/*-------------------------------------------- Function Header ---------------------------------------------*
Function Name  : CMatrixGridView::SetScrollPosition
Description      : Set the scroll sizes here depending on the zoom Factor
Return Type      : void 
Warning           : 
------------------------------------------------------------------------------------------------------------*/

void CMatrixGridView::SetScrollPosition()
{
CMatrixGridDoc *pDoc=GetDocument();
int iHorizontalPosition=pDoc->m_iXoffset+pDoc->m_iColoums*pDoc->m_iWidth+100;
int iVerticalPosition=pDoc->m_iYoffset+pDoc->m_iRow*pDoc->m_iHeight+100;
SetScrollSizes(MM_TEXT,CSize(iHorizontalPosition,iVerticalPosition));
}

void CMatrixGridView::InitializeCell()
{
CMatrixGridDoc *pDoc=GetDocument();
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.SetRect(0,0,pDoc->m_iWidth,pDoc->m_iHeight);
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.OffsetRect(pDoc->m_iXoffset+(pDoc->m_iWidth*iColIndex),pDoc->m_iYoffset+(pDoc->m_iHeight*iRowIndex));
        }
}
}

void CMatrixGridView::FillColor(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);

CBrush WhiteBrush(RGB(255,255,255));
CBrush RedBrush(RGB(255,0,0));
CBrush BlueBrush(RGB(0,0,255));
CBrush GreenBrush(RGB(0,255,0));
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{   
switch(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor)
{
case WHITE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&WhiteBrush);
break;
}
case RED:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&RedBrush);
break;
}
case GREEN:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&GreenBrush);
break;
}
case BLUE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&BlueBrush);
break;
}
case CUSTOM:
{  
CBrush CustomBrush(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_cColor);
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&CustomBrush);
break;
}
}
pDC->DrawText(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_strText,pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,DT_LEFT);
}
}

void CMatrixGridView::OnLButtonDown(UINT nFlags, CPoint point) 
{
CMatrixGridDoc *pDoc=GetDocument();
CPoint point1;

point1=GetScrollPosition();
point+=point1;
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{   
   if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.PtInRect(point))
{
m_iColIndex=iColIndex;
m_iRowIndex=iRowIndex;

if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor>BLUE )
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor=WHITE;
}
else
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor++;
}
}
}
}
Invalidate();

CScrollView::OnLButtonDown(nFlags, point);
}




Download Source Code

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.



Number Of articles Posted      : 60
Number Of Messages Posted  : 3
Joined Date : 2012/11/14
   
Author
Messages
Posted:
Post Your Comments  
Name (Max 50 Chars)
Comments
 
 

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android