Josh Lcs
Josh Lcs

Reputation: 55

Creating a window in OO

Here is my header

#pragma once
#ifndef BASE_H
#define BASE_H

#include <Windows.h>
#include <windowsx.h>

class Base
{
    HWND hWnd;
    WNDCLASSEX WndCls;
    HRESULT Hr;
public:
    Base();
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    void RegisterWnd(HINSTANCE hInstance);
    void ShowWnd(int nCmdShow);
    ~Base();
};

#endif

Here is my base.cpp

#include "Base.h"


Base::Base()
{
}

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch (message)
    {
        // this message is read when the window is closed
    case WM_DESTROY:
    {
        // close the application entirely
        PostQuitMessage(0);
        return 0;
    } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc(hWnd, message, wParam, lParam);
}

void Base::RegisterWnd(HINSTANCE hInstance)
{
    ZeroMemory(&WndCls, sizeof(WNDCLASSEX));
    WndCls.cbSize = sizeof(WNDCLASSEX);
    WndCls.hbrBackground = (HBRUSH)COLOR_WINDOW;
    WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndCls.hIcon = LoadIcon(hInstance, NULL);
    WndCls.hIconSm = LoadIcon(hInstance, NULL);
    WndCls.hInstance = hInstance;
    WndCls.lpfnWndProc = WndProc;
    WndCls.lpszClassName = "ClsName";
    WndCls.style = CS_HREDRAW | CS_VREDRAW;

    Hr = RegisterClassEx(&WndCls);
    if (FAILED(Hr))
        MessageBox(NULL, "Window Class failed to register.", "ERROR", MB_OK);

    hWnd = CreateWindowEx(
        NULL,
        "WndClassName",
        "WndName",
        WS_OVERLAPPEDWINDOW,
        100, 100,
        480, 640,
        NULL,
        NULL,
        hInstance,
        NULL);
    if (FAILED(hWnd))
        MessageBox(NULL, "Window Class failed to create", "ERROR", MB_OK);
}

void Base::ShowWnd(int nCmdShow)
{
    Hr = ShowWindow(hWnd, nCmdShow);
    if (FAILED(Hr))
        MessageBox(NULL, "Failed to display Window", "ERROR", MB_OK);
}

Base::~Base()
{
}

And here is my main.cpp

#include "Base.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    Base CreateWnd;

    CreateWnd.RegisterWnd(hInstance);
    CreateWnd.ShowWnd(nCmdShow);

    MSG Msg;
    while (GetMessage(&Msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&Msg);

        // send the message to the WindowProc function
        DispatchMessage(&Msg);
    }

    // return this part of the WM_QUIT message to Windows
    return Msg.wParam;
}

The problem is, I keep getting this error message that I dont understand of. Sorry for the bad explanation..Still a student in programming... Here is the image of the error

UPDATED : The error above has been corrected by replacing

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

with

LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

Thanks to ravi and IInspectable for the quick help.

Now I am having another error D: When i clicked on debug, everything run perfectly but nothing shows up. No window is showing. Visual studio is running perfectly as "Ready". (Sorry i do not want to make another new question because it's still related to creating window in oo

SECOND UPDATE : My class name in CreateWindowEx is different from the RegisterWnd..My bad. Thanks to IInspectable again for the help.

Upvotes: 1

Views: 81

Answers (1)

ravi
ravi

Reputation: 10733

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

You have to define this with class scope OR how compiler know if its global static OR static member of class. So it should be

LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

Upvotes: 2

Related Questions