Looking for a simple and interesting implementation of Windows' WndProc in C++ classes

B

Beach Potato

Hi:

I've searched newsgroups and various archives, including MSDN & MFC sources,
but at this point failed to locate an accurate and simple implementation of
WndProc function for MSWindows window objects.
Something like:

class BaseWindow {
virtual LRESULT WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam);
virtual OnPaint();
};

Then I would create a window pointing to a static StaticWndProc

static LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT message, WPARAM
wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, message, wParam, lParam);
}

But at certain point this static wndproc should be switched to the
BaseWindow::WndProc or gain access to the instance of the BaseWindow object
somehow. What is the simplest technique to make it happen?

So far, I figured only a way to throw an exclusive global pointer of this to
GWL_USERDATA and retrieve the object instance from it within every event
thrown to the static window procedure.

BaseWindow* g_window_ptr;
static LRESULT CALLBACK InitWndProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
SetWindowLong( hWnd, GWL_USERDATA, (long) g_window_ptr );
SetWindowLong( hWnd, GWL_WNDPROC, (long) StaticWndProc__v2 );
return DefWindowProc(hwnd, message, wParam, lParam);
}

static LRESULT CALLBACK StaticWndProc__v2(HWND hwnd, UINT message, WPARAM
wParam, LPARAM lParam)
{
BaseWindow* l_window_ptr = (BaseWindow *) GetWindowLong( hWnd,
GWL_USERDATA );
return l_window_ptr->WndProc(hwnd, message, wParam, lParam);
}

What other simple ways do you use to build windows classes, inheriting
WndProc and event handlers?
 
B

Buster

Beach Potato said:
So far, I figured only a way to throw an exclusive global pointer of this to
GWL_USERDATA and retrieve the object instance from it within every event
thrown to the static window procedure. [...]
What other simple ways do you use to build windows classes, inheriting
WndProc and event handlers?

One other way is to use a global 'std::map <HWND, BaseWindow*>'.

Regards,
Buster.
 
A

Alf P. Steinbach

...
What other simple ways do you use to build windows classes, inheriting
WndProc and event handlers?

As Attila Fehler wrote, this question is _off-topic_ in clc++.

Try to ask in [comp.os.ms-windows.programmer.win32], and please do read
Shiva's welcome-message as well as the FAQ before posting more in clc++.

This response has been crossposted to [comp.os.ms-windows.programmer.win32],
with follow-ups set there (that means if you answer, your answer will by
default be submitted to that group).

Hth.,

- Alf
 
B

Beach Potato

One other way is to use a global 'std::map <HWND, BaseWindow*>'.

The question is at what point and where I'd tie these two together.
CreateWindow throws a bunch of calls, like WM_NCCREATE, WM_CREATE before
releasing the handle to the caller.
So if I do:

void BaseWindow::Init() {
HWND hWnd = CreateWindow();
... <- manupulate with the map
}

it may be too late, and I'll miss the events.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top