Windows XP重新绘制OpenGL应用程序,没有任何消息

时间:2016-01-05 16:00:49

标签: windows opengl paint

我正在使用带有相当旧的英特尔视频适配器(82945G)的Windows XP。它支持OpenGL 1.1。

有一个简单的OpenGL应用程序。它处理WM_PAINT消息并绘制场景。

问题是,当我按下“开始”按钮时,窗口会被完全删除。那里出现了一些垃圾。当我将光标悬停在关闭按钮上并出现工具提示时,会发生同样的情况。

我已经检查过应用程序在此期间没有收到任何消息。 有些东西让我觉得windows只是重新绘制桌面并擦除我的窗口。我怎么能避免这个?

运行该应用程序。确保窗口在桌面上。调整一下。按下开始按钮或将光标悬停在关闭按钮上并闪烁,直到出现工具提示。

这是源代码:

#include <stdio.h>
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>

HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;

bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
    if (height==0)height=1;

    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int InitGL(GLvoid)
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    return TRUE;
}

int DrawGLScene(GLvoid)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    return TRUE;
}

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
    GLuint PixelFormat;
    WNDCLASS wc;
    DWORD dwExStyle;
    DWORD dwStyle;
    RECT WindowRect;
    WindowRect.left=(long)0;
    WindowRect.right=(long)width;
    WindowRect.top=(long)0;
    WindowRect.bottom=(long)height;
    fullscreen=fullscreenflag;

    hInstance = GetModuleHandle(NULL);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "OpenGL";

    if (!RegisterClass(&wc))
    {
        MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;
    }

    dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    dwStyle=WS_OVERLAPPEDWINDOW;

    AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);

    // Create The Window
    if (!(hWnd=CreateWindowEx( dwExStyle,"OpenGL", title,dwStyle |
        WS_CLIPSIBLINGS |WS_CLIPCHILDREN,0, 0,WindowRect.right-WindowRect.left,
        WindowRect.bottom-
        WindowRect.top,NULL,NULL,hInstance,NULL)))return FALSE;

    static PIXELFORMATDESCRIPTOR pfd=
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        bits,
        0, 0, 0, 0, 0, 0,
        0,
        0,
        0,
        0, 0, 0, 0,
        16,
        0,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0
    };

    if (!(hDC=GetDC(hWnd))) return FALSE;
    if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) return FALSE;
    if(!SetPixelFormat(hDC,PixelFormat,&pfd))return FALSE;
    if (!(hRC=wglCreateContext(hDC)))return FALSE;
    if(!wglMakeCurrent(hDC,hRC))return FALSE;
    ShowWindow(hWnd,SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    ReSizeGLScene(width, height);
    if (!InitGL())return FALSE;
    return TRUE;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    wchar_t buffer[256];
    swprintf(buffer,L"message is: %d\n");
    OutputDebugStringW(buffer);
    switch (uMsg)
    {
    case WM_SIZE:
        {
            ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
            return 0;
        }
    case WM_PAINT:
        {
            DrawGLScene();
            SwapBuffers(hDC);
            RECT rect;
            GetClientRect(hWnd,&rect);
            ValidateRect(hWnd,&rect);
            return 0;
        }
    }
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    BOOL done=FALSE;
    fullscreen=FALSE;
    CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen);
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

0 个答案:

没有答案