新手问:onpaint()没有执行是怎么回事?

csz327 2004-10-23 02:57:10
各位大侠:onpaint()没有执行是怎么回事啊?急!我是一个新手,谢谢!给分
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
csz327 2004-10-25
  • 打赏
  • 举报
回复
我想在界面上划一个黑色正方型和一个圆,圆可以用鼠标拖动,我是一个新手,等ing!
csz327 2004-10-25
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "Ex10b.h"

#include "Ex10bDoc.h"
#include "Ex10bView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEx10bView

IMPLEMENT_DYNCREATE(CEx10bView, CScrollView)

BEGIN_MESSAGE_MAP(CEx10bView, CScrollView)
//{{AFX_MSG_MAP(CEx10bView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
//}}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()

/////////////////////////////////////////////////////////////////////////////
// CEx10bView construction/destruction

CEx10bView::CEx10bView():m_sizeEllipse(100,-100),
m_pointTopLeft(10,-10),
m_sizeOffset(0,0)
{
// TODO: add construction code here
m_bCaptured=FALSE;
m_pdcMemory=new CDC;
m_pBitmap=new CBitmap;

}

CEx10bView::~CEx10bView()
{
delete m_pBitmap;
delete m_pdcMemory;
}

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

return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEx10bView drawing

void CEx10bView::OnDraw(CDC* pDC)
{
//CEX05CDoc* pDoc = GetDocument();
//ASSERT_VALID(pDoc);
pDC->TextOut(0,0,"Press the left mouse button here.");
CBrush brushHatch(HS_DIAGCROSS,RGB(255,0,0));
CPoint point(0,0);
pDC->LPtoDP(&point);
pDC->SetBrushOrg(point);
pDC->SelectObject(&brushHatch);
pDC->Ellipse(CRect(m_pointTopLeft,m_sizeEllipse));
pDC->SelectStockObject(BLACK_BRUSH);
pDC->Rectangle(CRect(100,-100,200,-200));
// TODO: add draw code for native data here
}

void CEx10bView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal(800,1050);
CSize sizePage(sizeTotal.cx/2,sizeTotal.cy/2);
CSize sizeLine(sizeTotal.cx/50,sizeTotal.cy/50);
SetScrollSizes(MM_LOENGLISH,sizeTotal,sizePage,sizeLine);
if(m_pdcMemory->GetSafeHdc()==NULL)
{
CClientDC dc(this);
OnPrepareDC(&dc);
CRect rectMax(0,0,sizeTotal.cx,-sizeTotal.cy);
dc.LPtoDP(rectMax);
m_pdcMemory->CreateCompatibleDC(&dc);
m_pBitmap->CreateCompatibleBitmap(&dc,rectMax.right,rectMax.bottom);
m_pdcMemory->SetMapMode(MM_LOENGLISH);
}

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

/////////////////////////////////////////////////////////////////////////////
// CEx10bView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx10bView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx10bView message handlers


void CEx10bView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rectEllipse(m_pointTopLeft,m_sizeEllipse);
CRgn circle;
CClientDC dc(this);
OnPrepareDC(&dc);
dc.LPtoDP(rectEllipse);
circle.CreateEllipticRgnIndirect(rectEllipse);
if(circle.PtInRegion(point))
{
SetCapture();
m_bCaptured=TRUE;
CPoint pointTopLeft(m_pointTopLeft);
dc.LPtoDP(&pointTopLeft);
m_sizeOffset=point-pointTopLeft;
::SetCursor(::LoadCursor(NULL,IDC_CROSS));
}
//CScrollView::OnLButtonDown(nFlags, point);
}

void CEx10bView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured)
{
CClientDC dc(this);
OnPrepareDC(&dc);
CRect rectOld(m_pointTopLeft,m_sizeEllipse);
dc.LPtoDP(rectOld);
InvalidateRect(rectOld,TRUE);
m_pointTopLeft=point-m_sizeOffset;
dc.DPtoLP(&m_pointTopLeft);
CRect rectNew(m_pointTopLeft,m_sizeEllipse);
dc.LPtoDP(rectNew);
InvalidateRect(rectOld,FALSE);
InvalidateRect(rectNew,FALSE);

}
//CScrollView::OnMouseMove(nFlags, point);
}

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


void CEx10bView::OnPaint()
{
CPaintDC dc(this); // device context for painting
OnPrepareDC(&dc);
CRect rectUpdate;
dc.GetClipBox(&rectUpdate);
//dc.TextOut(0,0,"aaaaaaaaaaaaaaaaaaa");
CBitmap* pOldBitmap=m_pdcMemory->SelectObject(m_pBitmap);
m_pdcMemory->IntersectClipRect(&rectUpdate);
CBrush backgroundBrush((COLORREF)::GetSysColor(COLOR_WINDOW));
CBrush* pOldBrush=m_pdcMemory->SelectObject(&backgroundBrush);
m_pdcMemory->PatBlt(rectUpdate.left,rectUpdate.top,rectUpdate.Width(),rectUpdate.Height(),PATCOPY);
OnDraw(m_pdcMemory);
dc.BitBlt(rectUpdate.left,rectUpdate.top,rectUpdate.Width(),rectUpdate.Height(),m_pdcMemory,rectUpdate.left,rectUpdate.top,PATCOPY);
m_pdcMemory->SelectObject(pOldBitmap);
m_pdcMemory->SelectObject(pOldBrush);
// TODO: Add your message handler code here

// Do not call CScrollView::OnPaint() for painting messages
}
sinall 2004-10-23
  • 打赏
  • 举报
回复
大概是需要invalidata一下吧
JJZHK 2004-10-23
  • 打赏
  • 举报
回复
把你的程序思路说一下,或者把主要代码贴出来吧,这么说也太含糊了!
C_M_ 2004-10-23
  • 打赏
  • 举报
回复
说的太含糊了吧?

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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