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

后端 未结 5 1786
刺人心
刺人心 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;
    
    0 讨论(0)
  • 2020-12-09 11:39

    To use a non-static class method as a window procedure requires a dynamically-allocated thunk, which is an advanced technique that I will not get into it here.

    The alternative is to declare the class method as static, then it will work as a window procedure. Of course, being a static method, it can no longer access non-static class members without an instance pointer. To get that pointer, you can have the class pass its this pointer to the lpParam parameter of CreateWindow/Ex(), then the window procedure can extract that pointer from the WM_NCCREATE message and store it in the window using SetWindowLong/Ptr(GWL_USERDATA). After that, subsequent messages can retrieve that pointer using GetWindowLong/Ptr(GWL_USERDATA) and thus be able to access non-static members of that object. For example:

    class viewvars
    {
    private:
        HWND hWindow;
        LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
    
        static LRESULT CALLBACK ThisWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    public:
        int CreateTestWindow(HINSTANCE hInst);
    };
    

    int viewvars::CreateTestWindow(HINSTANCE hInst)
    { 
        WNDCLASS wincl;
    
        if (!GetClassInfo(hInst, thisClassName, &wincl))
        {
            ...
            wincl.hInstance = hInst;
            wincl.lpszClassName = thisClassName;
            wincl.lpfnWndProc = &ThisWindowProc;
    
            if (RegisterClass(&wincl) == 0)
                return -1;
        }
    
        hWindow = CreateWindow(..., hInst, this);
        if (hWindow == NULL)
            return -1;
    
        ...
    
        MSG msg;
        while (GetMessage(&msg, hWindow, 0, 0))
        {
            TranslateMessage(&msg);  
            DispatchMessage(&msg); 
        }
    
        DestroyWindow(hWindow);
        hWindow = NULL;
    
        return msg.wParam;
    }
    
    LRESULT CALLBACK viewvars::ThisWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        viewvars *view;
    
        if (message == WM_NCCREATE)
        {
            CREATESTRUCT *cs = (CREATESTRUCT*) lParam; 
            view = (viewvars*) cs->lpCreateParams;
    
            SetLastError(0);
            if (SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR) view) == 0)
            {
                if (GetLastError() != 0)
                    return FALSE;
            }
        }
        else
        {
            view = (viewvars*) GetWindowLongPtr(hwnd, GWL_USERDATA);
        }
    
        if (view)
            return view->WindowProc(message, wParam, lParam);
    
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    LRESULT viewvars::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
        // you can access non-static members in here...
    
        switch (message)
        {
            case WM_PAINT: 
                return 0;
    
            case WM_DESTROY:
                PostQuitMessage(0);       
                return 0;
    
            default:
                return DefWindowProc(hWindow, message, wParam, lParam);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 11:54

    Unfortunately using an instance method as a C-style callback function for the WndProc won't work. At least not in any straight-forward way.

    The reason it doesn't work like that is that an instance method requires the this pointer to be passed in (to point to an instance) and that won't be correctly set by the code calling the WndProc. The Win32 API was originally designed with C in mind so this is one area where you have to use some work-arounds.

    One way to work around this would be to create a static method to serve as the window proc and dispatch messages to your class instances. The class instances would have to be registered (read added to a static collection) so the static method would know to dispatch WndProc messages to the instances. Instances would register themselves with the static dispatcher in the constructor and remove themselves in the destructor.

    Of course all the registration and unregistration and dispatching overhead is only necessary if your WndProc handler needs to invoke other instance member functions, or access member variables. Otherwise you can just make it static and you're done.

    0 讨论(0)
  • 2020-12-09 11:55

    Step through the code in the debugger. When you get to the line

        MessageBox(NULL,"DefWindowProc Returned.","Error",0);
        return DefWindowProc(hWindow, message, wParam, lParam);
    

    You will see something wrong: hWindow is garbage. You are using an uninitialized variable.

    0 讨论(0)
  • 2020-12-09 11:56

    The main message loop must not be in your class, and especially not in a "CreateTestWindow" function, as you will not return from that function until your thread receive the WM_QUIT message that makes GetMessage returns 0.

    Here is simple implementation of your viewvars class. Key points:

    • The Window Proc is a static member.
    • The link between the Window Proc and the object is made through the use of GWLP_USERDATA. See SetWindowLongPtr.
    • The class DTOR destroys the window if it still exists. The WM_DESTROY message set the HWND member to 0.
    • Adding OnMsgXXX methods to the class is simple: declare/define then and just call them from the WindowProc using the 'this' pointer stored in GWLP_USERDATA.

    EDIT:

    • As per Mr Chen suggestion, earlier binding of the HWND to the Object (in WM_NCCREATE) to allow message handler as methods during the Window Creation.

    I changed the creation styles, to show the window and to be able to move it.

    // VIEWVARS.H
    class viewvars {
    
    public:
        static viewvars* CreateTestWindow( HINSTANCE hInstance );
        viewvars() : m_hWnd( 0 ) {}
        ~viewvars();
    
    private:
        static LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
        static const char * m_pszClassName;
        HWND m_hWnd;
    
    };
    
    // VIEWVARS.CPP
    #include "viewvars.h"
    
    const char * viewvars::m_pszClassName = "viewvars";
    
    viewvars * viewvars::CreateTestWindow( HINSTANCE hInst ) {
    
        WNDCLASS wincl;
        if (!GetClassInfo(hInst, m_pszClassName, &wincl)) {
            wincl.style = 0;
            wincl.hInstance = hInst;
            wincl.lpszClassName = m_pszClassName;
            wincl.lpfnWndProc = WindowProc;
            wincl.cbClsExtra = 0;
            wincl.cbWndExtra = 0;
            wincl.hIcon = NULL;
            wincl.hCursor = NULL;
            wincl.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
            wincl.lpszMenuName = NULL;
            if (RegisterClass(&wincl) == 0) {
                MessageBox(NULL,"The window class failed to register.","Error",0);
                return 0;
            }
        }
    
        viewvars * pviewvars = new viewvars;
        HWND hWnd = CreateWindow( m_pszClassName, "Test", WS_VISIBLE | WS_OVERLAPPED, 50, 50, 200, 200, NULL, NULL, hInst, pviewvars );
        if ( hWnd == NULL ) {
            delete pviewvars;
            MessageBox(NULL,"Problem creating the window.","Error",0); 
            return 0; 
        }
    
        return pviewvars;
    
    }
    
     LRESULT CALLBACK viewvars::WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
    
        switch ( uMsg ) {
    
            case WM_NCCREATE: {
                CREATESTRUCT * pcs = (CREATESTRUCT*)lParam;
                viewvars * pviewvars = (viewvars*)pcs->lpCreateParams;
                pviewvars->m_hWnd = hwnd;
                SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG)pcs->lpCreateParams );
                return TRUE;
            }
    
            case WM_DESTROY: {
                viewvars * pviewvars = (viewvars *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
                if ( pviewvars ) pviewvars->m_hWnd = 0;
                break;
            }
    
            default:
                return DefWindowProc( hwnd, uMsg, wParam, lParam );
    
        }
    
        return 0;
    
    }
    
    viewvars::~viewvars() {
        if ( m_hWnd ) DestroyWindow( m_hWnd );
    }
    

    Finally, a "main" sample, but beware that there is here no way to end the process. That should be taken care by regular code (another windows).

    // MAIN.CPP
    #include <Windows.h>
    #include "viewvars.h"
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        viewvars * pviewvars = viewvars::CreateTestWindow( hInstance );
        if ( pviewvars == 0 ) return 0;
    
        BOOL bRet;
        MSG msg;
        while( (bRet = GetMessage( &msg, 0, 0, 0 )) != 0)
        { 
            if (bRet == -1)
            {
                // handle the error and possibly exit
            }
            else
            {
                TranslateMessage(&msg); 
                DispatchMessage(&msg); 
            }
        }
    
        delete pviewvars;
    
        return 0;
    
    }
    
    0 讨论(0)
提交回复
热议问题