东软实训之项目实现鼠标左键加,右键减的功能
// SXunView.cpp : implementation of the CSXunView class
//
#include "stdafx.h"
#include "SXun.h"
#include "SXunDoc.h"
#include "SXunView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CSXunView
IMPLEMENT_DYNCREATE(CSXunView, CView)
BEGIN_MESSAGE_MAP(CSXunView, CView)
//{{AFX_MSG_MAP(CSXunView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
//}}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()
/
// CSXunView construction/destruction
CSXunView::CSXunView()
{
// TODO: add construction code here
//CStdioFile file("data.txt", CFile);
num = 0;//对num赋初值0
}
CSXunView::~CSXunView()
{
}
BOOL CSXunView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CSXunView drawing
void CSXunView::OnDraw(CDC* pDC)
{
CSXunDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect rect;
this->GetWindowRect(&rect);
int x,y;
x=rect.Width()/2;//x坐标的位置
y=rect.Height()/2;//y坐标的位置
str.Format("%d",num);//格式转换
pDC->TextOut(x,y,str);//在视图上显示输出
CFile mFile(_T("data.txt"),CFile::modeWrite | CFile::modeCreate);
mFile.Write(str,2);
mFile.Flush();//刷新
mFile.Close();//关闭文件
// TODO: add draw code for native data here
}
/
// CSXunView printing
BOOL CSXunView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSXunView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSXunView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CSXunView diagnostics
#ifdef _DEBUG
void CSXunView::AssertValid() const
{
CView::AssertValid();
}
void CSXunView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSXunDoc* CSXunView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSXunDoc)));
return (CSXunDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CSXunView message handlers
void CSXunView::OnLButtonDown(UINT nFlags, CPoint point)
{
num+=1; //左键+
this->Invalidate();//刷新
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}
void CSXunView::OnRButtonDown(UINT nFlags, CPoint point)
{
num-=1;//右键-
this->Invalidate();//刷新
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
}
void CSXunView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
// SXunView.h : interface of the CSXunView class
//
/
#if !defined(AFX_SXUNVIEW_H__AC48FE93_3FD1_4E43_A682_CDC5E201178B__INCLUDED_)
#define AFX_SXUNVIEW_H__AC48FE93_3FD1_4E43_A682_CDC5E201178B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSXunView : public CView
{
protected: // create from serialization only
CSXunView();
DECLARE_DYNCREATE(CSXunView)
// Attributes
public:
CSXunDoc* GetDocument();
// Operations
public:
CString str;
int num;//定义成员变量
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSXunView)
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 ~CSXunView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CSXunView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in SXunView.cpp
inline CSXunDoc* CSXunView::GetDocument()
{ return (CSXunDoc*)m_pDocument; }
#endif
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SXUNVIEW_H__AC48FE93_3FD1_4E43_A682_CDC5E201178B__INCLUDED_)