Thread: Passing a templated function for WNDPROC

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    Passing a templated function for WNDPROC

    Hi, I'm trying to create a generic callback to use for my custom widgets, seeing as I pass control out to a class to keep it object oriented I need to create a callback for each individual widget however the only difference between each callback is the type of each class.

    Example:
    Code:
    LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
        WIDGETINFO* p = (WIDGETINFO*)GetWindowLong(hwnd, 0);
        if(p){
            return p->proc(hwnd, msg, wParam, lParam);
        }else{
            p = new WIDGETINFO;
            SetWindowLong(hwnd, 0, p);
        }
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    So for each new widget I create I have to create a new wndproc and change WIDGETINFO and that's all, because the proc member of the WIDGETINFO handles the messages and everything to keep this all really modular and easy to handle.

    Could I use polymorphism? I dunno, probably not because I need to change "new WIDGETINFO" ???
    Umm, I had attempted using a templated function but I wasn't really confident about it before I did it and it failed as I had predicted.

    My problem hinges [with the templated approach] in passing the type specifier for the lpfnWndProc member of the WNDCLASS struct when registering it.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    A typical approach is to make the window procedure a static member function of the c++ class wrapper and then forward messages from there to a virtual member function.

    This has been discussed often in the past and a board search will provide you with more information including examples and links.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM