简体   繁体   中英

OpenGL view on C# Form

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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM