主题:MFC中的随机函数
为什么每次点的都只是在那附近变呢?
以下是我的view类中的代码
// 123View.cpp : implementation of the CMy123View class
//
#include "stdafx.h"
#include "123.h"
#include "123Doc.h"
#include "123View.h"
#include "time.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int a,b;
/////////////////////////////////////////////////////////////////////////////
// CMy123View
IMPLEMENT_DYNCREATE(CMy123View, CView)
BEGIN_MESSAGE_MAP(CMy123View, CView)
//{{AFX_MSG_MAP(CMy123View)
ON_WM_LBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMy123View construction/destruction
CMy123View::CMy123View()
{
// TODO: add construction code here
}
CMy123View::~CMy123View()
{
}
BOOL CMy123View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy123View drawing
void CMy123View::OnDraw(CDC* pDC)
{
CMy123Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
srand(unsigned(time(NULL)));
a=rand()%500+50;
b=rand()%500+50;
pDC->Rectangle(a,b,a+50,b+50);
}
/////////////////////////////////////////////////////////////////////////////
// CMy123View printing
BOOL CMy123View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy123View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy123View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy123View diagnostics
#ifdef _DEBUG
void CMy123View::AssertValid() const
{
CView::AssertValid();
}
void CMy123View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy123Doc* CMy123View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy123Doc)));
return (CMy123Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy123View message handlers
void CMy123View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(point.x>a&&point.x<a+50&&point.y>b&&point.y<b+50)
{
InvalidateRect(NULL,FALSE);
RedrawWindow();
}
else
MessageBeep(0);
CView::OnLButtonDown(nFlags, point);
}
以下是我的view类中的代码
// 123View.cpp : implementation of the CMy123View class
//
#include "stdafx.h"
#include "123.h"
#include "123Doc.h"
#include "123View.h"
#include "time.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int a,b;
/////////////////////////////////////////////////////////////////////////////
// CMy123View
IMPLEMENT_DYNCREATE(CMy123View, CView)
BEGIN_MESSAGE_MAP(CMy123View, CView)
//{{AFX_MSG_MAP(CMy123View)
ON_WM_LBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMy123View construction/destruction
CMy123View::CMy123View()
{
// TODO: add construction code here
}
CMy123View::~CMy123View()
{
}
BOOL CMy123View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy123View drawing
void CMy123View::OnDraw(CDC* pDC)
{
CMy123Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
srand(unsigned(time(NULL)));
a=rand()%500+50;
b=rand()%500+50;
pDC->Rectangle(a,b,a+50,b+50);
}
/////////////////////////////////////////////////////////////////////////////
// CMy123View printing
BOOL CMy123View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy123View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy123View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy123View diagnostics
#ifdef _DEBUG
void CMy123View::AssertValid() const
{
CView::AssertValid();
}
void CMy123View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy123Doc* CMy123View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy123Doc)));
return (CMy123Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy123View message handlers
void CMy123View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(point.x>a&&point.x<a+50&&point.y>b&&point.y<b+50)
{
InvalidateRect(NULL,FALSE);
RedrawWindow();
}
else
MessageBeep(0);
CView::OnLButtonDown(nFlags, point);
}