以下是第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&gt; ------ Build build:Project:BlankWindow,Configuration:Debug Win32 ------ 1&gt; main.cpp 1&gt; c:\ coding \ c ++ \ visual c ++ \ directx \ blankwindow \ blankwindow \ main.cpp(10):错误C2065: 'WndProc':未声明的标识符1&gt; c:\ coding \ c ++ \ visual c ++ \ directx \ blankwindow \ blankwindow \ main.cpp(15):错误C2440:'=': 无法从'const char [20]'转换为'LPCWSTR'1&gt;类型 指出是无关的;转换需要reinterpret_cast, C风格的演员表或功能风格的演员表 ==========构建:0成功,1个失败,0个最新,0个跳过==========
基本上
'WndProc':未声明的标识符和'=':无法从'const转换 char [20]'到'LPCWSTR'
此代码有什么问题?
答案 0 :(得分:2)
以下是您的代码的基本工作程序:
#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;
}
点数:
我添加了一个WndProc
来处理发送到您程序的所有邮件。
我在您的计划中添加了message loop。否则,窗口会立即关闭。
我在另一个答案中添加了{_ 1}前缀,如the_mandrill所示。
这几乎是WinAPI编程的“Hello World”。但是,我强烈建议您在进入DirectX编程之前先学习WinAPI。
答案 1 :(得分:1)
问题是该项目正在构建为Unicode,但您的代码是非Unicode的。换句话说,Windows API调用期望宽(即16位)字符串,但您的代码使用8位字符串('char')。 LPCWSTR
表示指向常量宽字符串的长指针。因此函数调用期望一个恒定的宽字符串,但是你传递一个8位字符串。您有两种选择:
L
前加上L"DX11BookWindowClass"