MFC制作openGL动画时程序一直在调用OnPaint(),怪不得我SetTimer()后OnTimer()回调函数一直不执行,原来是OnPaint()一直不断地被调用,占据着事件队列,
为什么OnPaint()会不断地被调用啊?
这里附CXXXXView的.h和.cpp函数的代码
下面是 HEIHEIHEIView.h 文件代码
// HEIHEIHEIView.h : CHEIHEIHEIView 类的接口 // #pragma once class CHEIHEIHEIView : public CView { protected: // 仅从序列化创建 CHEIHEIHEIView(); DECLARE_DYNCREATE(CHEIHEIHEIView) // 属性 public: CHEIHEIHEIDoc* GetDocument() const; private: CPoint firstPoint; BOOL lButtonDown; //自己添加的属性 HGLRC m_hrc; //RC句柄 CClientDC* m_pDC; //DC句柄 CPoint points[100]; int count; // 操作 public: void DrawPic(void); static UINT Fun(LPVOID lpParamter); // 重写 public: virtual void OnDraw(CDC* pDC); // 重写以绘制该视图 virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: // 实现 public: virtual ~CHEIHEIHEIView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // 生成的消息映射函数 protected: DECLARE_MESSAGE_MAP() public: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msg void OnPaint(); afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnSize(UINT nType, int cx, int cy); }; #ifndef _DEBUG // HEIHEIHEIView.cpp 中的调试版本 inline CHEIHEIHEIDoc* CHEIHEIHEIView::GetDocument() const { return reinterpret_cast<CHEIHEIHEIDoc*>(m_pDocument); } #endif
下面是 HEIHEIHEIView.cpp 文件代码
// HEIHEIHEIView.cpp : CHEIHEIHEIView 类的实现 // #include "stdafx.h" #include "HEIHEIHEI.h" #include "HEIHEIHEIDoc.h" #include "HEIHEIHEIView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CHEIHEIHEIView IMPLEMENT_DYNCREATE(CHEIHEIHEIView, CView) BEGIN_MESSAGE_MAP(CHEIHEIHEIView, CView) ON_WM_CREATE() ON_WM_DESTROY() ON_WM_PAINT() ON_WM_TIMER() ON_WM_SIZE() END_MESSAGE_MAP() // CHEIHEIHEIView 构造/析构 CHEIHEIHEIView::CHEIHEIHEIView() { // TODO: 在此处添加构造代码 count = 0; for(int i=0; i<100; i++){ points[i].x = i; points[i].y = 100; } } CHEIHEIHEIView::~CHEIHEIHEIView() { } BOOL CHEIHEIHEIView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此处通过修改 // CREATESTRUCT cs 来修改窗口类或样式 cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; // 不要画到兄弟窗口或字窗口 return CView::PreCreateWindow(cs); } // CHEIHEIHEIView 绘制 void CHEIHEIHEIView::OnDraw(CDC* /*pDC*/) { CHEIHEIHEIDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 } // CHEIHEIHEIView 诊断 #ifdef _DEBUG void CHEIHEIHEIView::AssertValid() const { CView::AssertValid(); } void CHEIHEIHEIView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CHEIHEIHEIDoc* CHEIHEIHEIView::GetDocument() const // 非调试版本是内联的 { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHEIHEIHEIDoc))); return (CHEIHEIHEIDoc*)m_pDocument; } #endif //_DEBUG // CHEIHEIHEIView 消息处理程序 int CHEIHEIHEIView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: 在此添加您专用的创建代码 //设置RC像素格式 int pixelformat; m_pDC = new CClientDC(this); //在客户区作图,获得设备描述符DC; ASSERT(m_pDC != NULL); static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, 16, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; if((pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd )) == 0 ){ MessageBox(L"在该DC上找不到于PFD接近的位图结构"); return FALSE; } int nRet = SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); if ( nRet == FALSE)//测试硬件是否支持该pdf { MessageBox(L"无法在该DC上设置位图结构"); DWORD err = GetLastError(); //return FALSE; } m_hrc = wglCreateContext(m_pDC->GetSafeHdc()); wglMakeCurrent(m_pDC->GetSafeHdc(),m_hrc); //SetTimer(1,10000,NULL); return 0; } void CHEIHEIHEIView::OnDestroy() { CView::OnDestroy(); // TODO: 在此处添加消息处理程序代码 if(wglGetCurrentContext()!=NULL) //释放RC以及DC ::wglMakeCurrent(NULL, NULL); if (m_hrc) ::wglDeleteContext(m_hrc); if (m_pDC) delete m_pDC; } void CHEIHEIHEIView::OnPaint() { //CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CView::OnPaint(); DrawPic(); //Sleep(40); } void CHEIHEIHEIView::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 MessageBox(L"hello"); CView::OnTimer(nIDEvent); } void CHEIHEIHEIView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 glViewport(0, 0, cx, cy); } void CHEIHEIHEIView::DrawPic(){ count += 1; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINE_STRIP); glColor3f(1,0,0); if(count<=100){ for(int i=0; i<count; i++){ glVertex2f(points[i].x, points[i].y); } }else{ count = 0; } glEnd(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,100,99,101,-1,1); glFlush(); }
其余文件的代码都是CLASSWizard自动生成的,应该不会出问题