下面是代码
1 int COpenGLSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct) 2 { 3 if (CView::OnCreate(lpCreateStruct) == -1) 4 return -1; 5 6 // TODO: 在此添加您专用的创建代码 7 8 //设置RC像素格式 9 10 int pixelformat; 11 m_pDC = new CClientDC(this); //在客户区作图,获得设备描述符DC; 12 ASSERT(m_pDC != NULL); 13 14 m_hrc = wglCreateContext(m_pDC->GetSafeHdc()); 15 wglMakeCurrent(m_pDC->GetSafeHdc(),m_hrc); 16 17 static PIXELFORMATDESCRIPTOR pfd = { 18 19 sizeof(PIXELFORMATDESCRIPTOR), 20 1, 21 PFD_DRAW_TO_WINDOW | 22 PFD_SUPPORT_OPENGL | 23 PFD_DOUBLEBUFFER, 24 PFD_TYPE_RGBA, 25 16, 26 0,0,0,0,0,0, 27 0, 28 0, 29 0, 30 0,0,0,0, 31 32, 32 0, 33 0, 34 PFD_MAIN_PLANE, 35 0, 36 0,0,0 37 38 }; 39 40 if(pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd ) == 0 ){ 41 MessageBox(L"在该DC上找不到于PFD接近的位图结构"); 42 return FALSE; 43 } 44 45 int nRet = SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); 46 if ( nRet == FALSE)//测试硬件是否支持该pdf 47 { 48 MessageBox(L"无法在该DC上设置位图结构"); 49 DWORD err = GetLastError(); 50 //return FALSE; 51 } 52 53 54 glClearDepth(1.0f); 55 glEnable(GL_DEPTH_TEST); 56 57 glMatrixMode(GL_PROJECTION); 58 glLoadIdentity(); 59 60 glMatrixMode(GL_MODELVIEW); 61 62 return 0; 63 }
GetLastError()返回的是0.
下面是程序的结果
请问下到底哪里出错了,我是新手,望各位大侠指导。
if(pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd ) == 0 ) //自己弄错了,代码不好。上面应该给下面这句加上括号 (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd )) //自己代码太不规范了。。。 //常数比较时最好把常数放在前面 if( 0 == (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd )) ){ }