Skip to main content
GameDev.net gamedev.net
🔒 Locked

Trivial problem

Started by lejno Nov 22, 2003 at 9:14 AM 4 replies 600+ views
Original Post
lejno
lejno
I have a very trivial problem... I'm trying to do this:
class SomeClass
{
public:
     SomeClass(HWND hWnd)
     {
          this->hWnd = hWnd;

          WindowProcOriginal = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WindowProcHook);
     }

     ~SomeClass()
     {
          SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WindowProcOriginal);
     }
private:

     HWND hWnd;

     LRESULT CALLBACK WindowProcHook(HWND, UINT, WPARAM, LPARAM)
     {
          return CallWindowProc(WindowProcOriginal, hWnd, message, wParam, lParam);
     }

     WNDPROC WindowProcOriginal;
};
  
Visual C++ 6.0 tells me that I cant convert WindowProcHook to LONG I've done this before, but not in a class... [edited by - Lejno on November 22, 2003 10:15:59 AM]
angry
angry
WindowProcHook must be static. Classes has a closure that screws everything up.
Oxyd
Oxyd
But that interests me too! I know, that if I make the window procedure function static, it''ll work - but sometimes I want it not to be static (acces the member functions) - how can it be done with non-static window procedure?



---
Unreadable code is code written on a piece of paper, but not the one, in which its programmer is using a space in place you don''''t.
angry
angry
a window proc function must be static if put in a class. you can always pass this pointer to the hwnd by creating a property for it like this:

SetProp(myhWnd, "WINDOW_POINTER", this);

and then retrieve the pointer to which object the message is thrown to eh.:

MyWindowClass* wnd = (MyWindowClass*)GetProp(myhWnd, "WINDOW_POINTER");

of course having a shorter name for the property will speed the getter function up.. ie SetProp(hWnd, "_", this);

There are of course more ways of storing the this pointer. The more common way is by using SetWindowLong and GetWindowLong functions. However they have a downside and that is that they can''t be used for anything else. That is you can''t do SetWindowLong(hWnd, GWL_USERDATA, this); and then belive you can pass something else like SetWindowLong(hWnd, GWL_USERDATA, someStruct); and then think you can receive the this pointer, because it''s lost. Hopefully that wasnt too confusing.
Mastaba
Mastaba
That''s a rather contrived downside to SetWindowLong. If you needed more attributes for the window, simply indicate you need to allocate more private memory for the window when you register the window''s class.
lejno
lejno
Wow, this is a well known problem, huh?

I was kinda hoping someone would post me one reply telling me to put a couple of ()'s in the code or something

Anyways, there seems to be no non-ugly way to solve this, so i guess I'll just go with the least ugly one and then rename the .cpp/.h files to something noone would look at and hope for the best ...

[edited by - lejno on November 22, 2003 2:59:19 PM]

Topic Locked

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

Sign in to reply to this topic.