Jump to content
  • Advertisement
Sign in to follow this  
matt77hias

Windows callbacks as class members

This topic is 487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!