1

这是它在第 33 页给我的代码:

#include<Windows.h>

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
       LPWSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER( prevInstance );
    UNREFERENCED_PARAMETER( cmdLine );
    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof( WNDCLASSEX ) ;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wndClass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";
    if( !RegisterClassEx( &wndClass ) )
    return -1;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    HWND hwnd = CreateWindowA( "DX11BookWindowClass", "Blank Win32 Window",
    WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.
    left,
    rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
    if( !hwnd )
    return -1;
    ShowWindow( hwnd, cmdShow );

    return 0;
}

这段代码给了我2个错误-

1>----- 构建开始:项目:BlankWindow,配置:Debug Win32 ------ 1> main.cpp 1>c:\coding\c++\visual c++\directx\blankwindow\blankwindow\main. cpp(10): 错误 C2065: 'WndProc' : 未声明的标识符 1>c:\coding\c++\visual c++\directx\blankwindow\blankwindow\main.cpp(15): 错误 C2440: '=' : 无法从 ' 转换const char [20]' to 'LPCWSTR' 1> 指向的类型不相关;转换需要 reinterpret_cast、C 样式转换或函数样式转换 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

基本上

“WndProc”:未声明的标识符和“=”:无法从“const char [20]”转换为“LPCWSTR”

这段代码有什么问题?

4

2 回答 2

2

Here is a basic working program of your code:

#include<Windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
            {
                    case WM_CLOSE:
                            DestroyWindow(hwnd);
                            break;
                    case WM_DESTROY:
                            PostQuitMessage(0);
                            break;
                    default:
                            return DefWindowProc(hwnd,
                                                 message,
                                                 wParam,
                                                 lParam);
            }
    return 0;
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
       LPSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER( prevInstance );
    UNREFERENCED_PARAMETER( cmdLine );
    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof( WNDCLASSEX ) ;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wndClass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";
    if( !RegisterClassEx( &wndClass ) )
    return -1;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    HWND hwnd = CreateWindowA( L"DX11BookWindowClass", L"Blank Win32 Window",
    WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.
    left,
    rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
    if( !hwnd )
    return -1;
    ShowWindow( hwnd, cmdShow );
    MSG msg;
    while (GetMessage (&msg, 0, 0, 0))
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
    return msg.wParam;
}

Points:

  1. I added a WndProc which handles all messages sent to your program.

  2. I added a message loop to your program. Otherwise, the window would close right away.

  3. I added the L prefix as suggested by the_mandrill in the other answer.

This is pretty much your "Hello World" of WinAPI programming. However, I strongly suggest you learn the WinAPI first before jumping into DirectX programming.

于 2012-07-30T23:34:27.107 回答
1

问题是该项目正在构建为Unicode,但您的代码是非 Unicode。换句话说,Windows API 调用需要宽(即 16 位)字符串,但您的代码使用 8 位字符串('char')。 LPCWSTR表示指向常量宽字符串的长指针。所以函数调用需要一个恒定的宽字符串,但您传递的是一个 8 位字符串。你有两个选择:

  • 更改项目以使用非 Unicode 库(属性 -> 常规 -> 字符集 = '使用多字节')
  • 修复您的代码以使其成为 Unicode。在这种情况下,通过更改字符串,它会抱怨将它们变成宽字符串。您可以通过在它们前面加上前缀来做到这一点L,例如L"DX11BookWindowClass"
于 2012-07-30T22:18:14.453 回答