Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
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* /*pDC*/)
{
	CpracticeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: 在此处为本机数据添加绘制代码
	DrawScence();
}

int CpracticeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	m_pDC = new CClientDC(this);	// get device context
	if( m_pDC == NULL )
	{
		::AfxMessageBox( "fail to get device context" );
		return FALSE;
	}

	if( !SetupPixelFormat() )	// setup pixel format
	{
		::AfxMessageBox( "SetupPixelFormat failed" );
		return FALSE;
	}
	
	// get rendering context
	if( ( m_hRC = wglCreateContext(m_pDC->GetSafeHdc()) ) == 0 )
	{
		::AfxMessageBox( "wglCreateContext failed" );
		return FALSE;
	}

	// make current rendering context
	if( wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) == FALSE )
	{
		::AfxMessageBox( "wglMakeCurrent failed" );
		return FALSE;
	}
	return 0;
}

void CpracticeView::OnDestroy()
{
	CView::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
	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)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//return CView::OnEraseBkgnd(pDC);
	return TRUE;
}

void CpracticeView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	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?
Posted
Updated 22-Oct-13 5:30am
v2
Comments
Santhosh G_ 22-Oct-13 14:37pm    
Please ensure the version of OpenGL.
CHAR* pVersion = (CHAR*)glGetString(GL_VERSION); Call this after wglMakeCurrent().

Sorry I could not find any errors in the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900