问题
How to display a glut window inside Windows Form?
glutCreateWindow("Example") create another form,
glutCreateSubWindow(hwnd, 0, 0, 100, 100), where hwnd is handle to my main Window Form in C#, i get an AccessViolation Exception.
The Glut program is in a C++ DLL. My application is on C# WPF. I need to display glut view at my C# Form
C++ code:
extern "C"
{
__declspec(dllexport) int InitGlut(int hwnd, int top, int left, int width, int height)
{
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(top,left);
glutInitWindowSize(320,320);
//glutCreateWindow("Example");
glutCreateSubWindow(hwnd, top, left, width, height);
glutDisplayFunc(renderScene);
glutMainLoop();
return 0;
}
}
C# code:
const string pathToDll = "../../../Release/MyDLL.dll";
[DllImport(pathToDll)]
public static extern int InitGlut(IntPtr hwnd, int top, int left, int width, int height);
private void Window_Loaded(object sender, RoutedEventArgs e)
{
IntPtr hwnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;
InitGlut(hwnd, 0, 0, 100, 100);
}
回答1:
Looks like you're hosting a Win32 object in a WPF form. Yes, this requires workarounds.
Have you seen the WPF and Win32 Interoperation guide on MSDN?
http://msdn.microsoft.com/en-us/library/ms742522.aspx
You'll need to check out the HwndHost class, too:
http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndhost.aspx
来源:https://stackoverflow.com/questions/5747723/opengl-view-on-c-sharp-form