2011-04-21 66 views
2

如何在Windows窗体中显示过量窗口?关于C#表单的OpenGL视图

glutCreateWindow( “实施例”)创建另一种形式中,

glutCreateSubWindow(HWND,0,0,100,100),其中hwnd是处理在C#我的主窗口的形式,我得到一个AccessViolation异常。

Glut程序在C++ DLL中。我的应用程序在C#WPF上。我需要在我的C#形式显示过剩视图

C++代码:

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#代码:

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

我没有足够的信心张贴作为回答,但一对夫妇的想法... 1. /在你的C#代码中尝试在Window_Loaded和InitGlut中添加'unsafe'。 2.确保您使用的是全部3个元素(GLUT库,C++代码和C#代码)的64位版本。 – 2011-04-21 20:24:10

+0

您的代码中存在签名不匹配,这将导致64位操作系统崩溃。 C++方面希望hwnd是一个'int'(这也是错误的 - 它们是指针大小的),C#将它作为'IntPtr'传递(正确,但不是C++期望的)。 – ChrisV 2011-04-21 21:05:33

回答

1

你看上去托管在WPF形式的Win32对象。是的,这需要解决方法。

你见过MSDN上的WPF和Win32互操作指南吗?

http://msdn.microsoft.com/en-us/library/ms742522.aspx

你需要检查出HwndHost类,太:

http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndhost.aspx

+0

这不完全是Win32对象。例如,我从本教程中创建一个简单的glut应用程序(http://www.lighthouse3d.com/tutorials/glut-tutorial/initialization/)。这是一个Win32控制台应用程序,它在glutCreateWindow函数的帮助下创建glut窗口。我将其更改为dll并从我的WPF调用它的函数。 – EthanHunt 2011-04-22 10:19:42

+0

GLUT可能被WPF视为一个Win32对象,因为它不是WPF。 – djdanlib 2011-04-25 14:04:25