Get Window Handle, Dev Contx, Ren Contx in GLUT

FYI … Thought I would share …

Here is a slick way to get the window handle in GLUT without getting the GLUT source code and modifying it.

  1. Set the glut window title to a known string …
const char *windowTitle = "This is my title";
glutSetWindowTitle(windowTitle);
  1. The rest all falls into place once we use FindWindow(…) along with that known string:
HWND windowHandle = FindWindow(NULL,windowTitle);
HDC deviceContext = GetDC(windowHandle);
HGLRC renderingContext = wglGetCurrentContext();

Enjoy!
Paul

Try this…

  
wglGetCurrentDC
The wglGetCurrentDC function obtains a handle to the device context that is associated with the
current OpenGL rendering context of the calling thread.

HDC wglGetCurrentDC(VOID);
Parameters
This function has no parameters. 

Return Values
If the calling thread has a current OpenGL rendering context, the function returns a handle
to the device context associated with that rendering context by means of the wglMakeCurrent
function. Otherwise, the return value is NULL.

Remarks
You associate a device context with an OpenGL rendering context when it calls the wglMakeCurrent 
function. You can use the wglGetCurrentContext function to obtain a handle to the calling 
thread's current OpenGL rendering context.

Requirements 
  Windows NT/2000: Requires Windows NT 3.5 or later.
  Windows 95/98: Requires Windows 95 or later. Available as a redistributable for Windows 95.
  Header: Declared in wingdi.h.
  Import Library: Use opengl32.lib.
  
wglGetCurrentContext
The wglGetCurrentContext function obtains a handle to the current OpenGL rendering context of the 
calling thread.

HGLRC wglGetCurrentContext(VOID);
Parameters
This function has no parameters. 

Return Values
If the calling thread has a current OpenGL rendering context, wglGetCurrentContext returns a 
handle to that rendering context. Otherwise, the return value is NULL.

Remarks
The current OpenGL rendering context of a thread is associated with a device context by means of 
the wglMakeCurrent function. You can use the wglGetCurrentDC function to obtain a handle to the 
device context associated with the current OpenGL rendering context.

Requirements 
  Windows NT/2000: Requires Windows NT 3.5 or later.
  Windows 95/98: Requires Windows 95 or later. Available as a redistributable for Windows 95.
  Header: Declared in wingdi.h.
  Import Library: Use opengl32.lib.

yooyo

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