CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Child windows and OOP

    Hello! I'm working on a library that's going to deal with windows (to be more specific, I'm wrapping some windows APIs that deals with window creation/destruction and messages (like a window click etc.) into OOP).

    In the message handler this is what i have:

    Code:
    LRESULT CALLBACK zooppMessageHandler::msgHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        zooppMessageHandler *obj = (zooppMessageHandler*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
        int retVal = false;
    
        if(obj == NULL)
            if(msg == WM_CREATE || msg == WM_INITDIALOG)    //window creation
                {
                    SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(((CREATESTRUCT*)lParam)->lpCreateParams));
                    obj = (zooppMessageHandler*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    
                    retVal = obj->OnInit();
    
                    if(retVal != false)
                        return retVal;
                    else
                        return DefWindowProc(hwnd, msg, wParam, lParam);
                }
            else if(msg == WM_PARENTNOTIFY && wParam == WM_CREATE)      //child window creation
                {
                }
    
        retVal = obj->Handle(msg, wParam, lParam);
    
        if(retVal != false)
            return retVal;
        else
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    I know that so far it may look a bit unprofessional/unoptimized but i don't care about that yet. As you may see, for normal windows I pass the 'this' pointer into CreateWindowEx() function, and attach it's address to the window's handle with SetWindowLongPtr(). When a message (other tham WM_CREATE, WM_INITDIALOG etc.) comes I just grab the address of 'this' by calling GetWindowLongPtr() and then handle the message.

    But...I ran into a problem regarding child windows. As far as I'm aware, when a child window is created the system won't send a WM_CREATE message but instead it will send a WM_PARENTNOTIFY message, wParam contains the child window's message (in my case WM_CREATE) and lParam it's handle. This means I can't pass the address of the object which issued the CreateWindowEx() function into the message handler.

    Am I wrong? If no...how can i pass the object's address when creating child windows? For normal windows (as you see in the above code), I get the object's code from lParam which contains a pointer to a CREATESTRUCT structures in which member lpCreateParams points to the address of the object which issued a CreateWindowEx() function call.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Child windows and OOP

    how can i pass the object's address when creating child windows?
    Don't you create the ones with CreateWindowEx? If yes, act the same way you did before.
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2010
    Posts
    20

    Re: Child windows and OOP

    A few things. As stated before, if using CreateWindowEx your CHILD windows will get the WM_CREATE message.

    Also, there is a more elegant function to use for storing window data properties, has been since Windows 2000. It's call SetProp/GetProp. There are ANSI and UNICODE versions of these functions.

    Code:
    class CWindow
    {
    protected:
        HWND m_hWindowHandle;
    
        virtual INT_PTR WindowProcess(UINT message,WPARAM wParam,LPARAM lParam)
        {
            // message processing here
        }
    
        static INT_PTR CALLBACK HandleWindowProcess(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
        {
            // first message passed to a window is WM_NCCREATE
    
            if (message == WM_NCCREATE)
            {
                LPCREATESTRUCTW lpCreate = reinterpret_cast<LPCREATESTRUCTW>(lParam);
    
                CWindow * pWindow = reinterpret_cast<CWindow *>(lpCreate->lpCreateParams);
    
                pWindow->m_hWindowHandle = hWnd;
    
                ::SetPropW(hWnd,L"WindowObject");
    
                return pWindow->WindowProcess();
            }
            else
            {
                 CWindow * pWindow = reinterpret_cast<CWindow *>(::GetPropW(hWnd,L"WindowObject"));
    
                 if (pWindow != NULL)
                 {
                     return pWindow->WindowProcess(message,wParam,lParam);
                 }
                 else
                 {
                     return ::DefWindowProcW(hWnd,message,wParam,lParam);
                 }
            }
        }
    };
    Last edited by CppCoder2010; April 23rd, 2010 at 07:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured