Ok, basically i'm putting together a win32 wrapper based on various tutorials I've seen.
But when I try to close it via the close buttong (top right) or via the esacpe button it doesn't do anything, with the first it closes the window but doesn't end the program, with the latter it just doesn't acknowledge a thing.

So where am I going wrong?

// Header
#ifndef _WINCLUDE_H_
#define _WINCLUDE_H_

#include <stdint.h>
#include <Windows.h>

class winClude
{
public:

    int32_t winStart (HINSTANCE hInstance, int32_t nShowCmd);
    static LRESULT CALLBACK StaticWinProc(HWND hwnd, 
                                          UINT msg, 
                                          WPARAM wParam, 
                                          LPARAM lParam);

protected:
    LRESULT CALLBACK myWinProc(HWND hwnd,
                               UINT msg,
                               WPARAM wParam,
                               LPARAM lParam);

    inline static winClude *GetObjectFromWindow(HWND hwnd)
    {
        return (winClude *)GetWindowLong(hwnd, GWL_USERDATA);
    }
};
#endif


//CPP
#include <Windows.h>
#include <string>
#include <stdint.h>
#include "winClude.h"
using namespace std;



int32_t winClude::winStart (HINSTANCE hInstance, int32_t nShowCmd)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;

    int32_t screenWidth = 800;
    int32_t screenHeight = 600;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = winClude::StaticWinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "myWinClass";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        return 0;
    }

    int32_t winLeft;
    int32_t winTop;

    winLeft = (GetSystemMetrics(SM_CXSCREEN) / 2) - (screenWidth / 2);
    winTop = (GetSystemMetrics(SM_CYSCREEN) / 2) - (screenHeight / 2);

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                          "myWinClass",
                          "Window",
                          WS_OVERLAPPEDWINDOW,
                          winLeft, winTop, screenWidth, screenHeight, NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);

    while(true)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {

        }
    }

    return msg.wParam;
}


LRESULT CALLBACK winClude::StaticWinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    winClude *win = 0;

    if ( msg == WM_NCCREATE )
    {
        SetWindowLong(hwnd, GWL_USERDATA, (long)((LPCREATESTRUCT(lParam))->lpCreateParams));
    }

    win = GetObjectFromWindow(hwnd);

    if ( win )
        return win->myWinProc(hwnd, msg, wParam, lParam);
    else
        return DefWindowProc(hwnd, msg, wParam, lParam);
}


LRESULT CALLBACK winClude::myWinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch ( msg )
    {
        case WM_KEYDOWN:
        {
            switch(WM_KEYDOWN)
            {
                case VK_ESCAPE:
                {
                    PostQuitMessage( 0 );
                }
            }
        }
        case WM_CLOSE:
        {
            DestroyWindow( hwnd );
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage( 0 );
            break;
        }
        default:
            return DefWindowProc( hwnd, msg, wParam, lParam );
    }
    return 0;
}

Any advice?

Recommended Answers

All 2 Replies

Line 31 is sort of funny. You need to switch on wParam instead.

Well spotted, thanks but that doesn't actually solve the problem, the program still isn't doing anything =/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.