-1

I was trying to make application using win32 and directX now, but I stuck at the first step of my project, which is making window. I tried many things to fix, but it keep saying, failed to create window which means hWnd is null.

This is my code. app.h

#include <windows.h>
#include <string>

class App
{
    public:
        App    (void);
        ~App   (void);

        HRESULT InitApp   (HINSTANCE hInstance, int nCmdShow);
        //void UpdateApp (void);

        HWND window(void) { return m_hWnd; }

    private:
        static LRESULT CALLBACK WndProc(HWND hwnd,
                                        UINT msg, 
                                        WPARAM wParam, 
                                        LPARAM lParam);
    private:
        HINSTANCE m_hInstance;
        HWND      m_hWnd;
        RECT      m_rect;
        static bool     s_running;

        std::string m_className;

}

app.cpp

#include "app.h"
// std::string, c.str()
#include <cstring>
#include <iostream>

bool App::s_running = true;

App::App(void)
{
}

App::~App(void)
{
} 

HRESULT App::InitApp(HINSTANCE hInstance, int nCmdShow)
{
    m_hInstance = hInstance;

    WNDCLASSEX wc       = {};
    wc.cbSize           = sizeof(WNDCLASSEX);
    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = App::WndProc;
    wc.hInstance        = m_hInstance;
    wc.lpszClassName    = "Hello" ;

    RECT rc = {0, 0, 800, 600};
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);

    int width  = rc.right - rc.left;
    int height = rc.bottom - rc.top;


    if(!RegisterClassEx(&wc))
    {
        OutputDebugString("Failed to register win class\n");
        return E_FAIL;
    }

    m_hWnd = CreateWindow("Hello", "Hello",
            WS_OVERLAPPED | WS_CAPTION | WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, width, height,
            nullptr, nullptr, m_hInstance,
            nullptr);

    if(!m_hWnd)
    {
        OutputDebugString("Failed to create window\n");
        return E_FAIL;
    }

    ShowWindow(m_hWnd, nCmdShow);

    return S_OK;
}


LRESULT CALLBACK App::WndProc(
        HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;

    switch(msg)
    {
        case WM_CREATE:
        {

        } break;

        case WM_PAINT:
        {
            hdc = BeginPaint(hwnd, &ps);
            EndPaint(hwnd, &ps);
        } break;

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

        default:
        {
            DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }

    return 0;
}

and usage in main.cpp

#include "app.h"
#include "graphics.h"

// test comment

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{  
    App app;
    Graphics graphics(app.window());

    if(FAILED(app.InitApp(hInstance, nCmdShow)));
        return 0;

    /*if(FAILED(graphics.InitGraphics()))
    {
        graphics.CleanUpDevice();
        return 0;
    }i*/

    MSG msg = {0};

    while(WM_QUIT != msg.message)
    {
        if(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            //graphics.Render();
        }
    }

    //graphics.CleanUpDevice();

    return 0;
}

I have researched many of things like this, but nothing worked. I think I missed something very little...but I couldn't find anything. Please help out this pity noob coder....

  • 1
    Which function failed? What `GetLastError` returned? – valdo Mar 10 '18 at 16:11
  • Check the value of ``GetLastError`` after ``CreateWindow`` fails – Asesh Mar 10 '18 at 16:12
  • 1
    @valdo: `GetLastError` probably returns 0 ("no error"). That's because the code requested to terminate window creation, by returning `0` in response to the [WM_NCCREATE](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632635.aspx) message. Window creation thus fails by request, not due to an error. – IInspectable Mar 10 '18 at 16:27
  • 1
    At any rate, this question has been asked and answered so many times before. Downvoted due to lack of research. – IInspectable Mar 10 '18 at 16:28

1 Answers1

3

WndProc does not return the value returned by DefWindowProc so when window receives WM_NCCREATE it will still return 0 indicating that window should not be created. Note that returning 0 from WM_CREATE is fine. Window proc should look like this:

LRESULT CALLBACK App::WndProc(
        HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    LRESULT result{};
    switch(msg)
    {
        case WM_CREATE:
        {
            result = 0;
        } break;
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);
            EndPaint(hwnd, &ps);
            result = 0;
        } break;
        case WM_DESTROY:
        {
            PostQuitMessage(0);   
            result = 0;
        } break;
        default:
        {
            result = DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }
    return result;
}
user7860670
  • 35,849
  • 4
  • 58
  • 84