How to use GL functions outside of WM_PAINT

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).

Waiting for a help !


Well, all I can say is “do it”. What you’ve described in your post is exactly what you’re supposed to do…

Except, all you need to do is ensure your dc and rc is current (wglMakeCurrent), and then do your drawing of whatever you want.


It’s does`'t work.

  1. The DC and RC have been created on WM_CREATE and removed on WM_DESTROY. Because they are statical varaibles i think it is not neccessary to use GetDC and ReleaseDC with any kind of WGL functions on any other message for the current Window.

  2. Can you provide a small piece of code to demonstrate your solution ?


If the window has its own DC (I guess this is what you mean by “static”) you still need to call GetDC() to make it the “active” DC for GDI. The call will just return the window’s existing DC, it won’t create a new one.

You shouldn’t be calling ReleaseDC on an own-DC though; maybe that’s your problem.

I have a bad experience of trying to do that in VC. I was also not able to do that. If you are using two class architecture i.e. only mainframe and winapp class then this can be done, but in document view architecture release DC after rendering and make current using wglMakeCurrent(dc->m_hDC,m_hglrc) and not wglGetCurrent(). If anything works please post back.

It seems to be a big problem ! NE1 has a solution or not ? I am really need a tip to do this !!!


Just read the documentation and follow it.

wglMakeCurrent() will make sure that rendering happens into the context that you expect it to.

Try this: how would you structure your drawing code if you didn’t use GL? Do the same thing, except add wglMakeCurrent() before you start rendering GL.

Also, it’s a bad idea to hang on to DCs after you need them. It may, possibly, sometimes be necessary for FPS type games, but if you do a renderer or something else that’s windowed and interactive (which you seem to be doing), it’s easiest to follow the rules, and obtain the DC when necessary, and release it when done.

I must say that all yours “ideas” DO NOT WORK !

I will solve a following problem - ribbon line. Click a mouse button, hold it and move until required point on screen. The ribbon line must follow to mouse cursor. Any idea ?


Hey bratan, tvoego opisaniya problemy malovato… There’s no problem with using of GL outside of WM_PAINT handler. There’s great idea of graph API (Cosmo3D from SGI for instance…). I do not advice to use Cosmo3D cause it’s a raw thing and they stopped to develop it now. But it’s a good learning material.
I do some kind of CAD application which is deeply dependent from user’s input. That’s how I orginized my code. Scene is hierarchical graph of primitives (lines, quads… sensors - invisible objects to receive user’s input) to handle with different action classes (compile - to produce display lists, draw - to sort primitives to emulate the transparency, pick - to get the object pointed with the mouse, action of sensor’s handling). So on WM_CREATE application creates and executes GL context class, default scene graph, applyes compile action to the graph and first stage of draw action which bilds the linear list of primitives to draw and sorts it. WM_PAINT handler calls every GL display list or calls build method directly if object was changed and swaps buffers to display generated image inside window. Mouse handlers use pick action and sensor handling actions to get selected object and modify scene graph and call Invalidate(). That’s all.

Finally I got it working !!!

On each WM-message you need activate hglRC on current hDC again using wglMakeCurrent(…) and finally call SwapBuffers(…). It’s all. No Invalidate needed. See code below.
// Window procedure, handles all messages for this program
UINT message,
WPARAM wParam,
LPARAM lParam)
static HGLRC hRC; // Permanent Rendering context
static HDC hDC; // Private GDI Device context

switch (message)
	// Window creation, setup for OpenGL
	case WM_CREATE:
		// Store the device context
		hDC = GetDC(hWnd);

		// Select the pixel format

		// Create palette if needed
		hPalette = GetOpenGLPalette(hDC);

		// Create the rendering context and make it current
		hRC = wglCreateContext(hDC);
     wglMakeCurrent(hDC, hRC);

		// Call OpenGL setup code

	// Window is being destroyed, cleanup
		// Deselect the current rendering context and delete it


		// Delete the palette if it was created
		if(hPalette != NULL)

		// Tell the application to terminate after the window
		// is gone.



// Activate hRC again
wglMakeCurrent(hDC, hRC);

    glColor3f(1.0f, 1.0f, 0.0f); // White

// Swap buffer for drawing


default:   // Passes it on if unproccessed
    return (DefWindowProc(hWnd, message, wParam, lParam));


return (0L);

PS: Nadejus chto moje reschenie pomoghet vsem zainteresovannim lizam.