PDA

View Full Version : Create C# Winforms with VC++ OpenGL Scene



dinesh17
12-17-2014, 10:22 PM
Is it possible to render VC++ OpenGL output to a panel in Winforms window created with C# ? I'm aware of Tao framework and OpenTK but I don't want to rely on third party support. There is an article here (https://cadcammodelling.wordpress.com/2010/11/12/unmanaged-c-opengl-drawing-and-c-winformswpf-interoperability/) but this is merely only pixel reproduction and I think this method does not guarantee user interaction with OpenGL scene.

Shinta
01-05-2015, 08:19 AM
Hiho,

I don't know how your VC++ OpenGL output is realised, but I guess it will need a window (or device/rendering context) to work. So you'll need a control in WinForms that acts as a canvas for OpenGL, any UserControl that set the class styles CS_VREDRAW, CS_HREDRAW and CS_OWNDC (and some WinForms control styles) should suffice. And since Control.Handle is set public, you can get the HWND handle (as IntPtr) there, and init your OpenGL output in VC++ with it.



public static class CS
{
/// <summary>
/// Redraws the entire window if a movement or size adjustment changes the height of the client area.
/// </summary>
public const int VREDRAW=0x00000001;

/// <summary>
/// Redraws the entire window if a movement or size adjustment changes the width of the client area.
/// </summary>
public const int HREDRAW=0x00000002;

/// <summary>
/// Allocates a unique device context for each window in the class.
/// </summary>
public const int OWNDC=0x00000020;

// ... more
}

public partial class DummyOpenGLControl : UserControl
{
/// <summary>
/// Constructes a new instance of this class.
/// </summary>
public DummyOpenGLControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint|Contro lStyles.Opaque|ControlStyles.ResizeRedraw|ControlS tyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, false);

InitializeComponent();
}

/// <summary>
/// Adds the class styles <see cref="CS.VREDRAW"/>, <see cref="CS.HREDRAW"/> and <see cref="CS.OWNDC"/> to support OpenGL rendering.
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp=base.CreateParams;
cp.ClassStyle|=CS.VREDRAW|CS.HREDRAW|CS.OWNDC;
// maybe cp.Style|=WS.CLIPCHILDREN|WS.CLIPSIBLINGS;
return cp;
}
}

// ... more
}


How you can connect your WinForms user interactions with your VC++ OpenGL output depends on the way it works, can't help you there w/o more infos.