Home
Manage Your Code
Snippet: win32 message router (C++)
Title: win32 message router Language: C++
Description: Shows the proper way to direct a message through a router, when creating a win32 class wrapper. It is a public static function and calls the appropriate pure virtual functions defined in derived subclasses Views: 37
Author: Florea Stefan Date Added: 2/8/2010
Copy Code  
1LRESULT C_WindowAbstract::wndRooter( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2{
3    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
4    //OBS: WE DO NOT HAVE ACCESS TO 'THIS' POINTER IN STATIC METHODS//
5    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
6    C_WindowAbstract *wnd = NULL;//this will be associated later
7
8
9    //this is the first message to be sent after calling CreateWindow(..);
10    if(msg == WM_NCCREATE)//before WM_CREATE and before CreateWindow returns!!!
11    {                                                    //we do not have yet hwnd!
12        //we have to retrieve our instance of the class
13        //the window has been created
14        //associate handle with our instance
15        ::SetWindowLong(hwnd,GWL_USERDATA,long((LPCREATESTRUCT(lparam))->lpCreateParams));
16    }
17
18    // --- messages different from WN_NCCREATE / or WM_NCCREATE was just processed ---
19    // we retrieve the instance of AbstractWindow that corresponds to the destination window's HWND
20    wnd = (C_WindowAbstract *) (::GetWindowLong (hwnd, GWL_USERDATA));
21
22    // we then route the message to the wndProc method defined in the derived AbstractWindow class
23    if (wnd)
24        wnd->wndProc(hwnd, msg, wparam, lparam);
25    else
26        // for messages that arrive prior to WM_NCCREATE
27        // and the HWND <-> AbstractWindow * association was not made
28        return ::DefWindowProc (hwnd, msg, wparam, lparam);
29
30}
Usage
Dont have to use it. It is automatically called when calling create(..)