Sign in to follow this  

OpenGL in a 'Windows Forms Application'

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! I'm pretty familiar with Opengl however most of the initialization code I have obtained from other sites to create the window. I have just discovered the windows forms application in dev studio and although ive been stumbling about with it, ive got it to do some pretty cool stuff, however I would like to create an opengl window inside of one of these windows forms so that I can create a level editor. Does anyone know how i can get the context of my windows form ? to draw opengl into it, or how I would go about initialising this? If anyone knows how to draw into seperate panels for four windows etc that would be much appreciated. Thanks! Chris

Share this post


Link to post
Share on other sites
You could try the Tao framework. I think the Tao.OpenGL namespace has an OpenGL control in it.

You can also create a rendering context using the HDC of your panels, or whatever else you want to render to.

EDIT: I am assuming you're using Tao or some other managed OpenGL library.

Share this post


Link to post
Share on other sites
Thanks for the quick reply! Just decided to take a look around the net myself and see what I could find, I'm not using any libraries other than the standard GL library's.

I have found the following link however which looks pretty promising :)
http://www.codeproject.com/managedcpp/OpenGLViewWinForms.asp

Share this post


Link to post
Share on other sites
Quote:
Original post by chipmeisterc
Thanks for the quick reply! Just decided to take a look around the net myself and see what I could find, I'm not using any libraries other than the standard GL library's.

I have found the following link however which looks pretty promising :)
http://www.codeproject.com/managedcpp/OpenGLViewWinForms.asp
This is a complete no-brainer. Just get the HDC to whatever control you want to be the openGL viewport, and then render to it's client area.

Here's a class method I made ages ago to do this for me.

//init opengl in a premade control
int gl_video::MakeControl(HWND &hWnd)
{
//make sure the handle points to a valid control
if (hWnd == NULL)
{
MessageBox(NULL, "ERROR: Could not find control handle.", m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

m_WindowHandle = hWnd;

//atempt to create a device context
m_DeviceContext = GetDC(m_WindowHandle);
if (m_DeviceContext == NULL)
{
MessageBox(NULL,"ERROR: Could not create device context.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

//attempt to get a pixel format
if (!SetupPixelFormat())
{
MessageBox(NULL,"ERROR: Could not set pixel format.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

//attempt to initialize the rendering context
m_RenderContext = wglCreateContext(m_DeviceContext);
if (m_RenderContext == NULL)
{
MessageBox(NULL,"ERROR: Could not create rendering context.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

if (!wglMakeCurrent(m_DeviceContext, m_RenderContext))
{
MessageBox(NULL,"ERROR: Could not activate rendering context.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

//get the size of the control into a rect
//and set the window size member variables
RECT WindowRect;
GetWindowRect(m_WindowHandle, &WindowRect);
m_WindowWidth = WindowRect.right-WindowRect.left;
m_WindowHeight = WindowRect.bottom-WindowRect.top;

return true;
}

Share this post


Link to post
Share on other sites
To clarify Vampyre_Dark's post, you can initialise opengl in a winform exactly like any other window. You need a few .net bindings to win32 functions (which you can either get through the Tao framework or write yourself). Then intialise OpengL just like your existing code, using Form.Handle as the HWND.

Share this post


Link to post
Share on other sites
Hi Vampyre, just a quick question. What's the speed cost of spawning an opengl window like that? I'm interested in using a similar technique, but yours looks cleaner :)

Share this post


Link to post
Share on other sites
First of all Tao works with C# which is not working with most of c++ components. Now Vampyre_Dark wrote the easiest part of work, I cant get normal hdc and this is biggest problem! Till now I was unable to create OpenGL in windows forms, please chipmeisterc if You find any way to create such application help me!!!

Share this post


Link to post
Share on other sites

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this