Best method for storing this pointer for use in WndProc

前端 未结 10 820
南旧
南旧 2020-11-29 20:06

I\'m interested to know the best / common way of storing a this pointer for use in the WndProc. I know of several approaches, but each as I underst

10条回答
  •  长情又很酷
    2020-11-29 20:21

    In your constructor, call CreateWindowEx with "this" as the lpParam argument.

    Then, on WM_NCCREATE, call the following code:

    SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) ((CREATESTRUCT*)lParam)->lpCreateParams);
    SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
    

    Then, at the top of your window procedure you could do the following:

    MyWindowClass *wndptr = (MyWindowClass*) GetWindowLongPtr(hwnd, GWL_USERDATA);
    

    Which allows you to do this:

    wndptr->DoSomething();
    

    Of course, you could use the same technique to call something like your function above:

    wndptr->WndProc(msg, wparam, lparam);
    

    ... which can then use its "this" pointer as expected.

提交回复
热议问题