Click here to Skip to main content
11,705,617 members (48,534 online)
Click here to Skip to main content

A Simple Win32 Window Wrapper Class

, 11 Jul 2002 184.8K 5.9K 52
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Jason Henderson
Architect
United States United States
I have been a professional developer since 1996. I live in the middle of no where in Illinois, USA. I am married and have four children.

You may also be interested in...

Comments and Discussions

 
BugUnhandled Exception Pin
Captain Price29-Oct-13 20:46
professionalCaptain Price29-Oct-13 20:46 
QuestionI had to add "#include <Windows.h> Pin
trentgenmitrael23-Jun-13 20:41
membertrentgenmitrael23-Jun-13 20:41 
QuestionDoes it leak or not? Pin
masotta12-Feb-12 21:50
membermasotta12-Feb-12 21:50 
I do not know if I have to consider trinium's posting real or not.
Please tell me if those mentioned memory leaks are real or not.
I like the approach but I do not want to fight against hidden memory leaks if they are really there...

Thanks
Pat
AnswerRe: Does it leak or not? Pin
Jason Henderson13-Feb-12 3:53
memberJason Henderson13-Feb-12 3:53 
GeneralRe: Does it leak or not? Pin
masotta14-Feb-12 20:23
membermasotta14-Feb-12 20:23 
GeneralRe: Does it leak or not? Pin
masotta19-Feb-12 1:26
membermasotta19-Feb-12 1:26 
GeneralRe: Does it leak or not? Pin
Jason Henderson20-Feb-12 9:04
memberJason Henderson20-Feb-12 9:04 
GeneralRe: Does it leak or not? Pin
masotta20-Feb-12 19:21
membermasotta20-Feb-12 19:21 
GeneralMy vote of 5 Pin
Elveni12-Jun-11 8:17
memberElveni12-Jun-11 8:17 
Questionwhat are you.. 12 ?? Pin
trinium2-Aug-06 11:41
membertrinium2-Aug-06 11:41 
AnswerRe: what are you.. 12 ?? Pin
Jason Henderson2-Aug-06 17:05
memberJason Henderson2-Aug-06 17:05 
AnswerRe: what are you.. 12 ?? Pin
David Nash15-Sep-07 21:52
memberDavid Nash15-Sep-07 21:52 
QuestionWhy MS did'nt do it that way? Pin
Polite Programmer2-Aug-06 1:03
memberPolite Programmer2-Aug-06 1:03 
AnswerRe: Why MS did'nt do it that way? Pin
Member 109205799-Jul-14 20:25
memberMember 109205799-Jul-14 20:25 
GeneralGood idea Pin
Perry Zh11-Jul-05 23:05
memberPerry Zh11-Jul-05 23:05 
GeneralExcellent Pin
David Nash15-Feb-05 18:08
memberDavid Nash15-Feb-05 18:08 
GeneralRe: Excellent Pin
Jason Henderson16-Feb-05 3:29
memberJason Henderson16-Feb-05 3:29 
GeneralOops Pin
David Nash1-May-05 14:03
memberDavid Nash1-May-05 14:03 
GeneralOff Topic : This is really sick ! Pin
TomKat19-Dec-04 23:27
memberTomKat19-Dec-04 23:27 
GeneralYou are the sick one... Pin
Jason Henderson20-Dec-04 2:40
memberJason Henderson20-Dec-04 2:40 
GeneralRe: You are the sick one... Pin
TomKat20-Dec-04 6:34
memberTomKat20-Dec-04 6:34 
GeneralRe: You are the sick one... Pin
Jason Henderson20-Dec-04 6:54
memberJason Henderson20-Dec-04 6:54 
Generalmy project Pin
mehdic++18-Jul-04 0:05
membermehdic++18-Jul-04 0:05 
GeneralWont Compile, Cant get it to work Pin
drunkenZim2-May-04 10:15
memberdrunkenZim2-May-04 10:15 
GeneralRe: Wont Compile, Cant get it to work Pin
Jason Henderson2-May-04 19:01
memberJason Henderson2-May-04 19:01 
GeneralRe: Wont Compile, Cant get it to work Pin
Jason Henderson2-May-04 19:06
memberJason Henderson2-May-04 19:06 
GeneralRe: Wont Compile, Cant get it to work Pin
drunkenZim2-May-04 20:52
memberdrunkenZim2-May-04 20:52 
GeneralRe: Wont Compile, Cant get it to work Pin
Jason Henderson3-May-04 2:39
memberJason Henderson3-May-04 2:39 
GeneralRe: Wont Compile, Cant get it to work Pin
LonelyTower12-May-05 21:01
memberLonelyTower12-May-05 21:01 
GeneralMDI error!!!! Pin
mzmzmz12-Mar-04 17:43
membermzmzmz12-Mar-04 17:43 
Generalvery fine Pin
skaanji10-Oct-03 14:43
memberskaanji10-Oct-03 14:43 
Generalexactly what I needed! Pin
Scott Miller6-Jun-03 13:57
memberScott Miller6-Jun-03 13:57 
GeneralRe: exactly what I needed! Pin
Jason Henderson6-Jun-03 16:47
memberJason Henderson6-Jun-03 16:47 
GeneralRe: exactly what I needed! Pin
Groba10-Oct-06 2:49
memberGroba10-Oct-06 2:49 
Generalgrreat stuff, I got it working but... Pin
GnosticMan4-Mar-03 12:32
memberGnosticMan4-Mar-03 12:32 
Generalwindow Pin
noahb24-Feb-03 20:45
sussnoahb24-Feb-03 20:45 
GeneralJust some thoughts Pin
matsik5-Dec-02 0:17
membermatsik5-Dec-02 0:17 
GeneralRe: Just some thoughts Pin
Jason Henderson5-Dec-02 3:08
memberJason Henderson5-Dec-02 3:08 
GeneralRe: Just some thoughts Pin
GMorris23-May-05 4:06
memberGMorris23-May-05 4:06 
GeneralInteresting... Pin
Hockey19-Oct-02 18:10
memberHockey19-Oct-02 18:10 
GeneralRe: Interesting... Pin
skaanji10-Oct-03 14:48
memberskaanji10-Oct-03 14:48 
GeneralRe: Interesting... Pin
Hockey10-Oct-03 22:20
memberHockey10-Oct-03 22:20 
GeneralTake a look at jlWindows Pin
ukuk4-Oct-02 16:53
memberukuk4-Oct-02 16:53 
GeneralExcellent! Pin
Anonymous19-Sep-02 22:48
sussAnonymous19-Sep-02 22:48 
GeneralRe: Not quite right Pin
Jörgen Sigvardsson11-Jul-02 13:24
memberJörgen Sigvardsson11-Jul-02 13:24 
GeneralRe: Not quite right Pin
yarp11-Jul-02 19:52
memberyarp11-Jul-02 19:52 
GeneralRe: Not quite right Pin
Jason Henderson12-Jul-02 3:08
memberJason Henderson12-Jul-02 3:08 
GeneralRe: Not quite right Pin
S van Leent13-Jul-02 4:36
memberS van Leent13-Jul-02 4:36 
GeneralRe: Not quite right Pin
Jason Henderson12-Jul-02 3:44
memberJason Henderson12-Jul-02 3:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 12 Jul 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid