Skip to main content
gamedev.net
🔒 Locked

Classes and windows callback...

Started by Prankster 2 replies 1.3k views
Original Post
Prankster
Prankster
Cheers, I have a question about something that's been tearing my mind for some time... Is it possible to make the windows callback a class member...? Or to access class members via the callback if it's not a class member...? I've tried a few times, without any luck. Some pseudo code from the top of my head --- Main wrapper
        
//winmain.cpp

//

int WINAPI WinMain(etc...)
{
  // Class registration and main window creation is done in the

  // foobar class constructor

  CFoobar* foo = new CFoobar();

  ...
  ...
}
  
Foobar class header
  
//foobar.h

//

class CFoobar{
  CFoobar();
  virtual~ CFoobar();

  // Possible??

  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
};
  
Foobar class
        
//foobar.cpp

//

CFoobar::CFoobar()
{
  WNDCLASS wc;
  ZeroMemory(...);
  ...
  ...
  wc.WndProc = WndProc; // Doesn't work, i've tried casting as well (bad idéa :))

}
      
So if i instead try to get to the class member in the callback...
  
LRESULT CALLBACK WndProc(...)
{
  CFoobar *foo = (CFoobar*)GetWindowLong(hwnd, GWL_USERDATA);

  ...
  ...

  case WM_BLAH:
  {
    foo->DoSomething(); // Access violation...

  } break;

  ...
  ...
}
        
Well, I need to get this working in someway, and my only option at this time is to pick out the functions i access from the callback out of the classes and make them 'global' (can't find the word i'm looking for...). Can't find any tutorials on it either... Thanks in advance. [edited by - prankster on January 3, 2003 7:30:13 AM] [edited by - prankster on January 3, 2003 7:30:52 AM] [edited by - prankster on January 3, 2003 7:31:59 AM]
billybob
billybob
i''ve had a lot of trouble with this as well, and the way i eventaully solved it was just have one global AppWndProc(...). i also have (my only global variable) is a pointer to the instance of my app. all my wndclass and wndclassex have AppWndProc as their wndproc arg. the AppWndProc looks like this:

LRESULT CALLBACK AppWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return pAppInst->WndProc(hWnd, Message, wParam, lParam);
}

the pAppInst also has several LRESULT BlahBlahProcs(..), and the general WndProc checks which HWND is recieving the message, and then sends it off to the correct process, also a member of the class. it looks like this:

LRESULT CApp::WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
if(hWnd == hMainWnd)
return MainWndProc(hWnd, Message, wParam, lParam);
else if(hWnd == hViewClient)
return ViewClientProc(hWnd, Message, wParam, lParam);

else if(hWnd == hViewWndTop)
return ViewTopProc(hWnd, Message, wParam, lParam);
...
...
...
else
return DefWndProc(hWnd, Message, wParam, lParam);
}

i know its crappy with the if else if else if stuff, but the compiler doesn''t like switch(HWND)

this is how i avoid having a bunch of global window processes, and it also allows me to make general WNDCLASSes. i have 3 or 4 classes, like ones with the IDC_CROSS cursor, stuff like that.
original vesoljc
original vesoljc
try searching for a window wrapper...
well here''s what i did (with big help from gd)


  
// in class header

static LRESULT CALLBACK Router (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
LRESULT CALLBACK WindowProc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

// implementation

LRESULT CALLBACK WinApp::Router(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (msg == WM_NCCREATE)
{
LPCREATESTRUCT lpcs = LPCREATESTRUCT(lparam);
SetWindowLong(hwnd, GWL_USERDATA, (long) lpcs->lpCreateParams);
}
WinApp *pThis = (WinApp *) GetWindowLong(hwnd, GWL_USERDATA);
if (pThis)
{
return pThis->WindowProc(hwnd, msg, wparam, lparam);
}
else
{
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}

LRESULT CALLBACK WinApp::WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
Destroy();
PostQuitMessage(0);
break;
case WM_ACTIVATE:
if(LOWORD(wparam))
{
Activate(true);
}
else
{
Activate(false);
}
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}

// and when creating WNDCLASS

m_class.lpfnWndProc = WinApp::Router;





Abnormal behavior of abnormal brain makes me normal...
Abnormal behavior of abnormal brain makes me normal...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.