Barebone example of Hosting an Unmanaged C++ OpenGL Window in C# Winforms/WPF

Started by
6 comments, last by Nik02 9 years, 1 month ago

Hi,

Can anyone please share an example project where we can see a OpenGL Window running inside a Win32 Form/WPF using c#.

Till now i was using GLFW to create a window and render to that window. But, it would be awesome if i can render into a Winform, and create buttons,labels etc to use for creating visual debugging .....and hopefully some day into a full fledged WYSIWYG editor!

So, simply put, how can i get the hwnd from c# winform to a C++ opengl app.

I think below two replies address the above line.But, I needed more information than that, i need a complete example project/solution that i can compile and test, and learn from it, and develop on top of it.

Any useful links are also appreciated.

Thanks.

Advertisement

One thing you can do is to just drop a panel control on the form, and then use the hWnd property of that panel to spool up a GL context as usual.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

In addition to what Promit said, WPF has a HwndHost control that can host a native window. There are some issues with clipping/overlapping and such, but if you stay with simple rectangular stuff you'll be fine smile.png

Niko Suni

In addition to what Promit said, WPF has a HwndHost control that can host a native window. There are some issues with clipping/overlapping and such, but if you stay with simple rectangular stuff you'll be fine smile.png

One thing you can do is to just drop a panel control on the form, and then use the hWnd property of that panel to spool up a GL context as usual.

Thanks for the tips, but, since i'm a beginner im having a little hardtime, getting things up and running. I was hoping for a sample project that does the above.

i've modified the question a little to reflect my intention.

In both Forms and WPF, the mentioned techniques are very simple. I (and I believe Promit too) assumed that you can create a native window to begin with.

Both approaches give you a window handle (a HWND) that you can use as a drawing area.

Niko Suni

In both Forms and WPF, the mentioned techniques are very simple. I (and I believe Promit too) assumed that you can create a native window to begin with.

Both approaches give you a window handle (a HWND) that you can use as a drawing area.

I think i will post back with a Sample app myself, and then you guys can have look at it for corrections, where i fail.

p.s. it might take a while --- while i try :)

I think i will post back with a Sample app myself, and then you guys can have look at it for corrections, where i fail.

p.s. it might take a while --- while i try smile.png

You should google c++/CLI .

While we generally don't want to write complete samples just to reply to a topic, we can help you if you hit specific roadblocks. And yes, we can review your code if it is written concisely enough smile.png

Trying to code functionality yourself is the most effective way to learn.

To get you started though:

I checked the GLFW framework documentation last night, and that particular framework insists that it creates the rendering window for you. You will want to skip that, and as Promit said, create the OpenGL context manually against an existing window (which lives in your Forms or WPF app's control). This does not represent a huge amount of code anyway. Much of the rest of the code should be standard OpenGL stuff, regardless of which party created the window.

Niko Suni

This topic is closed to new replies.

Advertisement