• 15
• 15
• 11
• 9
• 10

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

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

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 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.