为什么这个指针(WndProc里面的游戏)是NULL?

时间:2013-03-18 21:41:37

标签: c++ windows oop winapi pointers

这是我的WinMain:

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
    if(SUCCEEDED(CoInitialize(NULL)))
    {
        {
            Game game;

            game.CreateRessources(hInst);

            game.ShowMainScreen();

            HWND hwnd=game.Getm_hWnd();
            HWND* pHWND=&hwnd;

            game.pWinsock->Initialize(hwnd);

            ShowWindow(game.Getm_hWnd(),SW_MAXIMIZE);
            UpdateWindow(game.Getm_hWnd());

            MSG msg;
            ZeroMemory(&msg,sizeof(MSG));

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

    return 0;
}

我的窗口创建类:

void Game::CreateWindowClass(HINSTANCE hInst)
{
    // Create a window class
    WNDCLASSEX wClass;
    ZeroMemory(&wClass,sizeof(WNDCLASSEX));
    wClass.cbClsExtra=NULL;
    wClass.cbSize=sizeof(WNDCLASSEX);
    wClass.cbWndExtra=NULL;
    wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
    wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wClass.hIcon=NULL;
    wClass.hIconSm=NULL;
    wClass.hInstance=hInst;
    wClass.lpfnWndProc=WinProc;
    wClass.lpszClassName="Window Class";
    wClass.lpszMenuName=NULL;
    wClass.style=CS_HREDRAW|CS_VREDRAW;

    if(!RegisterClassEx(&wClass))
    {
        int nResult=GetLastError();

        MessageBox(NULL,"Failed to register window class","Window Class Failed",MB_ICONERROR);
    }

    m_hWnd=CreateWindowEx(NULL,
            "Window Class",
            "Game",
            WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            NULL,
            NULL,
            hInst,
            this);

    if(!m_hWnd)
    {
        int nResult=GetLastError();

        MessageBox(NULL,"Window class creation failed","Window Class Failed",MB_ICONERROR);
    }
}

这是Game :: CreateRessources:

void Game::CreateRessources(HINSTANCE hInst)
{   
    CreateWindowClass(hInst);

    pD2DResources=CreateD2DResources(m_hWnd);
    pMessageLog=CreateMessageLog();
    pWinsock=CreateWinsock();
}

最后,我的WinProc略微在行之后:if(pGame == NULL)             MessageBox(NULL,"指针pGame为NULL","错误",NULL);

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if(msg==WM_CREATE)
    {
        LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
            Game* pGame = (Game*)pcs->lpCreateParams;

            ::SetWindowLongPtrW(
                hWnd,
                GWLP_USERDATA,
                PtrToUlong(pGame)
                );
    }

    else if(msg==WM_DESTROY)
    {
        PostQuitMessage(0);
        return 0;
    }

    else
    {
        Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(
            ::GetWindowLongPtrW(
                hWnd,
                GWLP_USERDATA
                )));

        if(pGame==NULL)
            MessageBox(NULL,"Pointer pGame is NULL","Error",NULL);

        if(pGame->pD2DResources!=NULL)
        {
            switch(msg)
            {

出于某种原因,自从MessageBox弹出以来,pGame为NULL。为什么这样?

编辑:顺便说一下,我动态创建了一些对象,我的游戏对象中的私有指针被分配给这些动态创建的对象。它可能与问题有关吗?

1 个答案:

答案 0 :(得分:4)

因为WM_CREATE不是第一条消息,所以窗口过程得到了。请参阅WM_NCCREATE