Advertisement

WindowProc question

Started by November 25, 2001 11:05 PM
1 comment, last by didilabu2 23 years, 3 months ago
Does anyone know how to make the WindowProc function part of a class. Anotherwords, how can I do this: WNDCLASS wc; . . . wc.lpfnWndProc = windowProc; . . . when windowProc is in a class? I can only get it to work when the function is static or a friend function, but I dont want this. Thanks for the help.
I don''t think it''s possible, because the wndproc doesn''t have the this pointer. But, I''m no expert so don''t take my word for it =)
Advertisement
you can do so as far as your WNDPROC is an static method of your class, because WinAPI is C and only accepts C functions or static C++ methods:


//CWin32Window.h

class CWin32Window
{

...

private:

static LRESULT CALLBACK wndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);

...

};

//CWin32Window.cpp

LRESULT CALLBACK CWin32Window::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
}



if you want to gain access to your class instance from your static method use SetWindowLong with GWL_USER_DATA:



// when building the window

m_hWnd = CreateWindow(...);

if(!m_hWnd)
return 0;

SetWindowLong(m_hWnd, GWL_USERDATA, (long)this);

// in your WNDPROC

CWin32Window* currWnd;

currWnd = (CWin32Window*)GetWindowLong(hWnd, GWL_USERDATA);



And if you need to use your current window class pointer in the WM_CREATE handling you''ll notice than this previous thing will be returning null, because WM_CREATE is issued before you can call SetWindowLong, so in that case use the CREATESTRUCT passed as parameter in WM_CREATE:



// when building the window
// fill CREATESTRUCT in createWindow

m_hWnd = CreateWindow(..., (void*)this);

if(!m_hWnd)
return 0;

SetWindowLong(m_hWnd, GWL_USERDATA, (long)this);

// in your wndProc
CWin32Window* currWnd;

if(message == WM_CREATE)
currWnd = (CWin32Window*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
else
currWnd = (CWin32Window*)GetWindowLong(hWnd, GWL_USERDATA);



Hope it''s of any help for u...

Matt
Matt

This topic is closed to new replies.

Advertisement