callbacks within a class?

This is a discussion on callbacks within a class? within the C++ Programming forums, part of the General Programming Boards category; heeuo, I'm just wondering if something like this is possible: Code: class myClass{ void RegisterWindow(); RESULT CALLBACK myProc(HWND hWnd, UINT ...

  1. #1
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161

    callbacks within a class?

    heeuo, I'm just wondering if something like this is possible:

    Code:
    class myClass{
    void RegisterWindow();
    RESULT CALLBACK myProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    ...
    }
    
    ...
    ...
    
    void myClass::RegisterWindow()
    {
    WNDCLASSEX wnd;
    wnd.cbClsExtra=NULL
    ...//some more initialization
    wnd.lpfnWndProc = myProc;
    ...
    }
    offcourse the above generates errors but is there a way to use a class member as callback function??

    /thankkkks
    /btq
    /
    /

    [edit]Nice try with the code tags, read this for next time. Hammer.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    a callback must be a _stdcall which means that it must not be a member function unless..... It's global or static. I'll give an example of how in just a sec
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    class Window
         {
         public:
              void CreateWnd(HWND parentwnd);
              .
              .
              .
         private:
              static int APIENTRY WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
              HWND
                   m_hwnd;
         };
    
    
    void Window::CreateWnd(HWND parentwnd)
         {
         m_hwnd = ::CreateWindow("ClassName","title stuff",
                             WS_BLAHBLAH, x, y, w, h, 
                             parentwnd, (HMENU)NULL, hInst, (LPARAM)this);
        
         }
    
    int APIENTRY Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
         {
         Window *This = (Window*)GetWindowLong(hWnd,GWL_USERDATA);
         if(message == WM_CREATE)
               {
               CREATESTRUCT *cs = lParam;
               This = (Window*)cs->lpCreateParams;
               SetWindowLong(hWnd,GWL_USERDATA, cs->lpCreateParams);
               }
         if(This)
             return This->CallBack(message, wParam, lParam);
         else
              return DefWindowProc(hWnd, message, wParam, lParam)
         }
    probably errors in this code, I just typed it in the browser and didn't actually attempt a compile but this is the idea
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    thanks man
    /btq

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    You can also defeat the WindProc by declaring it like:

    friend LRESULT CALLBACK WindowProcedure(/.../)

    And FillYourBrain: it is just as easy setting the GWL_USERDATA right after calling CreateWindowEx(). But then again, what's the diff, right?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Sebastiani, you're right. In fact I used to do that. The only difference is I liked getting the hWnd into the class during WM_CREATE (an assignment that I left out in the rushed example).
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21