How to use GL functions outside of WM_PAINT message

I wish to use GL functions outside of WM_PAINT message. For example, pick of mouse
button and on WM_LBUTTONDOWN draw something on the screen immediately (Under WinAPI GetDC -> drawing routines -> ReleaseDC). I have placed this question in a beginners forum, but i did’t get a helpful tip.

Waiting for a help (with a small code example) !


You should not have any problems if you make your DC and your RC global.

I have tried it. No effect !

I am an Windows programmer with 10 years of expirience and i know a lot of rules and restrictions of this enviroment. Due to last days i will solve this “problem” and all “ideas” from the members of this board DO NOT WORK. Can NE1 explain me, for example, how i can realize a changable rectangle for a zooming inside of window. In Windows GDI - no problem, but in OpenGL - no way ? All examples, that i found use redraw of scene in WM_PAINT message (OpenGL Bible, rotation around different axes and so on). It is possible to collect all calls to GL functions outside of WM_PAINT, but a drawing event will be active on WM_PAINT. That means for any minor change of a scene i need process all GL calls !

Please help or point to link where i can get a help. Thx.

The GDI style of redrawing invalidated sub-areas of a window isn’t really appropriate here. A more suitable way of doing things is:

  1. Do GetDC() and wglCreateContext() at startup and hang on to the DC + GLRC for the lifetime of your app. Ignore the DC you get from the WM_PAINT message.
  2. If any portion of the rendering window needs redrawing, redraw the whole thing and call SwapBuffers to display it.

The reason for (2) is double-buffering. You need to call SwapBuffers to display what you’ve drawn, but that leaves you with a backbuffer with undefined contents, so you can’t reuse what you just drew. There’s no way of page-flipping a sub-region of a window, so to update any portion of the screen you must redraw everything.

What do you mean by a ‘changable rectangle’? If you mean a resizable window, just call glViewport whenever the window dimensions change and it should just work.

Mike F

Take a look at the following code. This issues openGL instructions(could be drawing instructions). m_pCDC is a member of the view that points to the DC. First it is made current, then drawing instructions are issued and finally it is released. This particlar function will cause a WM_PAINT message to happen anyway but if this is not the case then you need to ask for one by calling:

InvalidateRect(NULL,FALSE) is order to refresh the screen

and BTW, dont forget to have a glFlush() at the end of your drawing code.
this kind of code could easily be put in a mouse event handler with GL instructions to draw stuff.

void CBaseOpenGLMDIView::OnSize(UINT nType, int cx, int cy)
CView::OnSize(nType, cx, cy);

// Use the rendering context do draw onto the view
wglMakeCurrent(m_pCDC->GetSafeHdc(), m_hGLContext);

// Set up the viewport
GLsizei w,h;
w = cx;
h = cy;

// Give a clean surface ready to render.

// Release thr rendering context



I got it working !!!

See forum for beginners where i have described the solution with a small example in C.

Thx to all.