Sign in to follow this  

[.net] C#: updating both the opengl render context and the form

This topic is 3745 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

I'm trying to display some OpenGL stuff in a picture box, using C#. It's all working fine so far except for one thing: I've overridden the WndProc function of my form, where I now draw my scene and invalidate the form to force another redraw. This is to create a main loop. However, I cannot get the WndProc right to redraw both the form and the render context. This is my current WndProc code: It renders the opengl part fine (done in the Engine.Run() function) but the form itself gets sort of 'transparent', it's clearly not being updated anymore. If I remove the "Invalidate()" line it runs fine, the form is updated nicely but now there is no opengl on the screen where it should be.
		protected override void WndProc( ref System.Windows.Forms.Message m ) 
		{
			switch ( m.Msg )
			{
				case Engine.WM_ACTIVATEAPP:
				{
					Engine.Instance.Active = ( ( int )m.WParam == Engine.WA_ACTIVE || ( int )m.WParam == Engine.WA_CLICKACTIVE );
					Invalidate();

					break;
				}

				case Engine.WM_PAINT:
				{
					Engine.Instance.Run();
					base.WndProc( ref m );
					Invalidate();

					break;
				}

				default:
				{
					base.WndProc( ref m );

					break;
				}
			}
		}


how can I achieve both being displayed correctly?

Share this post


Link to post
Share on other sites
There is a better way than the paint-invalidate loop: hook the Application.Idle event and DllImport the PeekMessage function and the MSG struct.

Then:

[...]
Application.Idle += Application_Idle;
[...]

void Application_Idle(object sender, EventArgs e)
{
MSG msg;
while (!API.PeekMessage(ref msg, IntPtr.Zero, 0, 0, 0))
{
Engine.Instance.Run();
}
}




You might want to take a look at OpenTK (see sig), which provides a cross-platform OpenGL control (as well as complete OpenGL bindings, support for input devices and more). Shameless plug, I know, but the source would give you a good idea how to achieve what you want (MIT licensed so you can use it freely). Or you might use it directly.

Share this post


Link to post
Share on other sites

This topic is 3745 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