Q:

Can windows proc/callback function be a member function of a class?

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?

A:

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;
}

