Pass user data at window creation

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Pass user data at window creation

    Hello,

    I would like to encapsulate a view that contains a window and some widget in a class. So I started with something like below :

    C Source Code

    1. /// --- view.hpp
    2. class View
    3. {
    4. public:
    5. void create();
    6. private:
    7. static void internal_callback(WM_MESSAGE* pMsg);
    8. void callback(WM_MESSAGE * pMsg);
    9. WM_HWIN win_;
    10. };
    11. /// --- view.cpp
    12. void View::create()
    13. {
    14. win_ = WM_CreateWindowAsChild(0, 0, 240, 160, WM_HBKWIN, WM_CF_SHOW, View::internal_callback,
    15. sizeof(this));
    16. WM_SetUserData(win_, this, sizeof(this));
    17. }
    18. void View::internal_callback(WM_MESSAGE* pMsg)
    19. {
    20. /// I need to test this because SetUserData is call after creation
    21. /// So it avoids NULL pointer
    22. if (pMsg->MsgId == WM_CREATE)
    23. {
    24. /// Use static members...
    25. }
    26. else
    27. {
    28. void* view = NULL;
    29. WM_GetUserData(pMsg->hWin, view, 4); /// I can't call sizeof(this) but I know it is 4
    30. if (view != NULL) /// !!! view is always NULL
    31. {
    32. static_cast<View*>(view)->callback(pMsg);
    33. }
    34. }
    35. }
    36. void View::callback(WM_MESSAGE * pMsg)
    37. {
    38. switch (pMsg->MsgId)
    39. {
    40. case WM_PAINT:
    41. {
    42. GUI_SetBkColor(GUI_WHITE);
    43. GUI_Clear();
    44. GUI_SetColor(GUI_BLACK);
    45. GUI_DispString("Hello World");
    46. break;
    47. }
    48. default:
    49. WM_DefaultProc(pMsg);
    50. break;
    51. }
    52. }
    Display All


    So I have two questions:

    - Does anyone knows why view is always equal to NULL after a call to WM_GetUserData() to retrieve a pointer to "this" ?

    - Is there a way to pass a user_data at construction and not after with a setter to avoid to check for WM_CREATE in internal_callback() ?

    Thanks a lot,
    Regards,

    Nicolas