Unmanaged OpenGL in .NET

I’m trying to migrate my old Win32 OpenGL code to C++ .NET to make it easier to build interfaces in Windows Forms (specifically to render OpenGL into a PictureBox control).
Currently, I’m only interested in C++ (not C# or VB), so I’ve been calling unmanaged setup/rendering functions from the managed windows form code (passing the HWND of the PictureBox to the OpenGL functions).
This works fine most of the time. However, on resize or maximize/minimize, not all of the OpenGL PictureBox window is rendered - perhaps only the top third will be rendered, and sometimes it is not rendered at all until I resize again to generate another paint event - which then paints the entire window without a problem!!
Does anyone know why this intermittent rendering occurs in .NET?

May be you just forgot to process WM_SIZE messeges. You must call glViewport every time you change sizes

Yes, I’ve been calling glViewport on each resize. I’m not processing WM_SIZE events directly as I’ve been doing in Win32, but using the methods inherited via the .NET Form instead. The resize event handler is defined as…

private: System::Void pictureBox1_Resize(Object *sender, EventArgs *e) {
extern bool glOK;
HWND hwnd = (HWND)pictureBox1->Handle.ToInt64();

 if (glOK) {


where pictureBox1 is the child pictureBox control I’m using to display OpenGL output.

The configureGLViewport function in turn calls glViewport using the client RECT from hwnd. The pictureBox1->Invalidate method call should then force a redraw? It appears that the viewport is resized ok, but sometimes, the OpenGL content in pictureBox1 only gets partially redrawn.

I suggest you to use a panel instead of a picturebox. The panel is the simplest .NET object in where you can draw, so you will have less overhead. Also, try with ToPointer() instead of ToInt64() and remember change all matrices and viewport in the Resize method. Also, if you will continue using a picturebox, disable the autodrawing, stretch image, and all related properties.

I suggest too that download the Shader Designer’s source code. It was programmed with C++ .NET and could help you with your problem.

Many thanks! I’ve used the panel and this seems to work. Prior to this my temporary fix was to call the OpenGL render function directly in the above resize handler, instead of invalidating the client area of the picturebox control.
I realised that the picturebox control was painting the background color too (.NET’s rendering order or threading of this I’m not sure of), but it was this background colour that seemed to overwrite the opengl rendering - the panel does not appear to do this so the OpenGL appears okay on resize. I think I’ll stick with the panel, but I’d still be interested to see if your advice on switching off autodrawing etc will work for the picturebox as well.
Also thanks for the tip on the Shader Designer source code!

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