OpenGL view on C# Form

纵然是瞬间 提交于 2019-12-13 16:33:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!