Windows XP repaints OpenGL application without any messages

'm using Windows XP with a pretty old Intel video adapter (82945G). It supports OpenGL 1.1.
There is a simple OpenGL application. It processes WM_PAINT message and draws the scene.
The problem is that when I press the Start button the window gets completely erased. Some rubbish appears there. The same situation is happening when I hover cursor on close button and a tooltip appears.
I have checked that the application receives no messages during that. Something makes me think that windows just repaints desktop and erases my window. How could I avoid that?
Run the application. Make sure that window is on the desktop. Resize it a bit. the press start button or hover cursor over close button and waint until tooltip appear.
Here is a source code:


#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
");
    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;
}

I found that when i put the window upon another window which is repainted constantly my window is got repainted also.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.