Skip to main content
gamedev.net
🔒 Locked

Problems with WndProc

Started by JK 3 replies 1.1k views
Original Post
JK
JK
How can I get a WNDPROC (Pointer) if my Funktion (LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) is in a class ??? Its it possible to do that ???
QWERTY
QWERTY
Yes, it is possible, but you have to make it a static method.

JK
JK
THX

But if I make the method static I cant use my class as I plan zu use it.

2Late
2Late
You could try search the archives... :-)

I use this kind of approach:

class MyWindowClass
{
...
ATOM m_aAtom;
static LRESULT CALLBACK sWndProc(....);
LRESULT MsgProc(UINT msg,WPARAM,LPARAM);

};

in .cpp

MyWindowClass::MyWindowClass()
{
m_aAtom = ::GlobalAddAtom("myWndProc");
}

MyWindowClass::~MyWindowClass()
{
::RemoveProp(m_hWnd,"myWndProc");
}

and after you created your window call

::SetProp(m_hWnd,"myWndProc",(HANDLE)this);

LRESULT CALLBACK MyWindowClass::sWndProc(...)
{
MyWindowClass *pThis;
pThis = (MyWindowClass *)GetProp(hWnd,"myWndProc");
if(pThis != NULL)
{
return MsgProc(msg,wParam,lParam);
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

LRESULT MyWindowWindowClass::MsgProc(...)
{
// Handle messages here
}

hope this helps...
DrPizza
DrPizza
I will take an example from threads, because they''re a little easier (ThreadProcs take a void* directly; WndProcs take it via a struct pointed at by lParam, and I don''t have any code lying around that examines that), but the technique can be used to work with WndProcs if you read up on the WndProc''s parameters.

This is uncompiled and untested, but the principle is sound.

  
template <typename C>
struct ThreadInfo
{
// we have an object

C* obj;
// and that object has a function that takes void* and returns DWORD, and so is suitable for a threadproc (except for it being a member function)

DWORD (C::* function)(void*);
// and we have any amount of extra data that we might need.

void* data;
};

template <typename C>
static DWORD WINAPI runThreadFunction(void* data)
{
ThreadInfo<C>* ti = (ThreadInfo<C>*)data;
C* obj = (C*)(ti->obj);
return (obj->*(ti->function))(ti->data);
}


To use this, we fill out a ThreadInfo and pass it to runThreadFunction:
  
#include <windows.h>
#include <iostream>
using namespace std;

class MyClass
{
public:
DWORD myThreadProc(void* data) { size_t i = (size_t)data; cout << "This is thread number " << i << endl; return 0; }
};

int main(void)
{
MyClass* c = new MyClass;
const size_t numThreads = 10;
HANDLE* threads = new HANDLE[numThreads];
ThreadInfo<MyClass>* ti = new ThreadInfo<MyClass>[numThreads];
for(size_t j = 0; j < numThreads; ++j)
{
ti[j].obj = c;
ti[j].function = &MyClass::myThreadProc;
ti[j].data = (void*)j;
threads[j] = CreateThread(NULL, 0, runThreadFunction<MyClass>, (void*)&ti[j], 0, NULL);
}
WaitForMultipleObjects(numThreads, threads, TRUE, INFINITE);
// can''t remember if I now need to close the thread handles

for(size_t j = 0; j < numThreads; ++j)
{
CloseHandle(threads[j]);
}
delete [] threads;
delete [] ti;
}

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/

Topic Locked

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

Sign in to reply to this topic.