WndProc - Windows Classes Question

T

Turbo_King

Hi,

I am trying to write a class which creates a window and then assigns a
method of the class as the wndproc function for the window so that
each instance of the class can handle the messages for its repective
form.

I am trying to do the following:

class myWinClass {
.....
private: LRESULT CALLBACK _wndProc(HWND, UINT, WPARAM, LPARAM);
.....
}
.....
WNDCLASS wc;
.....
wc.lpfnWndProc = (WNDPROC)&_wndProc;
.....

But this raises the following compile error under Borland C++ 5.5
Compiler:

"Cannot cast from 'long (__stdcall * (_closure )(HWND__ *,unsigned
int,unsigned int,long))(HWND__ *,unsigned int,unsigned int,long)'
to
'long (__stdcall *)(HWND__ *,unsigned int,unsigned int,long)'"

I should imagine this is a general c++ syntax problem and would happen
on any c++ compiler not just Borland's.

I can see that a method is not the same as a normal function (it has
the implicit this pointer for example) but would like to know if there
is a way to get this to work.

Thanks for your help

Gareth Williams
 
S

SirMike

Window procedure must be static when is inside a class.
If you don't want to make it static declare WndProc outside the class
and pass it's pointer to the class for exapmle in class constructor.
 
A

Aslan Kral

haber iletisinde sunlari said:
Hi,

I am trying to write a class which creates a window and then assigns a
method of the class as the wndproc function for the window so that
each instance of the class can handle the messages for its repective
form.

I am trying to do the following:

class myWinClass {
....
private: LRESULT CALLBACK _wndProc(HWND, UINT, WPARAM, LPARAM);
....
}
....
WNDCLASS wc;
....
wc.lpfnWndProc = (WNDPROC)&_wndProc;
....

But this raises the following compile error under Borland C++ 5.5
Compiler:

"Cannot cast from 'long (__stdcall * (_closure )(HWND__ *,unsigned
int,unsigned int,long))(HWND__ *,unsigned int,unsigned int,long)'
to
'long (__stdcall *)(HWND__ *,unsigned int,unsigned int,long)'"

I should imagine this is a general c++ syntax problem and would happen
on any c++ compiler not just Borland's.

I can see that a method is not the same as a normal function (it has
the implicit this pointer for example) but would like to know if there
is a way to get this to work.

Thanks for your help

Gareth Williams

Have you checked the ATL source code? It has a nice technique for
generating some thunk code for a WndProc that changes the first
parameter from a HWND to this. So your WndProc member function is directly
called. No static function at all.
 
T

Turbo_King

SirMike said:
Window procedure must be static when is inside a class.
If you don't want to make it static declare WndProc outside the class
and pass it's pointer to the class for exapmle in class constructor.

Thanks for the reply.

Correct me if I'm wrong...

If I make the method static then it won't be able to access its member
variables, because there is only one instance of the wndproc method
but multiple instances of the class that owns it.

If I declare the wnd proc outside the class and hand it a pointer then
how do I get the wndproc to reference the member variables of the
owning class instance?

Thanks again

Gareth Williams
 
H

Howard

Turbo_King said:
Thanks for the reply.

Correct me if I'm wrong...

If I make the method static then it won't be able to access its member
variables, because there is only one instance of the wndproc method
but multiple instances of the class that owns it.

If I declare the wnd proc outside the class and hand it a pointer then
how do I get the wndproc to reference the member variables of the
owning class instance?

Thanks again

Gareth Williams

Most such callbacks have a void* parameter which you can use to pass
whatever data you please. What I do is pass "this" as that pointer, so that
the callback can be a simple one-liner, like such:

{
((MyClass*)userData)->HandleCallback(wParam,hParam);
}

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top