#include "stdafx.h"
#include "item.h"
#include "itemDoc.h"
#include "itemView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int  g_nTimer = 0;//全局变量
IMPLEMENT_DYNCREATE(CItemView, CView)
BEGIN_MESSAGE_MAP(CItemView, CView)
//{{AFX_MSG_MAP(CItemView)
ON_WM_TIMER()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CItemView construction/destructionCItemView::CItemView()
{
// TODO: add construction code here}CItemView::~CItemView()
{
}BOOL CItemView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
}void CItemView::OnDraw(CDC* pDC)
{
CItemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here }#ifdef _DEBUG
void CItemView::AssertValid() const
{
CView::AssertValid();
}void CItemView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}CItemDoc* CItemView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CItemDoc)));
return (CItemDoc*)m_pDocument;
}
#endif //_DEBUGvoid CItemView::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
  
g_nTimer++;
if (g_nTimer == 10) g_nTimer=0;
DrawPicture();
    UpdateData();
CView::OnTimer(nIDEvent);
}void CItemView::OnInitialUpdate() 
{
CView::OnInitialUpdate();

//     SetTimer(1,60000,NULL);
SetTimer(1,6000,0);}void CItemView::OnPaint() 
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
//    DrawPicture();
// Do not call CView::OnPaint() for painting messages
}void CItemView::DrawPicture()
{
CDC *pDC = GetDC(); CItemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int xStep,yStep;
int i,k;
CString str;
CRect Rect(100,200,400,440);
xStep=(Rect.right-Rect.left-10)/5;
yStep=(Rect.bottom-Rect.top-10)/10;
    CPen Pen(PS_SOLID,2,RGB(255,0,0));
CPen* oldpen=pDC->SelectObject(&Pen); 
CClientDC dc(this);
CBrush Brush(RGB(0,0,0));
CBrush *poldbrush=dc.SelectObject(&Brush);
dc.FillRect(Rect,&Brush);
dc.SelectObject(poldbrush);
pDC->MoveTo(Rect.left,Rect.bottom);
pDC->LineTo(Rect.left,Rect.top);
    pDC->MoveTo(Rect.left,Rect.bottom);
pDC->LineTo(Rect.right,Rect.bottom);
pDC->MoveTo(Rect.left,Rect.top);
pDC->LineTo(Rect.left-6,Rect.top+10);
    pDC->MoveTo(Rect.left,Rect.top);
    pDC->LineTo(Rect.left+6,Rect.top+10); 
pDC->MoveTo(Rect.right,Rect.bottom);
pDC->LineTo(Rect.right-10,Rect.bottom-6);
    pDC->MoveTo(Rect.right,Rect.bottom);
pDC->LineTo(Rect.right-10,Rect.bottom+6);
CPen Pen1(PS_SOLID,1,RGB(128,128,128));
pDC->SelectObject(&Pen1);
    for(i=1;i<=5;i++)
{
     pDC->MoveTo(Rect.left+xStep*i,Rect.bottom);
 pDC->LineTo(Rect.left+xStep*i,Rect.top+10);
     str.Format("%d",i);
     pDC->TextOut(Rect.left+xStep*i,Rect.bottom+10,str);
}
for(k=1;k<=10;k++)
{
pDC->MoveTo(Rect.left,Rect.bottom-k*yStep);
pDC->LineTo(Rect.right-10,Rect.bottom-k*yStep);
     str.Format("%d",k);
     pDC->TextOut(Rect.left-20,Rect.bottom-k*yStep,str);
}
    pDC->SelectObject(oldpen); 
   str.Format("Distance=%d",g_nTimer);
pDC->TextOut(450,300,str); 
UpdateData();
}
怎么只有等待6秒钟才输出图形,我想开始就输出图形,然后每隔6秒钟Distance=变化一下。

解决方案 »

  1.   

    void CItemView::OnDraw(CDC* pDC)
    {
    CItemDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    DrawPicture();          //这是也加上才行。 }
      

  2.   

    在OnDraw()中drawpicture90;void CItemView::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    g_nTimer++;
    if (g_nTimer == 10) g_nTimer=0;
    Invalidate();//加上这句
    CView::OnTimer(nIDEvent);
    }void CItemView::OnInitialUpdate() 
    {
    CView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    SetTimer(1,6000,NULL);
    }
      

  3.   

    在ONSETFOCUS里加上
    DrawPicture();