i want to create class to instantiate windows on the fly, but since few weeks cant get it to work, and any help would be highly appreciated
As i was not able to retrieve correct data with GetObjectFromHandle function, i tried to use std::map to store class instances, and in constructor i can access data from the map as expected, but from message loop i can access only garbage while HWND hWnd is correct.
here is the code
.h
#ifndef BASE_WINDOW_H
#define BASE_WINDOW_H
#include "GlobalApp.h"
#include <string>
#include <map>
namespace G{
class cWin;
static void SetObjectToHandle( HWND hWnd, LPARAM lParam );
static cWin *GetObjectFromHandle( HWND hWnd );
class cWin{
static LRESULT CALLBACK internal_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
public:
static std::map<HWND, cWin*> hwndMap;
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
cWin();
int registerCls();
HWND createWnd();
HWND hWnd;
};
}
#endif
and .cpp
#include "stdafx.h"
#include "BaseWindow.h"
HWND G::cWin::createWnd(){
HWND hWnd;
hWnd = ::CreateWindowEx(WS_EX_WINDOWEDGE, L"div", NULL,
WS_CHILD | WS_VISIBLE,
50, 50, 50, 50,
G::hWnd, NULL, G::hInst, this );
::UpdateWindow( hWnd );
return hWnd;
}
int G::cWin::registerCls(){
WNDCLASSEX wcex;
if ( !::GetClassInfoEx(G::hInst, L"div", &wcex) ){
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)this->internal_WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = DLGWINDOWEXTRA;
wcex.hInstance = G::hInst;
wcex.hIcon = LoadIcon(G::hInst, MAKEINTRESOURCE(IDI_WINAPITWO));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+3);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"div";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
if ( ::RegisterClassEx(&wcex) == 0 ){
G::Console( L"wcex_ERR" );
return -1;
}
}
return 0;
}
G::cWin::cWin(){
this->registerCls();
this->hWnd = this->createWnd();
G::Console( L"wndCreated", this->hWnd );
this->hwndMap.insert( std::pair< HWND, G::cWin*>( this->hWnd, this ) );
G::cWin *pWnd = this->hwndMap[ this->hWnd ];
G::Console( L"map:", pWnd->hWnd ); //point to correct data
}
LRESULT G::cWin::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
if ( !this->hwndMap.count( hWnd ) ){
return DefWindowProc(hWnd, message, wParam, lParam);
}
G::cWin *pWnd = this->hwndMap[ hWnd ];
switch (message){
case WM_LBUTTONDOWN:
G::Console( L"ButtonDown", pWnd->hWnd ); // not correct, why?
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
std::map<HWND, G::cWin*> G::cWin::hwndMap;
LRESULT CALLBACK G::cWin::internal_WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
if( uMsg == WM_NCCREATE ){
G::SetObjectToHandle( hWnd, lParam );
}
G::cWin *pWnd = G::GetObjectFromHandle( hWnd );
if( pWnd ){
return pWnd->WindowProc( hWnd, uMsg, wParam, lParam );
} else
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
void G::SetObjectToHandle( HWND hWnd, LPARAM lParam ){
LPCREATESTRUCT cs = reinterpret_cast<LPCREATESTRUCT>( lParam );
G::cWin *pWnd = reinterpret_cast<G::cWin*>( cs->lpCreateParams );
SetLastError( 0 );
if( !SetWindowLongPtr( hWnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>( pWnd ) ) && GetLastError() ){
G::Console( L"Error" );
}
}
G::cWin *G::GetObjectFromHandle( HWND hWnd ){
return reinterpret_cast<G::cWin*>( GetWindowLongPtr( hWnd, GWL_USERDATA ) );
}
i use visual studio 2005