c ++打开一个Window

时间:2013-12-05 16:35:40

标签: visual-studio-2010 visual-c++ user32

我是c ++编程的新手。当我编译这段代码时它没有给出任何错误,但是当它仍在运行时,我看不到任何Window.I将这个代码从一本书复制到我的visual c ++。我做错了什么。谢谢你的一切

// How to print text in Win32 C/C++
// Auther: -LeetGamer-

#include <Windows.h>
#include <WindowsX.h>

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX ps;
    HWND hWnd;

    ZeroMemory(&ps,sizeof(WNDCLASSEX));

    ps.cbSize=sizeof(WNDCLASSEX);
    ps.style=CS_HREDRAW | CS_VREDRAW ;
    ps.lpfnWndProc=(WNDPROC)WindowProc;
    ps.hInstance=hInstance;
    ps.hCursor=LoadCursor(NULL,IDC_ARROW);
    ps.hbrBackground=(HBRUSH)COLOR_WINDOW;
    ps.lpszClassName="asdasdasd";

    RegisterClassEx(&ps);

    hWnd=CreateWindowEx(NULL,"ASDASDDA","dsfsdfsdf",WS_OVERLAPPEDWINDOW,300,300,500,400,NULL,NULL,hInstance,NULL);

    ShowWindow(hWnd,nCmdShow);

    MSG mesaj;

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


    return mesaj.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
            break;

    }

    return DefWindowProc(hWnd,message,wParam,lParam);
}

1 个答案:

答案 0 :(得分:1)

RegisterClassExCreateWindowEx

中的班级名称必须相同

使用:

const char * pszClassName = "MyClassName";

[...]

ps.lpszClassName = pszClassName;

[...]

hWnd = CreateWindowEx( NULL, pszClassName, [...]

此外,养成检查API结果的习惯。在此,CreateWindowEx返回了NULL