我用MFC 建了一个单文档,想用OpenGL来画图,但是第一步就收挫,没有出现我想要的结果,View类的代码如下,请高手指出其中的错误,小弟不胜感激!
#include "stdafx.h"
#include "MySDOpenGL.h"#include "MySDOpenGLDoc.h"
#include "MySDOpenGLView.h"#include <GL/glut.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMySDOpenGLViewIMPLEMENT_DYNCREATE(CMySDOpenGLView, CView)BEGIN_MESSAGE_MAP(CMySDOpenGLView, CView)
//{{AFX_MSG_MAP(CMySDOpenGLView)
ON_WM_CREATE()
ON_WM_PAINT()
//}}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()/////////////////////////////////////////////////////////////////////////////
// CMySDOpenGLView construction/destructionCMySDOpenGLView::CMySDOpenGLView()
{
// TODO: add construction code here}CMySDOpenGLView::~CMySDOpenGLView()
{
DestroyOpenGL();
}BOOL CMySDOpenGLView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
}/////////////////////////////////////////////////////////////////////////////
// CMySDOpenGLView drawingvoid CMySDOpenGLView::OnDraw(CDC* pDC)
{
CMySDOpenGLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}/////////////////////////////////////////////////////////////////////////////
// CMySDOpenGLView printingBOOL CMySDOpenGLView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}void CMySDOpenGLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}void CMySDOpenGLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}/////////////////////////////////////////////////////////////////////////////
// CMySDOpenGLView diagnostics#ifdef _DEBUG
void CMySDOpenGLView::AssertValid() const
{
CView::AssertValid();
}void CMySDOpenGLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}CMySDOpenGLDoc* CMySDOpenGLView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMySDOpenGLDoc)));
return (CMySDOpenGLDoc*)m_pDocument;
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CMySDOpenGLView message handlersint CMySDOpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
InitOpenGL();
// TODO: Add your specialized creation code here

return 0;
}void CMySDOpenGLView::InitOpenGL(void)
{
int nPixelFormat; // Pixel format index
m_hDC= ::GetDC(m_hWnd);// Get the Device context
/*CClientDC dc(this);
dc.TextOut(50,50,"octopus");*/
PIXELFORMATDESCRIPTOR pixelDesc;

pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits =24;
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 8;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 0;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
// Choose a pixel format that best matches that described in pfd
if((nPixelFormat = ::ChoosePixelFormat(m_hDC, &pixelDesc))==0)
{
::MessageBox(this->m_hWnd,"Choose Pixel Format Failed!\t","ERROR",MB_OK);
return;
}

// Set the pixel format for the device context
if(!::SetPixelFormat(m_hDC, nPixelFormat, &pixelDesc))
{
::MessageBox(this->m_hWnd,"Set Pixel Format Failed!\t","ERROR",MB_OK);
return;
}

nPixelFormat=::GetPixelFormat(m_hDC);
::DescribePixelFormat(m_hDC,nPixelFormat,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc); m_hRC = wglCreateContext(m_hDC);
::wglMakeCurrent(m_hDC,m_hRC);
glClearColor(1.0f,0.0f,0.2f,0.0f);
glFlush();
}void CMySDOpenGLView::OnPaint() 
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
//InitOpenGL();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,1.0);
glBegin(GL_TRIANGLES);
glVertex2i(0,0);
glVertex2i(1,0);
glVertex2i(1,1);
glEnd();
glFlush();

// Do not call CView::OnPaint() for painting messages
}void CMySDOpenGLView::DestroyOpenGL()
{
::wglMakeCurrent(NULL,NULL);
if(m_hRC)
::wglDeleteContext(m_hRC);
if(m_hDC)
::ReleaseDC(this->m_hWnd,m_hDC);
return;
}我希望的是运行后窗口客户区变成指定的颜色,但是窗口没有反应。
谢谢。
我只有20多分,不好意思了。