Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C++
Article

A Simple Win32 Window Wrapper Class

Rate me:
Please Sign up or sign in to vote.
4.88/5 (38 votes)
11 Jul 20022 min read 499.9K   7.8K   62   51
How to make a Win32 application object oriented, without using MFC.

Introduction

I like object-oriented programming. In my professional C++ career, I have used nothing but MFC. So, when I want to make a good ole' fashioned Win32/SDK application, I cringe at the thought of not using the OO paradigm. If you're like me and you want to get away from the old C programming style, here is some code that will help you write your own window wrapper class.

Where are all of the examples?

It's nearly impossible to find good examples of a window wrapper class. None of my game development books have one. None of my general Windows programming books do either. Conducting a Google search only gave me one good example, Creating a Win32 Window Wrapper Class. You would think more people would be doing this. Unfortunately, there aren't more examples because its not easy to get it working.

The main problem

When you create a WNDCLASSEX structure you need to give the structure a pointer to your window procedure in the lpfnWndProc member. The window procedure is a callback, a special kind of function called directly by Windows when needed.

To make callbacks work correctly, strict typedefs are defined in the Windows header files. If you try to use a class member as a callback, the compiler will give you an error, saying that the member prototype does not match the required typedef.

The solution

To get around this compiler error, we need to make the window procedure static. From this static method we then call another method of the class to actually handle the messages.

To call the message handler of the correct window, we need to get the pointer of the window class using the SetWindowLong and GetWindowLong API functions. We send the this pointer to the window procedure via the CreateWindow function's LPVOID lpParam // window-creation data parameter.

When the WM_NCCREATE message is received, the window procedure sets the window long value to the pointer and is then able to call the correct message handler.

class CBaseWindow
{
public:
    // there are more members and methods
    // but you can look at the code to see them

    // static message handler to put in WNDCLASSEX structure
    static LRESULT CALLBACK stWinMsgHandler(HWND hwnd, 
                UINT uMsg, WPARAM wParam, LPARAM lParam);
protected:
    // the real message handler
    virtual LRESULT CALLBACK WinMsgHandler(HWND hwnd, 
                UINT uMsg, WPARAM wParam, LPARAM lParam)=0;

    // returns a pointer the window (stored as the WindowLong)
    inline static CBaseWindow *GetObjectFromWindow(HWND hWnd)
    {
        return (CBaseWindow *)GetWindowLong(hWnd, GWL_USERDATA);
    }
};

LRESULT CALLBACK CBaseWindow::stWinMsgHandler(HWND hwnd, 
                   UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    CBaseWindow* pWnd;

    if (uMsg == WM_NCCREATE)
    {
        // get the pointer to the window from
        // lpCreateParams which was set in CreateWindow
        SetWindowLong(hwnd, GWL_USERDATA, 
           (long)((LPCREATESTRUCT(lParam))->lpCreateParams));
    }

    // get the pointer to the window
    pWnd = GetObjectFromWindow(hwnd);

    // if we have the pointer, go to the message handler of the window
    // else, use DefWindowProc
    if (pWnd)
        return pWnd->WinMsgHandler(hwnd, uMsg, wParam, lParam);
    else
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

BOOL CBaseWindow::Create(DWORD dwStyles, RECT* rect)
{ 
    // Create the window

    // send the this pointer as the window creation parameter
    m_hwnd = CreateWindow(szClassName, szWindowTitle, dwStyles, 
        rect->left, rect->top, rect->right - rect->left, 
        rect->bottom - rect->top, NULL, NULL, hInstance, 
        (void *)this);

    return (m_hwnd != NULL);
}

Conclusion

Although, the demo (and source) is a very simple and crude example of what you can do with a window wrapper class, I hope it will help make your Win32 programming projects easier to understand and your code more reusable.

If you can find more information on this subject, I would gladly add a links section to the article.

Credit where credit is due

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
I have been a professional developer since 1996. I live in Illinois, in the USA. I am married and have four children.

Comments and Discussions

 
GeneralRe: Just some thoughts Pin
Jason Henderson5-Dec-02 3:08
Jason Henderson5-Dec-02 3:08 
GeneralRe: Just some thoughts Pin
GMorris23-May-05 4:06
GMorris23-May-05 4:06 
GeneralInteresting... Pin
alex.barylski19-Oct-02 18:10
alex.barylski19-Oct-02 18:10 
GeneralRe: Interesting... Pin
skaanji10-Oct-03 14:48
skaanji10-Oct-03 14:48 
GeneralRe: Interesting... Pin
alex.barylski10-Oct-03 22:20
alex.barylski10-Oct-03 22:20 
GeneralTake a look at jlWindows Pin
Gabriel Bizzotto4-Oct-02 16:53
Gabriel Bizzotto4-Oct-02 16:53 
GeneralExcellent! Pin
Anonymous19-Sep-02 22:48
Anonymous19-Sep-02 22:48 
GeneralRe: Not quite right Pin
Jörgen Sigvardsson11-Jul-02 13:24
Jörgen Sigvardsson11-Jul-02 13:24 
Russ Freeman wrote:
CBaseWindow::MsgLoop() - shouldn't be a part of the window. It has almost no relationship to a window and any relationship it does have isn't 1:1

If you make it static then it would fit better. After all, the purpose is to pump and dispatch window messages. Another nice detail about this is that one could make use of "private" features CBaseWindow, thus you can encapsulate a lot of functions which the user of CBaseWindow should not be able to call.. I found this technique to work remarkably well with XLib on X, and since X is not very different from Win32 in this area I'd think this approach would work equally well on Windows.

Russ Freeman wrote:
Also, they never actually hide any of the crud associated with writing windows programs. You say you want an OO approach and yet the presence of nasty message handling remains. You just end up with a lesser MFC than MFC (albeit lighter).

Not entirely true.. Have you ever had a look at Qt? It feels very OO, in fact it feels as soft and gOOey as Java. Although it is not as blunt as Java... Wink | ;)
You never see any Win32 message nor any X message in Qt. And I must say I really dig their signal/slot mechanism..

Sonorked as well: 100.13197 jorgen
FreeBSD is sexy.
GeneralRe: Not quite right Pin
yarp11-Jul-02 19:52
yarp11-Jul-02 19:52 
GeneralRe: Not quite right Pin
Jason Henderson12-Jul-02 3:08
Jason Henderson12-Jul-02 3:08 
GeneralRe: Not quite right Pin
S van Leent13-Jul-02 4:36
S van Leent13-Jul-02 4:36 
GeneralRe: Not quite right Pin
Jason Henderson12-Jul-02 3:44
Jason Henderson12-Jul-02 3:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.