0% found this document useful (0 votes)
73 views3 pages

Counter

This document contains code for a CounterView class that displays and updates a count value. It includes message map entries for left and right mouse button clicks that call methods to increment or decrement the count stored in the associated CounterDoc class. The OnDraw method retrieves the count from the document and displays it centered in the view.

Uploaded by

sonibiren
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views3 pages

Counter

This document contains code for a CounterView class that displays and updates a count value. It includes message map entries for left and right mouse button clicks that call methods to increment or decrement the count stored in the associated CounterDoc class. The OnDraw method retrieves the count from the document and displays it centered in the view.

Uploaded by

sonibiren
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CounterView.

cpp

BEGIN_MESSAGE_MAP(CCounterView, CView)
//{{AFX_MSG_MAP(CCounterView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CView::OnFilePrintPreview)
END_MESSAGE_MAP()

void CCounterView::OnDraw(CDC* pDC)


{
CCounterDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect r;
GetClientRect(&r);

CString str;
str.Format("%d",pDoc->count);
pDC->DrawText(str,-1,&r,DT_CENTER|DT_VCENTER);
}

CCounterView message handlers

void CCounterView::OnLButtonDown(UINT nFlags, CPoint point)


{
// TODO: Add your message handler code here and/or call default
CCounterDoc *pdoc=GetDocument();
pdoc->count++;
Invalidate();

CView::OnLButtonDown(nFlags, point);
}

void CCounterView::OnRButtonDown(UINT nFlags, CPoint point)


{
// TODO: Add your message handler code here and/or call default
CCounterDoc *pdoc=GetDocument();
pdoc->count--;
Invalidate();
CView::OnRButtonDown(nFlags, point);
}
CounterDoc.h

class CCounterDoc : public CDocument


{
protected: // create from serialization only
CCounterDoc();
DECLARE_DYNCREATE(CCounterDoc)

// Attributes
public:

// Operations
int count;
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCounterDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CCounterDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions


protected:
//{{AFX_MSG(CCounterDoc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CounterView.h

class CCounterView : public CView


{
protected: // create from serialization only
CCounterView();
DECLARE_DYNCREATE(CCounterView)

// Attributes
public:
CCounterDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCounterView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CCounterView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions


protected:
//{{AFX_MSG(CCounterView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

You might also like