今天重写了一下3D程序的架构,基本上是参考DX9b那套构架写的,发现CreateWindow返回NULL,而且GetLastError()也返回0,一时很奇怪,后来上网查了一下,才发现是WndProc函数的问题,我代码如下:
if( m_hWnd == NULL)

{
// Register the windows class

WNDCLASS wndClass =
{ 0, WndProc, 0, 0, hInst,
LoadIcon( hInst, MAKEINTRESOURCE(IDI_MY3DGRAPHIC) ),
LoadCursor( NULL, IDC_ARROW ),
(HBRUSH)GetStockObject(WHITE_BRUSH),
NULL, m_szWindowClass };
RegisterClass( &wndClass );

// Set the window's initial style
m_dwWindowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME |
WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE;

// Set the window's initial width
RECT rc;
SetRect( &rc, 0, 0, m_dwCreationWidth, m_dwCreationHeight );
AdjustWindowRect( &rc, m_dwWindowStyle, true );

// Create the render window
m_hWnd = CreateWindow( m_szWindowClass,
m_strWindowTitle,
m_dwWindowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
(rc.right - rc.left),
(rc.bottom - rc.top),
0,
NULL, //LoadMenu( hInstance, MAKEINTRESOURCE(IDC_MY3DGRAPHIC) ),
hInstance,
0 );

WndProc代码如下:
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )


{
return g_sApp->MsgProc( hWnd, uMsg, wParam, lParam );
}
因为在WndProc中,g_sApp指针此时还为NULL,所以创建不成功。
调用CreateWindow时,CreateWindow会调用一次WndProc,并且要求WndProc返回TRUE,因此要注意该问题。