1

When you for example create a window on the screen in windows, you create a callback function to receive message from windows

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

Can you make this function a member function of a class? If so, how do I do it? The reason I would like the callback function to be a member of a class is to have acces to certain variables, while still having the variables incapsulated. For instance where I save the width and height of the window, so if they ever where to be changed I can changed them in the callback function directly.

If not, do you have any suggestions on how I would go about solving this problem, giving the callback function acces to incapsulated variables?

3
  • 2
    It can be a static member function - not that it helps much. There are many different tricks to associate a class instance with an HWND handle - see e.g. SetProp or SetWindowLongPtr Jan 28, 2015 at 15:12
  • static member functions can be used as callback functions without too much effort. Making non-static member function work as callback functions requires creating wrapper functions.
    – R Sahu
    Jan 28, 2015 at 15:13
  • hmm okay. Well its mainly getting acces to width and height changes im after, without having to do some ugly work around using global variables. I want to create a compact and incapsulated window class Jan 28, 2015 at 15:18

1 Answer 1

3

Just like the comments suggest a combination of static member functions and SetWindowLongPtr can be used to achieve what you are looking to do.

Here is a sample class declaration that defines the static WindowProc as a member function.

class MyClass
{
public:
    void MainThreadProc ();

private:
    static LRESULT CALLBACK WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    LRESULT _WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};

The MainThreadProc method will create the window. Make sure to pass in this to CreateWindowEx as that is how you will get access to your class members.

void MyClass::MainThreadProc()
{
    MSG msg;    
    HINSTANCE hinstance = GetModuleHandle (NULL);    
    WNDCLASSEX wcx = {0};
    HWND hwnd = NULL;

    wcx.cbSize = sizeof wcx;
    wcx.hInstance = hinstance;
    wcx.hCursor = LoadCursor (NULL, IDC_NO);
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    wcx.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wcx.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wcx.lpfnWndProc = WindowProc;
    wcx.lpszClassName = L"MyWindowClass";

    RegisterClassEx (&wcx);

    hwnd = CreateWindowEx ( WS_EX_APPWINDOW,
                            L"MyWindowClass",
                            L"MyWindow",
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hinstance,
                            this);

    ShowWindow (hwnd, SW_SHOWDEFAULT);    

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

In WindowProc you will store the this object using SetWindowLongPtr for future use.

LRESULT MyClass::WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if (WM_NCCREATE == uMsg)
    {
        SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) ((CREATESTRUCT*) lParam)->lpCreateParams);
        return TRUE;
    }

    return ((MyClass*) GetWindowLongPtr (hwnd, GWLP_USERDATA))->_WindowProc (hwnd, uMsg, wParam, lParam);
}

And for completeness sake, here is the final member function for processing window messages and you will have access to all class members at this point.

LRESULT MyClass::_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {   
    case WM_DESTROY:
        PostQuitMessage (S_OK);
        break;
    default:
        return DefWindowProc (hwnd, uMsg, wParam, lParam);
    }

    return 0;
}
2
  • Does this also work for the callback of DialogBox for instance?
    – Matthias
    Aug 9, 2017 at 9:39
  • It works with DialogBoxParam instead of DialogBox (See: gamedev.net/forums/topic/…)
    – Matthias
    Aug 9, 2017 at 11:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.