#include <Windows.h>
#include <stdio.h>LRESULT CALLBACK WinSunProc(
HWND hwnd,       /*窗口句柄*/
UINT uMsg,       /*信息标识*/
WPARAM wParam,
LPARAM lParam
);int WINAPI WinMain(
HINSTANCE hInstance,       /*当前实例句柄*/
HINSTANCE hPrevInstance,   /*上一个实例句柄*/
LPSTR lpCmdLine,           /*命令行*/
int nCmdShow               /*显示状态*/
)
{
/*设计一个窗口类*/
WNDCLASS wndcle;
wndcle.cbClsExtra = 0;
wndcle.cbWndExtra = 0;
wndcle.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcle.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcle.hIcon = LoadIcon(NULL, IDI_ERROR);
wndcle.hInstance = hInstance;
wndcle.lpfnWndProc = WinSunProc;
wndcle.lpszClassName = TEXT("lpl2018");
wndcle.lpszMenuName = NULL;
wndcle.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcle);
char pszErrMsg[128] = { 0 }; /*创建窗口,定义一个变量用来保存成功创建窗口后返回的句柄*/
HWND hwnd;
hwnd = CreateWindow(TEXT("LCK2018"), TEXT("http://www.baidu.com"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
snprintf(pszErrMsg,sizeof(pszErrMsg),TEXT("err[%d]"), GetLastError());
OutputDebugString(pszErrMsg);
MessageBox(NULL, "CreateWindow Err", "Error", 0);
}

/*显示及刷新窗口*/
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd); /*定义消息结构体,开始消息循环*/
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;}/*编写窗口过程函数*/
LRESULT CALLBACK WinSunProc(
HWND hwnd,       /*窗口句柄*/
UINT uMsg,       /*信息标识*/
WPARAM wParam,
LPARAM lParam
)
{ switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, TEXT("char"), 0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked", "message", 0);
HDC hdc;
hdc = GetDC(hwnd); /*不能再相应WM_PAINT 消息时调用*/
TextOut(hdc, 0, 50, TEXT("LOL HOME"), strlen("LOL HOME"));
/*ReleaseDC(hwnd,hdc);*/
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc = BeginPaint(hwnd, &ps); /* BeginPaint 只能在响应 WM_PAINT 消息时调用*/
TextOut(hDc, 0, 0, "http://www.csdn.com", strlen("http://www.csdn.com"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBox(hwnd, "Is End?", "message", MB_YESNO))
        {
DestroyWindow(hwnd);
        }
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
} return 0;
}CreateWindow返回NULL, GetLastError 是1407.  是我类填漏了还是填错了?

解决方案 »

  1.   

    wndcle.lpszClassName = TEXT("lpl2018");
    hwnd = CreateWindow(TEXT("LCK2018"), TEXT("http://www.baidu.com"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
    你注册的类名 要和创建的类名一致
      

  2.   

    "lpl2018" 注册名
    "LCK2018" 创建名 
    不一致 !
      

  3.   

    1047错误码对应的错误信息是:  "找不到窗口类。"所以 1楼2楼说的, 就是你的问题所在. MSDN中, 也有详细描述
    HWND CreateWindow(          LPCTSTR lpClassName,
        LPCTSTR lpWindowName,
        DWORD dwStyle,
        int x,
        int y,
        int nWidth,
        int nHeight,
        HWND hWndParent,
        HMENU hMenu,
        HINSTANCE hInstance,
        LPVOID lpParam
    );
    ParameterslpClassName
    [in] Pointer to a null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. If lpClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names. For a list of system class names, see the Res section. 
      

  4.   

    hwnd = CreateWindow(TEXT("LCK2018"),创建窗口的类名和注册的 窗口类类名不一样,你创建的窗口应该是返回失败,hWnd应该是0