Hi,I have wrote a MFC project and I have compile and debuged the project.But the output result is not right. I am a freshman on computer programming.Please help me solve this problem.Thank you!
This is the part of the "practiceView.cpp".
void CpracticeView::OnDraw(CDC* )
{
CpracticeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
DrawScence();
}
int CpracticeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_pDC = new CClientDC(this);
if( m_pDC == NULL )
{
::AfxMessageBox( "fail to get device context" );
return FALSE;
}
if( !SetupPixelFormat() )
{
::AfxMessageBox( "SetupPixelFormat failed" );
return FALSE;
}
if( ( m_hRC = wglCreateContext(m_pDC->GetSafeHdc()) ) == 0 )
{
::AfxMessageBox( "wglCreateContext failed" );
return FALSE;
}
if( wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) == FALSE )
{
::AfxMessageBox( "wglMakeCurrent failed" );
return FALSE;
}
return 0;
}
void CpracticeView::OnDestroy()
{
CView::OnDestroy();
if(wglMakeCurrent(0,0)==FALSE)
::AfxMessageBox("wglMakeCurrent failed");
if(m_hRC && (wglDeleteContext(m_hRC)==FALSE))
::AfxMessageBox("wglDeleteContext failed");
if( m_pDC)
delete m_pDC;
}
BOOL CpracticeView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CpracticeView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}
BOOL CpracticeView::SetupPixelFormat(PIXELFORMATDESCRIPTOR* pPFD)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof( PIXELFORMATDESCRIPTOR ),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0,
16,
0, 0,
PFD_MAIN_PLANE,
0, 0, 0, 0 };
int pixelformat;
PIXELFORMATDESCRIPTOR* pPFDtoUse;
pPFDtoUse = (0 == pPFD)? &pfd : pPFD;
if ( 0 == ( pixelformat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), pPFDtoUse ) ) )
{
::AfxMessageBox( "ChoosePixelFormat failed" );
return FALSE;
}
if ( FALSE == ::SetPixelFormat( m_pDC->GetSafeHdc(), pixelformat, pPFDtoUse ) )
{
::AfxMessageBox( "SetPixelFormat failed" );
return FALSE;
}
return TRUE;
}
void CpracticeView::DrawScence(){
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_POLYGON);
glVertex2f(100,100);
glVertex2f(100,200);
glVertex2f(200,200);
glVertex2f(200,100);
glEnd();
glFlush();
}
I have already add opengl32.lib ,glu32.lib,glut32.lib.
The output result should have a red rectangle,but my result have not any graphics.
Do you kown where the error is?