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

Manually 'Dispatching' Messages

Started by Sephirox Jul 11, 2004 at 12:57 PM 7 replies 550+ views
Original Post
Sephirox
Sephirox
hi, i have a question... is there anyway i can process messages on my own, using a function besides a WNDPROC? i am making a class to contain a window, and i want to use a member function as the WndProc, allowing me access to member data i dont want to have to call 'DispatchMessage()', instead i want to call the WndProc manually (which will allow me to use my own custom function type or a member function) like this: TranslateMessage(&Msg); CustomWndProc(hWnd, Msg.message, Msg.wParam, Msg.lParam, ...); this would allow me to add custom parameters to the function (not actually doing that), but i need it so i can use a member function i've done something like this before, but it didnt work for some odd reason, cant remember what it was though...i think it had something to do with WM_DESTROY not actually being sent or something weird like that... thx for any help in advance
DigitalDelusion
DigitalDelusion
I would actually guess that it's at least error prone, simply because functions like SendMessage directly calls the WNDPROC.

Why not simply encapsulate it and have it call a per window specific function (i.e. the same way you create window wrappers)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
klems
klems
Couldn't you do something like:
int CustomMessageHandler(MSG msg){    switch(msg.message)    {    ...    }    if(AllowDefaultMessageProcessingForThisMessage(msg))        DispatchMessage(&msg);    return some_value;}
Sephirox
Sephirox
hmm...

what i was looking for was more of a way to have access to member data in a WndProc
DigitalDelusion
DigitalDelusion
Quote:
Original post by Sephirox
what i was looking for was more of a way to have access to member data in a WndProc


member data? well if you're going hardcore W32API GetWindowLong and extra storage alloced in your window is a nice thing.

Else just search for wrapping window procedure or something like that and you'll find articles both here on gamedev and elswhere on how to encapsulate your window proc in a nice way.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
klems
klems
Wait, you want to access member data from a class in your WNDPROC? That shouldn't be too hard, just make your WNDPROC a friend of the class whose member you want to touch and pass a pointer to the class instance associated with the window, either with WM_CREATE or a custom WM. Make the pointer static.

EDIT: An example...
#define MYWM_SETCLASSPTR WM_USER+1class CMyWindowClassThingy{public:    friend LRESUT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);    void SomeFunc() {/*stuff*/}private:    int m_member;};LRESUT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){    static CMyWindowClassThingy *ptr = 0;    switch(uMsg)    {    case MYWM_SETCLASSPTR:        ptr = (CMyWindowClassThingy*)wParam;        break;    }    return DefWindowProc(hWnd, uMsg, wParam, lParam);}
Sephirox
Sephirox
ok thx:)

this is what i have so far:

LRESULT DirectDrawWindow::WndProc()
{
switch(Msg.message)
{
case WM_KEYDOWN:
MessageBox(0,0,0,0);
break;
case WM_QUIT:
isOpen = false;
PostQuitMessage(0);
break;
case WM_DESTROY:
isOpen = false;
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, Msg.message, Msg.wParam, Msg.lParam);
}
return 0;
}

and the MessagePump:

while (PeekMessage(&Msg, hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
//DispatchMessage(&Msg);
WndProc();
}

all this works fine, the messagebox pops up, but when i close the window the process is still running...im guessing that DispatchMessage() does something besides just calling the WndProc...if i just new what it was...

EDIT:
hmm, passing that pointer could work, i might end up doing that, thx
klems
klems
Quote:
im guessing that DispatchMessage() does something besides just calling the WndProc...if i just new what it was...
The WndProc needs to return true for the program to exit, IIRC.

Or you could go the dirty way and just ExitProcess(0);
Sephirox
Sephirox
ExitProcess is too much of a hack, lol

i think sending a MYWM_SETCLASSPTR message is a bit too hacky too tho...

Topic Locked

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

Sign in to reply to this topic.