大侠看一看。C++调用API建窗口的出现异常。

大侠看一看。。。C++调用API建窗口的出现错误。。。
vs没有报错。。。也没有警告。。。但是调试之后一直没有窗口出现。。。
在重填完wndclass结构之后我设置了断点之后,才看到很多wndclass结构中的变量错误:CXX0030:错误:无法计算表达式的值。。。

源码:

#include <Windows.h>
#include <iostream>

struct DB
{
DB ()
{
this->cxChar = 0;
this->cyChar = 0;
this->posVscroll = 0;
}

int cxChar;
int cyChar;
int posVscroll;
};


//系统数据
DB db;


LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

//WinMain主函数
int WINAPI WinMain (
HINSTANCE hInstance = 0,
HINSTANCE hPreInstance = 0,
PSTR szCmdLine = 0,
int iCmdShow = 0)
{
HWND hwnd;
MSG msg;
WNDCLASS wnd;

wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.lpfnWndProc = WndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wnd.hCursor = LoadCursor (NULL, IDC_ARROW);
wnd.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
wnd.lpszMenuName = NULL;
wnd.lpszClassName = L"窗口测试";

RegisterClass (&wnd);

hwnd = CreateWindow (
wnd.lpszClassName,
L"窗口测试",
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);

ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);

while (GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}

return msg.wParam;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;

switch (msg)
{
case WM_CREATE:
{
hdc = GetDC (hwnd);
TEXTMETRIC tm;
GetTextMetrics (hdc, &tm);
db.cxChar = tm.tmAveCharWidth;
db.cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC (hwnd, hdc);
SetScrollRange (hwnd, SB_VERT, 0, 4, 0);
SetScrollPos (hwnd, SB_VERT, 0, 0);
return 0;
}

case WM_PAINT:
{
hdc = BeginPaint (hwnd, &ps);
TextOut (hdc, 0, 0, L"檀锦 制作", 5);
TextOut (hdc, 500, 500, L"澜意工作室 出品", 8);
EndPaint (hwnd, &ps);
return 0;
}

case WM_DESTROY:
{
PostQuitMessage (0);
return 0;
}

//队列消息处理
case WM_CLOSE:
{
if (IDYES==MessageBox(hwnd, L"确定要退出?", L"退出", MB_YESNO))
{
DestroyWindow(hwnd);
break;
}
}

//处理鼠标左键
case WM_LBUTTONDOWN:
{
MessageBox(hwnd, L"鼠标左键按键", L"澜意提示", MB_OK);
break;
}

//处理滚动条
case WM_VSCROLL:
{
switch (LOWORD(wparam))
{
case SB_LINEUP:
db.posVscroll-=1;
break;
case SB_LINEDOWN:
db.posVscroll-=1;
break;

}
}

//默认处理
default:
DefWindowProc (hwnd, msg, wparam, lparam);
}

return 0;
}

大侠们帮忙看一看啦。。。。。。

------解决方案--------------------
我运行了下,调试进去发现你的CreateWindow返回句柄为空.
检查你的消息循环switch(msg)——case语句出了问题
应该把最后几行默认处理的代码改成
//默认处理
//default:
//DefWindowProc (hwnd, msg, wparam, lparam);
}
return DefWindowProc (hwnd, msg, wparam, lparam) ;
//return 0;
}
仔细想想C语言的这个选择条件运行结构吧。。。