求助关于VC++图形学,窗口重绘怎么解决

weixin_44004318 2019-06-13 06:35:13
// 形状View.cpp : implementation of the CMyView class // #include "stdafx.h" #include "形状.h" #include "形状Doc.h" #include "形状View.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMyView IMPLEMENT_DYNCREATE(CMyView, CView) BEGIN_MESSAGE_MAP(CMyView, CView) //{{AFX_MSG_MAP(CMyView) ON_COMMAND(ID_CIRCLE, OnCircle) ON_COMMAND(ID_HAND, OnHand) ON_COMMAND(ID_RECT, OnRect) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() 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() ///////////////////////////////////////////////////////////////////////////// // CMyView construction/destruction CMyView::CMyView() { // TODO: add construction code here m_hand=m_circle=m_rect=false; } CMyView::~CMyView() { } BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMyView drawing void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnCircle() { // TODO: Add your command handler code here m_circle=true; m_rect=m_hand=false; } void CMyView::OnHand() { // TODO: Add your command handler code here m_hand=true; m_circle=m_rect=false; } void CMyView::OnRect() { // TODO: Add your command handler code here m_rect=true; m_circle=m_hand=false; } void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(m_hand) { spt=point; } if(m_circle||m_rect) { SetCapture(); spt=point; ept=point; } CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default ReleaseCapture(); CView::OnLButtonUp(nFlags, point); } void CMyView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDC *pDC=GetDC(); if(m_hand&&(nFlags&&MK_LBUTTON)) { pDC->MoveTo(spt); pDC->LineTo(point); spt=point; } if(m_rect&&(nFlags&&MK_LBUTTON)) { CGdiObject *old=pDC->SelectStockObject(NULL_BRUSH); int drawmode=pDC->GetROP2();//保存原有的绘图模式 pDC->SetROP2(R2_NOTCOPYPEN);//设置新的绘图模式 pDC->Rectangle(ept.x,ept.y,spt.x,spt.y); pDC->SetROP2(drawmode); pDC->Rectangle(spt.x,spt.y,point.x,point.y); pDC->SelectObject(old); ept=point; } if(m_circle&&(nFlags&&MK_LBUTTON)) { CGdiObject *old=pDC->SelectStockObject(NULL_BRUSH); int drawmode=pDC->GetROP2();//保存原有的绘图模式 pDC->SetROP2(R2_NOTCOPYPEN);//设置新的绘图模式 pDC->Ellipse(ept.x,ept.y,spt.x,spt.y); pDC->SetROP2(drawmode); pDC->Ellipse(spt.x,spt.y,point.x,point.y); pDC->SelectObject(old); ept=point; } CView::OnMouseMove(nFlags, point); }
...全文
145 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
baita96 2019-06-16
  • 打赏
  • 举报
回复
楼上正解,不在WM_PAINT(OnDraw)里面绘制的东西,只要一刷新就没了。
schlafenhamster 2019-06-14
  • 打赏
  • 举报
回复
你只是 在 OnMouseMove 里绘制, 应该 放到 Onpaint 中 绘制, 其他地方 只是 Invalidate 来请求重绘

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧