Hey all,
I started building a class which shall manage the window and its objects.
Quellcode follows (Well, that was my first try so don't expect it to be perfect

)
(if you have any suggestions, please let me know!)
I fail at that point where I have to handle the window messages in the WinProc-function.
The problem is, in all tutorials and "help me"-forums I visited, the objects I want to add to the window (e.g. a text or a button) needed to be declared in the WinProc-function.
That means I can't just add a method like .addObject( ) to my window-class.
Or I missed the way to do that. And that's why I ask you, guys.
Do you know any way?
window.h
class WINDOW
{
public:
typedef struct s_icons
{
HICON Large;
HICON Small;
std::string szPathLarge;
std::string szPathSmall;
};
LPCWSTR Topic;
WNDCLASSEX wClass;
HWND hWindow;
MSG wMessage;
HINSTANCE ProcessHandleInstance;
HINSTANCE ParentProcessHandleInstance;
LPSTR CommandLineParams;
int nShowCommand;
struct s_icons Icons;
WINDOW::WINDOW( )
{
/* - - - - - - Window - - - - - - */
ZeroMemory( &this->wClass, sizeof( WNDCLASSEX ) );
ZeroMemory( &this->hWindow, sizeof( HWND ) );
ZeroMemory( &this->wMessage, sizeof( MSG ) );
/* - - - - - - Icons - - - - - - */
this->Icons.Large = NULL;
this->Icons.Small = NULL;
this->Icons.szPathLarge = "";
this->Icons.szPathSmall = "";
}
bool Initiate( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd, WNDPROC winproc, std::string topic )
{
/* - - - - - - Give Window standard values - - - - - - */
this->ProcessHandleInstance = hInst;
this->ParentProcessHandleInstance = hPrevInst;
this->CommandLineParams = lpCmdLine;
this->nShowCommand = nShowCmd;
this->Topic = L"Touhou Invaders";
/* - - - - - - Give wClass standard values - - - - - - */
this->wClass.cbClsExtra = NULL;
this->wClass.cbSize = sizeof( WNDCLASSEX );
this->wClass.cbWndExtra = NULL;
this->wClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
this->wClass.hCursor = LoadCursor( NULL, IDC_ARROW );
this->wClass.hIcon = NULL;
this->wClass.hIconSm = NULL;
this->wClass.hInstance = hInst;
this->wClass.lpfnWndProc = winproc;
this->wClass.lpszClassName = "Window Class";
this->wClass.lpszMenuName = NULL;
this->wClass.style = CS_HREDRAW|CS_VREDRAW;
if( !this->RegisterWindow( ) )
return FAIL;
return SUCCESS;
}
bool RegisterWindow( void )
{
if( !RegisterClassEx( &this->wClass ) )
{
MessageBox( this->hWindow, "Can't register Window-Class!", "(Error): Register", MB_ICONERROR|MB_OKCANCEL );
return FAIL;
}
return SUCCESS;
}
bool CreateWin( void )
{
this->hWindow = CreateWindowEx( NULL, this->wClass.lpszClassName, (LPCSTR)this->Topic, WS_OVERLAPPEDWINDOW, 40, 40, 800, 600, NULL, NULL, this->ProcessHandleInstance, NULL );
if( !this->hWindow )
{
MessageBox( NULL, "Can't create main-window!", "(Error): Create Main-Window", MB_ICONERROR|MB_OKCANCEL );
return FAIL;
}
return SUCCESS;
}
bool Show( void )
{
int x = 0;
if( this->hWindow )
{
ShowWindow( this->hWindow, SW_SHOWNORMAL );
this->Update( );
}
else MessageBox( NULL, "Can't show window!", "(Error): Show", MB_ICONERROR|MB_OKCANCEL );
return SUCCESS;
}
bool Update( void )
{
UpdateWindow( this->hWindow );
return SUCCESS;
}
bool AddIcon( std::string path, int which = 0 )
{
if( which != 1 )
this->Icons.szPathLarge = path.c_str( );
if( which != 2 )
this->Icons.szPathSmall = path.c_str( );
return SUCCESS;
}
struct s_icons * CreateIcons( )
{
this->Icons.Large = (HICON)LoadImage( NULL, this->Icons.szPathLarge.c_str( ), IMAGE_ICON, 32, 32, LR_LOADFROMFILE );
if( !this->Icons.Large )
MessageBox( this->hWindow, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR );
this->Icons.Small = (HICON)LoadImage( NULL, this->Icons.szPathSmall.c_str( ), IMAGE_ICON, 16, 16, LR_LOADFROMFILE );
if( !this->Icons.Small )
MessageBox( this->hWindow, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR );
return &this->Icons;
}
};
WINDOW Window; // global instance to make it easier to use
main.cpp
LRESULT CALLBACK WinProc( HWND, UINT, WPARAM, LPARAM );
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd )
{
Window.Initiate( hInst, hPrevInst, lpCmdLine, nShowCmd, (WNDPROC)WinProc, "Project" );
if( Window.CreateWin( ) )
Window.Show( );
while( GetMessage( &Window.wMessage, NULL, 0, 0 ) )
{
TranslateMessage( &Window.wMessage );
DispatchMessage( &Window.wMessage );
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_CLOSE:
DestroyWindow( hWnd );
break;
case WM_COMMAND:
// Handle messages here
// Handle messages here
// Handle messages here
break;
case WM_CREATE:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}