小弟用vc编了一个绘图程序,但是有一个问题(是关于如何擦除图形的)

shanweibing shanweibing 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这个程序长方形和椭圆.在菜单中设置所绘图形的种类,然后用鼠标拖动画图.在菜单中有一个橡皮命令.它一次擦除一个已有图形.我是这样实现的:首先将计数器减1,再调用Invalidate(int)来重画视图,我不知道Invalidate函数的参数是干什么用的,我先用计数器作实参,发现不行.用橡皮命令有时不擦除图形,有时一次擦除两个图形.我又用计数器的值加1作实参就没问题了.请大侠为我解释一下Invalidate函数参数的意义.多谢了.
(MyDrawView.cpp摘录如下,前面都没问题,有问题的地方在这个文件的倒数第八行左右)
#include "stdafx.h"
#include "MyDraw1.h"

#include "MyDraw1Doc.h"
#include "MyDraw1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDraw1View

IMPLEMENT_DYNCREATE(CMyDraw1View, CView)

BEGIN_MESSAGE_MAP(CMyDraw1View, CView)
//{{AFX_MSG_MAP(CMyDraw1View)
ON_COMMAND(ID_TOOL_RECTANGLE, OnToolRectangle)
ON_COMMAND(ID_TOOL_ELLIPSE, OnToolEllipse)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_UPDATE_COMMAND_UI(ID_TOOL_RECTANGLE, OnUpdateToolRectangle)
ON_UPDATE_COMMAND_UI(ID_TOOL_ELLIPSE, OnUpdateToolEllipse)
ON_COMMAND(ID_VISUAL_CAN, OnVisualCan)
ON_COMMAND(ID_VISUAL_CNANOT, OnVisualCnanot)
ON_UPDATE_COMMAND_UI(ID_VISUAL_CAN, OnUpdateVisualCan)
ON_UPDATE_COMMAND_UI(ID_VISUAL_CNANOT, OnUpdateVisualCnanot)
ON_COMMAND(ID_TOOL_ERASE, OnToolErase)
ON_COMMAND(ID_TOOL_ERASE_ALL, OnToolEraseAll)

//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDraw1View construction/destruction

CMyDraw1View::CMyDraw1View()
{
// TODO: add construction code here
m_typeNext = shpRectangle;
m_bCaptured = false;
m_pBrushOld = NULL;
m_countShapes = 0;
m_bVisual = false;


}

CMyDraw1View::~CMyDraw1View()
{
}

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

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyDraw1View drawing

void CMyDraw1View::OnDraw(CDC* pDC)
{
CMyDraw1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
for(int nShp = 0; nShp < m_countShapes; nShp++)
{
ASSERT(m_arShps[nShp].m_typeShape == shpRectangle ||
m_arShps[nShp].m_typeShape == shpEllipse);
SetPenBrush(pDC);
switch(m_arShps[nShp].m_typeShape)
{
case shpRectangle:
pDC->Rectangle(m_arShps[nShp].m_boxShape);
break;
case shpEllipse:
pDC->Ellipse(m_arShps[nShp].m_boxShape);
break;
}

}

}

/////////////////////////////////////////////////////////////////////////////
// CMyDraw1View diagnostics

#ifdef _DEBUG
void CMyDraw1View::AssertValid() const
{
CView::AssertValid();
}

void CMyDraw1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CMyDraw1View message handlers

void CMyDraw1View::OnToolRectangle()
{
// TODO: Add your command handler code here
m_typeNext = shpRectangle;

}

void CMyDraw1View::OnToolEllipse()
{
// TODO: Add your command handler code here
m_typeNext = shpEllipse;

}
void CMyDraw1View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
m_bCaptured = true;

ASSERT(m_typeNext == shpRectangle || m_typeNext ==
shpEllipse);
m_shpTemp.m_typeShape = m_typeNext;
m_shpTemp.m_boxShape.left = m_shpTemp.m_boxShape.right = point.x;
m_shpTemp.m_boxShape.top = m_shpTemp.m_boxShape.bottom = point.y;


CView::OnLButtonDown(nFlags, point);
}

void CMyDraw1View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured)
{
CClientDC dc(this);
m_shpTemp.m_boxShape.bottom = point.y;
m_shpTemp.m_boxShape.right = point.x;
}

CView::OnMouseMove(nFlags, point);
}

void CMyDraw1View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured)
{
::ReleaseCapture();
m_bCaptured = false;

CClientDC dc(this);

m_shpTemp.m_boxShape.right = point.x;
m_shpTemp.m_boxShape.bottom = point.y;

m_arShps[m_countShapes] = m_shpTemp;
m_countShapes++;

Draw(&dc,m_shpTemp);
}

CView::OnLButtonUp(nFlags, point);
}



void CMyDraw1View::SetPenBrush(CDC *pDC )
{
if(m_bVisual)

m_pBrushOld = (CBrush* )pDC->SelectStockObject(NULL_BRUSH);
else
m_pBrushOld = (CBrush* )pDC->SelectStockObject(WHITE_BRUSH);
}






void CMyDraw1View::OnUpdateToolRectangle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_typeNext == shpRectangle);

}

void CMyDraw1View::OnUpdateToolEllipse(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_typeNext == shpEllipse);
}





void CMyDraw1View::Draw(CDC *pDC, Shape &s)
{
ASSERT(pDC != NULL);
SetPenBrush(pDC);
switch(s.m_typeShape)
{
case shpRectangle: pDC->Rectangle(s.m_boxShape);
break;
case shpEllipse: pDC->Ellipse(s.m_boxShape);
break;


}

}


void CMyDraw1View::OnVisualCan()
{
// TODO: Add your command handler code here
m_bVisual = true;


}

void CMyDraw1View::OnVisualCnanot()
{
// TODO: Add your command handler code here
m_bVisual = false;


}

void CMyDraw1View::OnUpdateVisualCan(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bVisual == true);

}

void CMyDraw1View::OnUpdateVisualCnanot(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bVisual == false);
}

void CMyDraw1View::OnToolErase()
{
// TODO: Add your command handler code here
m_countShapes--;

Invalidate(m_countShapes+1); /*问题在这儿,Invalidate函数的参数 是什么意思 */

}

void CMyDraw1View::OnToolEraseAll()
{
// TODO: Add your command handler code here
m_countShapes = 0;

Invalidate(m_countShapes+1);
}



...全文

相关推荐

自己写的画板程序 vc 源码

【描述】:“用VC写的画板。附源码。功能一般。是小弟的第一个windows程序设计的成果。”描述中提到,这个画板程序的功能可能比较基础,没有复杂的特性。

hsy36000

vc6.0 控制摄像头源代码,采用directxshow 效率很高,小弟参考了很多的资料才搞明白,和大家共享,希望对大家有帮助

首先,我们需要了解Visual C++ 6.0(简称VC6.0)是一个经典的集成开发环境,主要用于编写Windows平台上的C++程序。

like120049214

C语言(VC)计算器

标题 "C语言(VC)计算器" 指的是一个基于C语言编程的计算器程序,它在Windows环境下运行,采用Visual C++(简称VC)作为开发工具。

Glock18C

CodeForces 785A Anton and Polyhedrons【水题】

题目链接:http://codeforces.com/contest/785/problem/A 题意:给你n个立体图形,问你所有立体图形加起来有多少面 解析:水题,map处理直接做#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> #include <vec

15zhazhahe

UE5游戏逆向实战:用FModel提取.pak文件中的3D模型(附Dumper-7避坑指南)

本文详细介绍了在UE5游戏逆向工程中使用FModel提取.pak文件中3D模型的实战技巧,包括Dumper-7工具的深度配置与避坑指南。针对UE5特有的unversioned属性系统,提供了从源码编译到智能注入的全链路解决方案,帮助开发者高效获取游戏资产并优化处理流程。

weixin_30374009
评论 3
收藏 已收藏
分享
返回顶部
shanweibing
VC/MFC 16,550 成员 421,606 内容