用一个无模式对话框显示opengl图形,关闭对话框后(程序未关闭),再打开一次对话框就不能显示了?同时打开多个对话框也只是第一个能显示,怎么解决?

解决方案 »

  1.   

    // Draw.cpp : implementation file
    //#include "stdafx.h"
    #include "Nurbs.h"
    #include "Draw.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CDraw dialog
    CDraw::CDraw(CWnd* pParent /*=NULL*/)
    : CDialog(CDraw::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CDraw)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    }
    void CDraw::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDraw)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CDraw, CDialog)
    //{{AFX_MSG_MAP(CDraw)
    ON_WM_CREATE()
    ON_WM_DESTROY()
    ON_WM_PAINT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDraw message handlersint CDraw::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;

    PIXELFORMATDESCRIPTOR pixelDesc = { 
        sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
        1,                                // 版本号 
        PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
        PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
        PFD_DOUBLEBUFFER,                 // 双缓存模式 
        PFD_TYPE_RGBA,                    // RGBA 颜色模式 
        24,                               // 24 位颜色深度 
        0, 0, 0, 0, 0, 0,                 // 忽略颜色位 
        0,                                // 没有非透明度缓存 
        0,                                // 忽略移位位 
        0,                                // 无累加缓存 
        0, 0, 0, 0,                       // 忽略累加位 
        32,                               // 32 位深度缓存     
        0,                                // 无模板缓存 
        0,                                // 无辅助缓存 
        PFD_MAIN_PLANE,                   // 主层 
        0,                                // 保留 
        0, 0, 0                           // 忽略层,可见性和损毁掩模 
    }; HDC hDC = ::GetDC(GetSafeHwnd()); m_GLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc); //选择像素格式
    SetPixelFormat(hDC, m_GLPixelIndex, &pixelDesc); //设置像素格式 m_hGLContext = wglCreateContext(hDC);

    return 0;
    }void CDraw::OnDestroy() 
    {
    if(wglGetCurrentContext() != NULL)
    wglMakeCurrent(NULL,NULL); if(m_hGLContext != NULL)
    {
    wglDeleteContext(m_hGLContext);
    m_hGLContext = NULL;
    }

    CDialog::OnDestroy();

    delete this;
    }void CDraw::OnPaint() 
    {
    CPaintDC dc(this);  HDC hDC = ::GetDC(GetSafeHwnd());
    wglMakeCurrent(hDC,m_hGLContext); //以下为绘图代码 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(0.2f, 0.6f, 1.0f);
    auxWireSphere(0.5);
    ::SwapBuffers(hDC); }在CNurbsView.CPP中有以后调用:void CNurbsView::OnNurbsDraw() 
    {
    CDraw* pDraw = new CDraw();
    pDraw->Create(IDD_DRAW,NULL);
    pDraw->ShowWindow(SW_SHOW);
    }
      

  2.   

    HDC hDC = ::GetDC(GetSafeHwnd());
    凭直觉是这里有问题。因为除了你第一个窗口能绘制之外,其他的窗口都不能绘制。
    尤其是同时开多个窗口的时候。
    获取DC可以有多种途径的,试试其他方法。//以下为绘图代码 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(0.2f, 0.6f, 1.0f);
    auxWireSphere(0.5);
    ::SwapBuffers(hDC);还有这段,你用标准的GL库函数试试
      

  3.   

    void CDraw::OnPaint() 
    {
    CPaintDC dc(this);  // 其实这里不需要再取得环境句柄,用dc可以达到同样的效果
    //HDC hDC = ::GetDC(GetSafeHwnd()); 
    ...
    }