Win32 C++ Create a Window and Procedure Within a Class

后端 未结 5 1790
刺人心
刺人心 2020-12-09 11:16

Pre-Text/ Question

I am trying to make a fairly simple tool to help debug variable values. For it to be completely self contained within the class i

5条回答
  •  执念已碎
    2020-12-09 11:35

    Your window procedure is called during CreateWindow. You pass hWindow to DefWindowProc, but hWindow is only set after CreateWindow returns - so you pass DefWindowProc a garbage window handle.

    I don't see a nice way to do it. You could set hWindow inside the window procedure, by changing WindowProc to:

    LRESULT WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    

    (added the hwnd parameter), changing the call to:

    return view->WindowProc(hwnd, message, wParam, lParam);
    

    creating the window like this:

    hWindow = NULL;
    hWindow = CreateWindow(..., hInst, this);
    if (hWindow == NULL)
        return -1;
    

    (the first assignment is to make sure hWindow is initialized; the second one is in case CreateWindow fails after calling the window procedure), and adding this at the start of WindowProc:

    if(!this->hWindow)
        this->hWindow = hwnd;
    

提交回复
热议问题