B
Bonj
i'm trying to create a class that will contain all the features of
instantiating a window and showing it. I've got the SetWindowLong /
GetWindowLong pair to succesfully store the 'this' pointer in the hWnd's
GWL_USERDATA, but the pointer that it's referring to always seems to point
to the base class and never the derived class. I don't know what I'm doing
wrong, this has been driving me mad for hours. Consequently, the static
WndProc handler function can never route the messages to the class's actual
wndproc (which is virtual and implemented in the derived class) - it gives
me some low-level debug error of 'pure virtual function call' .... WHY can't
I get it to see the derived class? help!....
Thanks
this is in window1.h (the base class)
#ifndef WINDOW1CLASS_DEFINED
#define WINDOW1CLASS_DEFINED
class Window1
{
public:
//variables:
HWND hWnd;
HCURSOR hcWait, hcArrow;
BOOL bWaiting;
//methods:
void Create( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle);
static LRESULT CALLBACK WPHandler(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
virtual LRESULT WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) = 0;
virtual int ShowModally(int nCmdShow) = 0;
//constructor/destructor:
Window1() : hWnd(NULL) {};
virtual ~Window1() {};
Window1( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle);
};
#endif
//this is in Window1.cpp
//======================================
#include "Generics.h"
#include "Window1.h"
Window1::Window1( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle)
{
Create(hInst, hIcon, hIconSm, hbrBackground,
lpszCaption, nClassStyle, nWindowStyle);
}
LRESULT CALLBACK Window1::WPHandler( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
if(uMsg == WM_NCCREATE)
{
LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
SetWindowLong(hWnd, GWL_USERDATA, (long)lpcs->lpCreateParams);
}
Window1* pThis = (Window1*)(LPVOID):GetWindowLong(hWnd, GWL_USERDATA));
//Window1* pThis = (Window1*)(GetWindowLong(hWnd, GWL_USERDATA));
if(pThis) return pThis->WndProc(hWnd, uMsg, wParam, lParam);
else return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void Window1::Create(HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle)
{
bWaiting = FALSE;
WNDCLASSEX wcx;
memset(&wcx, 0, sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = nClassStyle;
wcx.lpfnWndProc = Window1::WPHandler;
wcx.hInstance = hInst;
wcx.hIcon = NULL;
wcx.hCursor = NULL;
wcx.hbrBackground = hbrBackground;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = lpszCaption;
wcx.hIconSm = NULL;
hcWait = (HCURSOR)LoadImage( NULL,
IDC_ARROW,
IMAGE_CURSOR,
CW_USEDEFAULT,
CW_USEDEFAULT,
LR_DEFAULTSIZE | LR_DEFAULTCOLOR | LR_SHARED);
hcArrow = (HCURSOR)LoadImage( NULL,
IDC_WAIT,
IMAGE_CURSOR,
CW_USEDEFAULT,
CW_USEDEFAULT,
LR_DEFAULTSIZE | LR_DEFAULTCOLOR | LR_SHARED);
ATOM ClassName = RegisterClassEx(&wcx);
hWnd = CreateWindow( (LPCTSTR)ClassName,
lpszCaption,
nWindowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND)NULL,
(HMENU)NULL,
(HINSTANCE)hInst,
(LPVOID)this);
}
//and the base class is defined as such (settingsdlg.h)
#include "Window1.h"
class SettingsDlg : public Window1
{
public:
//constructor/destructor:
SettingsDlg( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle) :
Window1( hInst,
hIcon,
hIconSm,
hbrBackground,
lpszCaption,
nClassStyle,
nWindowStyle) {};
//SettingsDlg() {};
//~SettingsDlg() {};
LRESULT WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
int ShowModally(int nCmdShow);
};
//(settingsdlg.cpp)
#include "Generics.h"
#include "settingsdlg.h"
int SettingsDlg::ShowModally(int nCmdShow)
{
{
if(hWnd)
{
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}
else
return -1;
}
{
MSG msg;
while(GetMessage(&msg, hWnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
}
LRESULT SettingsDlg::WndProc( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch(uMsg)
{
case WM_MOUSEMOVE:
{
SetCursor((bWaiting) ? (hcWait) : (hcArrow));
return 0;
}
/*
case WM_CLOSE:
{
PostMessage(hWnd, WM_QUIT, 0, 0);
return 0;
}
*/
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//and my winmain cpp file looks like this:
#include "Generics.h"
#include "SettingsDlg.h"
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow
)
{
HICON hIcon = (HICON)LoadImage( hInstance,
MAKEINTRESOURCE(IDI_TRANS),
IMAGE_ICON,
GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON),
LR_DEFAULTCOLOR | LR_SHARED),
hIconSm = (HICON)LoadImage( hInstance,
MAKEINTRESOURCE(IDI_TRANS),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR | LR_SHARED);
SettingsDlg sdlg( hInstance,
hIcon,
hIconSm,
(HBRUSH)(COLOR_3DFACE + 1),
"SpaceMaze settings",
CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
WS_OVERLAPPEDWINDOW);
return sdlg.ShowModally(SW_SHOWMAXIMIZED);
}
Thanks ever so much to anybody that can help me out of this !
instantiating a window and showing it. I've got the SetWindowLong /
GetWindowLong pair to succesfully store the 'this' pointer in the hWnd's
GWL_USERDATA, but the pointer that it's referring to always seems to point
to the base class and never the derived class. I don't know what I'm doing
wrong, this has been driving me mad for hours. Consequently, the static
WndProc handler function can never route the messages to the class's actual
wndproc (which is virtual and implemented in the derived class) - it gives
me some low-level debug error of 'pure virtual function call' .... WHY can't
I get it to see the derived class? help!....
Thanks
this is in window1.h (the base class)
#ifndef WINDOW1CLASS_DEFINED
#define WINDOW1CLASS_DEFINED
class Window1
{
public:
//variables:
HWND hWnd;
HCURSOR hcWait, hcArrow;
BOOL bWaiting;
//methods:
void Create( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle);
static LRESULT CALLBACK WPHandler(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
virtual LRESULT WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) = 0;
virtual int ShowModally(int nCmdShow) = 0;
//constructor/destructor:
Window1() : hWnd(NULL) {};
virtual ~Window1() {};
Window1( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle);
};
#endif
//this is in Window1.cpp
//======================================
#include "Generics.h"
#include "Window1.h"
Window1::Window1( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle)
{
Create(hInst, hIcon, hIconSm, hbrBackground,
lpszCaption, nClassStyle, nWindowStyle);
}
LRESULT CALLBACK Window1::WPHandler( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
if(uMsg == WM_NCCREATE)
{
LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
SetWindowLong(hWnd, GWL_USERDATA, (long)lpcs->lpCreateParams);
}
Window1* pThis = (Window1*)(LPVOID):GetWindowLong(hWnd, GWL_USERDATA));
//Window1* pThis = (Window1*)(GetWindowLong(hWnd, GWL_USERDATA));
if(pThis) return pThis->WndProc(hWnd, uMsg, wParam, lParam);
else return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void Window1::Create(HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle)
{
bWaiting = FALSE;
WNDCLASSEX wcx;
memset(&wcx, 0, sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = nClassStyle;
wcx.lpfnWndProc = Window1::WPHandler;
wcx.hInstance = hInst;
wcx.hIcon = NULL;
wcx.hCursor = NULL;
wcx.hbrBackground = hbrBackground;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = lpszCaption;
wcx.hIconSm = NULL;
hcWait = (HCURSOR)LoadImage( NULL,
IDC_ARROW,
IMAGE_CURSOR,
CW_USEDEFAULT,
CW_USEDEFAULT,
LR_DEFAULTSIZE | LR_DEFAULTCOLOR | LR_SHARED);
hcArrow = (HCURSOR)LoadImage( NULL,
IDC_WAIT,
IMAGE_CURSOR,
CW_USEDEFAULT,
CW_USEDEFAULT,
LR_DEFAULTSIZE | LR_DEFAULTCOLOR | LR_SHARED);
ATOM ClassName = RegisterClassEx(&wcx);
hWnd = CreateWindow( (LPCTSTR)ClassName,
lpszCaption,
nWindowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND)NULL,
(HMENU)NULL,
(HINSTANCE)hInst,
(LPVOID)this);
}
//and the base class is defined as such (settingsdlg.h)
#include "Window1.h"
class SettingsDlg : public Window1
{
public:
//constructor/destructor:
SettingsDlg( HINSTANCE hInst,
HICON hIcon,
HICON hIconSm,
HBRUSH hbrBackground,
LPCTSTR lpszCaption,
int nClassStyle,
int nWindowStyle) :
Window1( hInst,
hIcon,
hIconSm,
hbrBackground,
lpszCaption,
nClassStyle,
nWindowStyle) {};
//SettingsDlg() {};
//~SettingsDlg() {};
LRESULT WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
int ShowModally(int nCmdShow);
};
//(settingsdlg.cpp)
#include "Generics.h"
#include "settingsdlg.h"
int SettingsDlg::ShowModally(int nCmdShow)
{
{
if(hWnd)
{
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}
else
return -1;
}
{
MSG msg;
while(GetMessage(&msg, hWnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
}
LRESULT SettingsDlg::WndProc( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch(uMsg)
{
case WM_MOUSEMOVE:
{
SetCursor((bWaiting) ? (hcWait) : (hcArrow));
return 0;
}
/*
case WM_CLOSE:
{
PostMessage(hWnd, WM_QUIT, 0, 0);
return 0;
}
*/
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//and my winmain cpp file looks like this:
#include "Generics.h"
#include "SettingsDlg.h"
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow
)
{
HICON hIcon = (HICON)LoadImage( hInstance,
MAKEINTRESOURCE(IDI_TRANS),
IMAGE_ICON,
GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON),
LR_DEFAULTCOLOR | LR_SHARED),
hIconSm = (HICON)LoadImage( hInstance,
MAKEINTRESOURCE(IDI_TRANS),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR | LR_SHARED);
SettingsDlg sdlg( hInstance,
hIcon,
hIconSm,
(HBRUSH)(COLOR_3DFACE + 1),
"SpaceMaze settings",
CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
WS_OVERLAPPEDWINDOW);
return sdlg.ShowModally(SW_SHOWMAXIMIZED);
}
Thanks ever so much to anybody that can help me out of this !