Directx 设备不会为窗口初始化,程序挂起

本文关键字:初始化 程序 挂起 窗口 Directx | 更新日期: 2023-09-27 18:37:24

我尝试了几个教程,似乎遇到了问题。

有一个现有的程序,我正在尝试添加一个 directx 窗口作为额外的弹出论坛,该论坛将作为主应用程序表单的子级运行。

下面是窗口窗体类:

public partial class DxWindow : Form
{
    Device device;
    public DxWindow()
    {
        InitializeComponent();
        initDevice();
    }
    private void initDevice()
    {
        MessageBox.Show("hello");
        PresentParameters pp = new PresentParameters();
        pp.Windowed = true;
        pp.SwapEffect = SwapEffect.Discard;
        device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
    }
    private void Render()
    {
        //render stuff
    }
    private void DxWindow_Paint(object sender, PaintEventArgs e)
    {
        Render();
    }
}

这是我初始化表单的地方(从主窗口中的 UI 按钮)

private void toolStripButton3_Click_1(object sender, EventArgs e)
{
    if (DirectxWindow == null)
    {
        DirectxWindow = new DxWindow();
        DirectxWindow.Show();
    }
}

当我运行程序并单击按钮时。 它似乎在内存中创建表单,但从未出现。 当我在调试器中单步执行它时,它会到达"DirectxWindow = new DxWindow();",然后自动跳出中断模式并在主窗口冻结且没有新的 Dxwindow() 的情况下继续运行。

当我中断执行时,似乎仍在"DirectxWindow = new DxWindow();"

此外,DxWindow 构造函数中的"MessageBox.Show("hello");"永远不会被调用"

编辑:我推断,一旦它点击"PresentParameters pp = new Microsoft.DirectX.Direct3D.PresentParameters();",应用程序就会变得无响应而不会引发任何错误。

Directx 设备不会为窗口初始化,程序挂起

原来我的问题需要使用

<startup useLegacyV2RuntimeActivationPolicy="true">

在"应用程序配置"文件中

在此处找到解决方案:混合模式程序集是针对版本"v1.1.4322"构建

虽然我从来没有收到OP描述的错误,但我只是遇到了评论中描述的这个问题:"谢谢!!!这是我遇到过的最奇怪的问题。在VS 2012 .Net 4.0中,我的应用程序将在初始化与此DLL相关的任何类型的变量时挂起。我从未见过这样的东西。在我找到这个之前找不到关于这个问题的任何信息!"