.NET OpenGL

Is there a way to implement OpenGL in a
Windows Forms Application using VC++ .Net?

Thanks in advance

Dunno what you mean by Windows Forms Application but I use .NET and OpenGL.

Whatever you use you can get a context from an HDC (be calling GetDC) and a call to ChoosePixelFormat.

The rest is up to you. You could derive a class from something you can get an HDC from.

Windows Forms is the GUI framework that comes with .NET.

Assuming you can get a DC out of a Forms control, you should be able to make it work.

I think the best way to integrate non-managed APIs (such as OpenGL) with .NET when there is no managed wrapper already, is to use Managed C++ to write a wrapper; it’s fairly easy to mingle managed and unmanaged heap objects and safe and unsafe pointers in that environment. Then use this C++ wrapper from the .NET language of your choice.

You can get the DC by doing something like this:

Graphics g = this->CreateGraphics();
IntPtr hDC = g.GetHdc();

Now you have your DC. The ‘this’ is refering to your Form of your application. There are other classes that have the CreateGraphics function like the Control class, etc. In order to draw to anything yourself, you must get a Graphics object, which is just a drawing surface. From here you can use any of the GDI+ drawing functions or get the DC and go from there, with say opengl or something.

You will also have to do what jwatte said about making a wrapper with the mixing of managed and unmanaged code since there is no managed OpenGL assembly.

-SirKnight

Hmm… so MFC code is old hat now, guess I’d better brush up on the .NET GUI stuff.

Yeah this is going more and more off topic but using .NET for GUIs makes writing GUI apps very easy. Using .NET makes windows programming a breeze. The downside is if someone wants to run your program, they have to have the .NET framework installed. Once Longhorn arrives, that wont be a problem for users of that OS though. I still think wxWindows (now wxWidgets) is a very good GUI SDK which now supports .NET I think. Plus its cross platform.

-SirKnight

Yes, you can do it. I have done it with the Shader Designer.
The way is to get the HWND of the Window/control into you want render the opengl stuff.
You only need this line:
HWND hWnd = (HWND)mydotNetWindow->get_Handle().ToPointer();
with this you can call win32 functions like getDC

First, thanks so much for trying to help me out and I apologize for asking such a generalized question. Next, I have linked with opengl32.lib and glu32.lib. Also I have #include <gl/gl.h> and <gl/glu.h> in the main .cpp file.

Whenever i try to build the project I get a rather long list of errors. I have pasted a few of them here…

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152): error C2144: syntax error : ‘void’ should be preceded by ‘;’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152): error C2146: syntax error : missing ‘;’ before identifier ‘glAccum’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152): error C2182: ‘APIENTRY’ : illegal use of type ‘void’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1153): error C2144: syntax error : ‘void’ should be preceded by ‘;’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1153): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1153): error C2086: ‘int WINGDIAPI’ : redefinition

    C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152) : see declaration of 'WINGDIAPI'

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1153): error C2146: syntax error : missing ‘;’ before identifier ‘glAlphaFunc’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1153): error C2182: ‘APIENTRY’ : illegal use of type ‘void’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1153): error C2086: ‘int APIENTRY’ : redefinition

    C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152) : see declaration of 'APIENTRY'

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1154): error C2146: syntax error : missing ‘;’ before identifier ‘GLboolean’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1154): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1154): error C2086: ‘int WINGDIAPI’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1152) : see declaration of ‘WINGDIAPI’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1154): error C2146: syntax error : missing ‘;’ before identifier ‘glAreTexturesResident’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1154): error C2371: ‘APIENTRY’ : redefinition; different basic types

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1155): error C2144: syntax error : ‘void’ should be preceded by ‘;’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1155): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1155): error C2086: ‘int WINGDIAPI’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1155): error C2146: syntax error : missing ‘;’ before identifier ‘glArrayElement’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1155): error C2182: ‘APIENTRY’ : illegal use of type ‘void’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1155): error C2086: ‘int APIENTRY’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1156): error C2144: syntax error : ‘void’ should be preceded by ‘;’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1156): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1156): error C2086: ‘int WINGDIAPI’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1156): error C2146: syntax error : missing ‘;’ before identifier ‘glBegin’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1156): error C2182: ‘APIENTRY’ : illegal use of type ‘void’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1156): error C2086: ‘int APIENTRY’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1157): error C2144: syntax error : ‘void’ should be preceded by ‘;’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1157): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1157): error C2086: ‘int WINGDIAPI’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1157): error C2146: syntax error : missing ‘;’ before identifier ‘glBindTexture’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1157): error C2182: ‘APIENTRY’ : illegal use of type ‘void’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1157): error C2086: ‘int APIENTRY’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1158): error C2144: syntax error : ‘void’ should be preceded by ‘;’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1158): error C2501: ‘WINGDIAPI’ : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1158): error C2086: ‘int WINGDIAPI’ : redefinition

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1158): error C2146: syntax error : missing ‘;’ before identifier ‘glBitmap’

C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(1158): error C2182: ‘APIENTRY’ : illegal use of type ‘void’

This goes on and on for two pages

Am i doing something wrong here?

Is this why i need to use a wrapper?

Sorry for the bloated post and any “more” help would be appreciated

Make sure to include windows.h before gl.h

OMG ~DUH!~
Thanks