Jump to content
  • Advertisement
Sign in to follow this  
matt77hias

Windows callbacks as class members

Recommended Posts

I try to get rid of some Windows callbacks and variables in global scope. The Windows callbacks can be made class member methods (i.e. static), extracting the right instance and invoking the callback of that instance:

if (WM_NCCREATE == uMsg) {
            const LONG_PTR replacement_value = (LONG_PTR)((CREATESTRUCT *)lParam)->lpCreateParams;
            SetWindowLongPtr(hwndDlg, GWLP_USERDATA, replacement_value);
            return TRUE;
}

((DeviceEnumeration *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA))->SettingsDialogProc(hwndDlg, uMsg, wParam, lParam);

This works by passing "this" as the "lpParam" in "CreateWindow".

Is a similar approach possible for "DialogBox"?

 

Edited by matt77hias

Share this post


Link to post
Share on other sites
Advertisement

I found a solution: you can use DialogBoxParam method/macro instead of DialogBox method/macro. The DialogBox wraps DialogBoxParam but does not expose the dwInitParam, which you can use for passing data in the LPARAM for a WM_INITDIALOG message.

Here, I just pass this as actual parameter for the formal parameter dwInitParam of DialogBoxParam:

const INT_PTR result_dialog = DialogBoxParam(nullptr, MAKEINTRESOURCE(IDD_GRAPHICS_SETTINGS), 
            nullptr, SettingsDialogProcDelegate, reinterpret_cast< LPARAM >(this));

Next, the class member Window callback is defined as:

INT_PTR CALLBACK DeviceEnumeration::SettingsDialogProcDelegate(
        HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {

        DeviceEnumeration *device_enumeration = nullptr;

        if (WM_INITDIALOG == uMsg) {
            device_enumeration = reinterpret_cast< DeviceEnumeration * >(lParam);

            const LONG_PTR replacement_value = reinterpret_cast< LONG_PTR >(device_enumeration);
            // Changes an attribute of the specified window.
            // 1. A handle to the window and, indirectly, the class to which the window belongs.
            // 2. Sets the user data associated with the window.
            // 3. The replacement value.
            SetWindowLongPtr(hwndDlg, GWLP_USERDATA, replacement_value);
        }
        else {
            // Retrieves information about the specified window.
            // 1. A handle to the window and, indirectly, the class to which the window belongs.
            // 2. Retrieves the user data associated with the window.
            device_enumeration = reinterpret_cast< DeviceEnumeration * >(GetWindowLongPtr(hwndDlg, GWLP_USERDATA));
        }

        return device_enumeration->SettingsDialogProc(hwndDlg, uMsg, wParam, lParam);
    }

 

Edited by matt77hias

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!