无法使用 Visual Studio 启动 DirectX 11 可执行文件,构建工作正常

Cannot start DirectX 11 executable with Visual Studio, Build works properly

以下是 Directx 11 代码,显示 window 并保持打开状态等待消息:

#include "stdafx.h"
#include <iostream>

LRESULT CALLBACK WindowProc(_In_ HWND   hwnd, _In_ UINT   uMsg,
    _In_ WPARAM wParam, _In_ LPARAM lParam)
{
    if (uMsg == WM_DESTROY) {
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

// Directx main
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmd, int nCmdShow)
{
    WNDCLASSEX window;
    ZeroMemory(&window, sizeof(WNDCLASSEX));
    window.cbSize = sizeof(WNDCLASSEX);
    window.hbrBackground = (HBRUSH) COLOR_WINDOW;
    window.hInstance = hInstance;
    window.lpfnWndProc = WindowProc;
    window.lpszClassName = (LPCWSTR)"MainWindow";   // class name
    window.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&window);

    HWND windowHandle = CreateWindow((LPCWSTR)"Main Window", (LPCWSTR)"DirectX Tut!", WS_OVERLAPPEDWINDOW,
        100, 100, 600, 800, NULL, NULL, hInstance, 0);

    if (!windowHandle) 
        return -1;

    ShowWindow(windowHandle, nCmdShow);

    MSG message;
    while (GetMessage(&message, NULL, 0, 0))    // continuously loop for messages
    {
        DispatchMessage(&message);
    }

    return 0;
}

stdafx.h 是一个预编译头文件,其中包含所有 DirectX 包含项。即在C:\Program Files (x86)\Windows Kits.1\Include\shared;C:\Program Files (x86)\Windows Kits.1\Include\um;

我还包含位于 C:\Program Files (x86)\Windows Kits.1\Lib\winv6.3\um\x64

中的 C:\Program Files (x86)\Windows Kits.1\Include\shared;C:\Program Files (x86)\Windows Kits.1\Include\um;

我正在使用 Visual Studio 2015,Windows 8.1 64 位。我按照 this 教程创建了 Directx 应用程序。只需创建一个 Win32 项目,在 include 和 libs 中完成这些修改,粘贴代码并正确构建它。 运行 但是不输出任何东西。它只是说构建成功。 VS 适用于我的所有其他项目。我已经在 x64 模式下尝试了所有配置。如果我不得不猜测我会说它没有找到 dll..我找不到罪魁祸首。

您在注册 window class 时将 "MainWindow" 指定为 class 名称,但在创建 window 时您指定了“"Main Window" ,因此 Windows 无法找到 class。将 "MainWindow" 作为 class 名称传递给 CreateWindow 将解决该问题:

window.lpszClassName = L"MainWindow";   // class name
window.style = CS_HREDRAW | CS_VREDRAW;

RegisterClassEx(&window);
HWND windowHandle = CreateWindow(L"MainWindow", L"DirectX Tut!", WS_OVERLAPPEDWINDOW,
    100, 100, 600, 800, NULL, NULL, hInstance, 0);

如上所示,您应该使用 L 作为宽字符串文字的前缀