We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,866 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Win32 Wrapper not doing anything with messages

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?

2
Contributors
2
Replies
17 Hours
Discussion Span
9 Months Ago
Last Updated
3
Views
Question
Answered
DeeperShade
Newbie Poster
19 posts since Jul 2012
Reputation Points: 4
Solved Threads: 1
Skill Endorsements: 0

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

nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 0

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

DeeperShade
Newbie Poster
19 posts since Jul 2012
Reputation Points: 4
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 9 Months Ago by nezachem

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1000 seconds using 2.67MB