|
-
April 20th, 2007, 01:40 PM
#1
Problem accessing a class through a static function.
Hello.
I am having problems accessing methods from a class inside a static function.
I basically have two classes. One called CWindow that creates a window(obviously ), which uses a callback function (also inside CWindow).
The other class is called CSkin which will be use the created window and draw things on it.
This is the thing. I want CSkin to call one of it's own methods (in this case PaintSkin(HDC hdc)) when the message WM_PAINT is sent to the window. But, I can't access anything from my CSkin class inside the callback function.
I can easily access them from other functions, but not in the callback function. Which I guess have something to do with it being static, it is however necessary or otherwise I receive a compile error.
My Window class:
Code:
class CWindow
{
public:
CWindow();
~CWindow();
int CreateWindow( );
...
private:
...
CSkin *skin;
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
};
Constructor-code, where I create an instance of my CSkin class:
Code:
CWindow::CWindow()
{
...
CWindow::skin = new CSkin;
...
}
CreateWindow()-code, WndProc needs to be static:
Code:
int CWindow::CreateWindow()
{
...
wc.lpfnWndProc = (WNDPROC)(WndProc);
...
}
Callback-code, this is where I want the instance of CSkin to call it's PaintSkin()-method. But "CWindow::skin->" doesn't give me anything at all, and I can't access it.
Code:
LRESULT CALLBACK CWindow::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
...
if(CWindow::skin != NULL)
// CWindow::skin->PaintSkin(ps.hdc); // Desired result
...
}
}
How shall I proceed? 
(I am using VS.NET 2k5 and the Win32 API).
Thanks.
-
April 20th, 2007, 02:00 PM
#2
Re: Problem accessing a class through a static function.
Please read up on what a static method is. You can't access member functions because a static method is stateless. It does not require an instance of a class to be called and can be called directly through scope resolution, for example CWindow::WndProc(hwnd, msg, wparam, lparam).
Anyway, the moral of the story: you can't call anything stateful -- member variables, member functions -- from a static method.
-
April 20th, 2007, 04:45 PM
#3
Re: Problem accessing a class through a static function.
Make your variable ('CWindow::skin') static.
Viggy
-
April 21st, 2007, 07:09 AM
#4
Re: Problem accessing a class through a static function.
This is a typical problem. You need to pass the object's pointer as a parameter to you static function. You call the static function from one of the class's function and pass "this" pointer as a parameter. However this is not possible in your scenario because WndProc is a callback function and it is called by the system:
The following is a way to handle your problem: It supports for creation of MDI child window. You may not require MDI child window support and your application can be a simple one.
Code:
bool CWindow::CreateWindow(ExStyle,
LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle,
int x, int y, int nWidth, int nHeight, HWND hParent,
HMENU hMenu, HINSTANCE hInst)
{
if ( ! RegisterClass(lpszClass, hInst) )
return false;
// use MDICREATESTRUCT to pass this pointer, support MDI child window
MDICREATESTRUCT mdic;
memset(& mdic, 0, sizeof(mdic));
mdic.lParam = (LPARAM) this;
m_hWnd = CreateWindowEx(dwExStyle, lpszClass, lpszName,
dwStyle, x, y, nWidth, nHeight,
hParent, hMenu, hInst, & mdic);
return m_hWnd!=NULL;
}
Code:
LRESULT CALLBACK CWindow::WndProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
CWindow * pWindow;
if ( uMsg == WM_NCCREATE )
{
assert( ! IsBadReadPtr((void *) lParam,
sizeof(CREATESTRUCT)) );
MDICREATESTRUCT * pMDIC = (MDICREATESTRUCT *)
((LPCREATESTRUCT) lParam)->lpCreateParams;
pWindow = (CWindow *) (pMDIC->lParam);
assert( ! IsBadReadPtr(pWindow, sizeof(CWindow)) );
SetWindowLong(hWnd, GWL_USERDATA, (LONG) pWindow);
}
else
pWindow=(CWindow *)GetWindowLong(hWnd, GWL_USERDATA);
if ( pWindow )
..........................
..........................
else
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
The main two vital functions are SetWindowLong and GetWindowLong
If there is no love sun won't shine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|