0
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 = CallWindowProc; << What should i put here
}

I am trying to register the window class by using object oriented method. I have no idea what should I put for the lpfnWndProc in the class cpp. For the header file, this is how it looks like

#pragma once
#ifndef BASE_H
#define BASE_H

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

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

#endif

Please correct me if I am doing anything wrong. Still learning. Sorry and thanks :)

12
  • 3
    lpfnWndProc must be global function (or static class function). The only data which can help to identify window instance is HWND hWnd parameter. So, you need some global map, which keeps (HWND, Base*) pairs. Using this map, you can call Base instance member from global lpfnWndProc. Commented Oct 30, 2014 at 9:45
  • 2
    I would strongly recommend against reinventing the wheel. If you absolutely must implement your own C++ wrapper around the Windows API, you could always check out the MFC implementation. You should also acknowledge that, if you're having difficulty understanding the difference between a function pointer and a member function pointer, you may not be ready to write framework code. Commented Oct 30, 2014 at 9:49
  • 1
    @IInspectable I am still studying on C++ and I dont see anything wrong of learning if I am not good at it. Thanks for the MFC suggestion though. Commented Oct 30, 2014 at 10:02
  • @Josh Lcs I don't think IInspectable was exactly recommending MFC. It's just that if you absolutely MUST use Microsoft's API to code GUIs in C++, then MFC is a better choice than the bare WIN32 API. It's still a poor choice, though. Better options would be wxWidgets or Qt. Both of these are cross-platform GUI toolkits that are much nice to use than either the WIN32 API or MFC. Commented Oct 30, 2014 at 10:10
  • @antred: I recommended reading the MFC implementation to get a feeling of what is required to get this going. Other than that, there really is nothing wrong with using the Windows API or MFC or WTL to write GUI code. While Qt makes it more comfortable to write GUI code, it generally produces extremely low quality GUIs, that are impossible to use without a pointing device. When it comes to maintaining GUI code, Windows API code is generally a lot more maintainable than Qt code. The ease of setting up signal-slot-connections comes at the expense of producing lots of invisible code. Commented Oct 30, 2014 at 11:21

1 Answer 1

2

Declare the following outside any class:

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

and define it elsewhere

WndCls.lpfnWndProc = WndProc; // address of your window procedure

WndProc is supposed to have a switch-case within to take care of the events or messages that you need to handle in the specific window class function ( a C function or a class static method)

Sign up to request clarification or add additional context in comments.

3 Comments

I doesn't necessarily have to be a C function, but it can't be a thiscall ( en.wikipedia.org/wiki/X86_calling_conventions#thiscall ). If the OP wants the lpfnWndProc to be a method, it has to be a static method, though.
Thanks for the quick reply. Do I need to add it to the main.cpp? No other way to add all the registering of window class into a single class?
-1 Demanding a free function, without even mentioning a static class member fails to address the OP's question. A free function necessarily needs to break data encapsulation, a required feature of object oriented programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.