Render in a selected control

Hi, I am new in openGl. How can I render my glut scene in selected windows form control. For instance I have a panel in my form and I want my scene to shown in that panel.


the answer would be longer then you may expect.
but the basic is: you need a DeviceContect propperbly set up for Open GL … so a opengl Context.
You may google this topic to find a complex introduction.


I have googled the topic. I tried many things but nothing shown on my panel. Here is the part of my code

mhWnd = (HWND)mainControlToRender.ToPointer();

mhDC = GetDC( mhWnd );

// set the pixel format for the DC
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
int format = ChoosePixelFormat( mhDC, &pfd );
SetPixelFormat( mhDC, format, &pfd );

// create the render context (RC)
Class1::mhRC = wglCreateContext( mhDC );

// make it the current render context
wglMakeCurrent( mhDC, mhRC );

Could anyone say what is wrong? I am developing my OpenGL class in C++/CLR and use that dll in my C# project.


this looks basicly right, but there are so many OpenGL settings that may leed to the fact that you can´t see anything.
my hint would be.
cut out your opengl part into a simple c++ / opengl programm running in the full window.
when you are shure its showing correctly in this version move it back to the programm that should only show in a pannel.
we could help you mutch better with this ‘only opengl’ version.


say hello to Hugo:

Have you remembered to swap buffers after drawing? If you write:

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

does the control turn red? If not have you checked that the context is created successfully? i.e. Is mhRC != NULL ?

Also, SetPixelFormat can only be called once for a window so if it’s called elsewhere and sets a pixel format that doesn’t support OpenGL, then you will need to recreate the window to set a pixel format that does support OpenGL.

In my c# app I call my cli method as myObject.initWin(panel1.Handle);
I gave my code above. It seems correct but nothing happens in panel.