My windows wrapper

Started by
5 comments, last by wild_pointer 21 years, 7 months ago
I sat down for an hour and came up with the simplest wrapper I could, looking for comments/suggestions/errors Win.h
  
class CWin  
{
public:
	CWin( HINSTANCE app_inst );
	virtual ~CWin();

    bool MakeWindow( std::string title, RECT Res );
    bool ProcessMessages( MSG &msg );
    static LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param );

private:
    WNDCLASSEX ClassInfo;
    HWND hwnd;
};
  
Win.cpp
  
CWin::CWin( HINSTANCE app_inst )
{
    WNDCLASSEX WndClass =
    {
        sizeof( WNDCLASSEX ),               // cbSize

        CS_HREDRAW | CS_VREDRAW,            // style

        WndProc,                            // lpfnWndProc

        0, 0,                               // cbClsExtra, cbWndExtra

        app_inst,                           // hInstance

        LoadIcon( NULL, IDI_APPLICATION ),  // hIcon

        LoadCursor( NULL, IDC_ARROW ),      // hCursor

        (HBRUSH)GetStockObject( WHITE_BRUSH ),  // hbrBackground

        NULL,                               // lpszMenuName

        "lpszClassName",                    // lpszClassName

        LoadIcon( NULL, IDI_WINLOGO )       // hIconSm

    };

    ClassInfo = WndClass;
}

CWin::~CWin()
{
    DestroyWindow( hwnd );
}

bool CWin::MakeWindow( std::string title, RECT Res )
{
    if( 0 == RegisterClassEx( &ClassInfo ) )
        return false;

    hwnd = CreateWindowEx(
        NULL,                               // dwExStyle

        ClassInfo.lpszClassName,            // lpClassName

        title.c_str(),                      // lpWindowName

        WS_OVERLAPPEDWINDOW | WS_VISIBLE,   // dwStyle

        Res.left, Res.top,                  // x, y

        Res.right, Res.bottom,              // nWidth, nHeight

		NULL,                               // hWndParent

		NULL,                               // hMenu

		ClassInfo.hInstance,                // hInstance

		NULL );                             // lpParam


    if( NULL == hwnd )
        return false;

    return true;
}

LRESULT CALLBACK CWin::WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param )
{
    switch( msg )
    {
        case WM_CLOSE:			            // Windows is closing

            DestroyWindow( hwnd );
            PostQuitMessage( 0 );
            return 0;
            break;

        default:
            break;
    }

    return DefWindowProc( hwnd, msg, w_param, l_param );
}

bool CWin::ProcessMessages( MSG &msg )
{
    if( PeekMessage( &msg, hwnd, NULL, NULL, PM_REMOVE ) )
    {
        if( msg.message == WM_QUIT )
        {
            return false;
        }
        else
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }

    return true;
}
  
WinMain
  
int APIENTRY WinMain( HINSTANCE hInstance,
                      HINSTANCE hPrevInstance,
                      LPSTR     lpCmdLine,
                      int       nCmdShow )
{
    CWin        MyWin( hInstance );

    RECT rect = { 0, 0, 800, 600 };

    if( false == MyWin.MakeWindow( "My Window", rect ) )
    {
        MessageBox( NULL, "MakeWindow", "ERROR", MB_OK );
        return 0;
    }

    MSG	        msg;

    while( MyWin.ProcessMessages( msg ) )
    {
        // Do Stuff

    }

	return msg.wParam;
}
  
I was thinking about passing a function pointer to ProcessMessages, and putting the loop inside the process method.


MyWin.ProcessMessages( msg, UpdateScene )

void UpdateScene()
{
    // Do Stuff
}
 
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
Advertisement
Nothing? Ok, well I just made a opengl class that is derived from the one above. Seems to work fairly well, just had to overload the makewindow method.

Only reason i''m asking is this is the start of my first non-procedural "engine", i don''t want to paint myself into a corner. Anything to watch out for?
[size=2]
I made such a class too. I just added virtual mouse and keyboard functions to my application-class that can be overloaded by any class that extends it.
The code you have for process messages function is the same code used for all the controls (windows even) in your program. You need ONE copy of that code in your main function.

Image what would happen if you have 2 windows and you call both of their processmessage functions in the main code.

I suggest you create a CApplication class and put the main function in there as static and wrap the message handling code in there.

Also, you might want to give access to the window handle (HWND) to other objects, so why not make an accessor function getHandle(). Just an idea.

-Viktor


-G|aD-
I see what your saying, at this point shouldn''t be a big deal, because I don''t anticipate having to have more then one window. Unless I''m missing something really obvious, which is entirely possible.

I''m trying to avoid accessors/mutators, hopefully they won''t be neccesary.


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
What''s wrong with accessors?
daerid@gmail.com
A single window app is pretty limiting. You can see one way to accomplish what Viktor is suggesting here:

http://home.earthlink.net/~deadlyo/Programming/WinApi/WindowsAPI_Overview.html

David

This topic is closed to new replies.

Advertisement