Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / C++

Simple Mapping of WndProc to your Specific Class' WndProc - Part 1

Rate me:
Please Sign up or sign in to vote.
3.78/5 (14 votes)
11 Jun 2010CPOL2 min read 88.6K   33   19
Map all WndProc messages to your class' message handlers. Simplest approach. Part 1.

Introduction

This is my first article, so please excuse any newbie-ness you might find.

I have been reading many articles on message mapping from the WndProc function to your own message handlers, and all articles required something either complex or just plain stupid. So, I went about finding a way to implement this in a very easy manner and with as little code as possible. What I came up with satisfies me greatly, and I hope it will satisfy you as well.

The Setup

First off, we need to create the window. I am not going to get into all the code required to do this since there are many other good articles describing each step and giving much better advice on this than I can.

What you need to do is just put a simple line of code into your creation function to allow this whole process to work.

C++
if (hwnd == NULL) {
    MessageBox("CreateWindowEx() Failed!", "Debug", NULL, MB_OK);
    return false;
}
    
// Adds a pointer to your current class to the WndClassEx structure

SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)this);

ShowWindow(hwnd, SW_NORMAL);
UpdateWindow(hwnd);

The important line here is:

C++
SetWindowLongPtr(HWND hWnd, int nIndex, LONG_PTR dwNewLong);

All we do is pass in the handle to the window we just created (hwnd, in this case), give it the flag of the parameter we want to change (GWLP_USERDATA, in this case), and finally, the pointer of the class ((LONG_PTR)this - the type cast is required because of the function prototype). This function will allow us to retrieve the pointer to the class at a later time.

The WndProc

Now that we have the pointer saved, we can recover it and use it to reroute our messages.

C++
LRESULT CALLBACK Win::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {    
    // Recover the pointer to our class, don't forget to type cast it back

    CWin* winptr = (CWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    // Check if the pointer is NULL and call the Default WndProc

    if (winptr == NULL) {
        return DefWindowProc(hwnd, message, wParam, lParam);
    } else {
        // Call the Message Handler for my class (MsgProc in my case)

        return winptr->MsgProc(hwnd, message, wParam, lParam); 
    }
}

This may look like a lot, but it is all quite simple code. First, we call GetWindowLongPtr(HWND hWnd, int nIndex) to recover the pointer to our class. Don't forget that since it is stored as a long*, we must type cast it back to that of our class. Now, we just check to make sure the pointer is not NULL. If it is, call the default WndProc. If it isn't NULL, call our message handler function from our class. You can make the function in your class whatever prototype you want and pass it whatever parameters you want. Just remember to have a return type of LRESULT and return the result of your function. As an example, here is my MsgProc function.

C++
virtual LRESULT MsgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case (WM_CLOSE):
            DestroyWindow(hwnd);
            return 1;
            break;
        case (WM_DESTROY):
            if (!GetParent(hwnd)) {
                PostQuitMessage(0);
            }
            return 1;
            break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

I have my function as virtual to allow me to inherit it down my class hierarchy, but this is not necessary. I do have the default case return the default WndProc as is usual for a WndProc.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General64 bit Compliance bug Pin
David Nash9-Jun-10 4:29
David Nash9-Jun-10 4:29 
GeneralRe: 64 bit Compliance bug Pin
AdamMartin9-Jun-10 17:31
AdamMartin9-Jun-10 17:31 
QuestionPlease... Pin
bluebillxp25-Mar-07 16:52
bluebillxp25-Mar-07 16:52 
GeneralNice, but... Pin
Endurion_8-Mar-07 6:47
Endurion_8-Mar-07 6:47 
GeneralRe: Nice, but... [modified] Pin
AdamMartin8-Mar-07 7:15
AdamMartin8-Mar-07 7:15 
RantI disagree Pin
S.H.Bouwhuis21-Jun-10 23:27
S.H.Bouwhuis21-Jun-10 23:27 
NewsPosted part 2 Pin
AdamMartin6-Mar-07 21:19
AdamMartin6-Mar-07 21:19 
Generalgood article Pin
ThatsAlok6-Mar-07 20:33
ThatsAlok6-Mar-07 20:33 
GeneralYou lost some messages... Pin
count06-Mar-07 18:44
count06-Mar-07 18:44 
GeneralRe: You lost some messages... Pin
AdamMartin6-Mar-07 19:28
AdamMartin6-Mar-07 19:28 
GeneralRe: You lost some messages... Pin
~MyXa~12-Mar-07 5:42
~MyXa~12-Mar-07 5:42 
Questionwhat can I benifit from mapping of WndProc to my specific class's WndProc Pin
code_discuss6-Mar-07 17:26
code_discuss6-Mar-07 17:26 
AnswerRe: what can I benifit from mapping of WndProc to my specific class's WndProc Pin
AdamMartin6-Mar-07 19:31
AdamMartin6-Mar-07 19:31 
AnswerRe: what can I benifit from mapping of WndProc to my specific class's WndProc Pin
ThatsAlok6-Mar-07 20:32
ThatsAlok6-Mar-07 20:32 
AnswerRe: what can I benifit from mapping of WndProc to my specific class's WndProc Pin
AdamMartin6-Mar-07 20:52
AdamMartin6-Mar-07 20:52 
GeneralIt works but ... Pin
cmk6-Mar-07 15:01
cmk6-Mar-07 15:01 
GeneralRe: It works but ... Pin
AdamMartin6-Mar-07 19:24
AdamMartin6-Mar-07 19:24 
GeneralRe: It works but ... Pin
Tomaž Štih6-Mar-07 20:41
Tomaž Štih6-Mar-07 20:41 
GeneralCool Pin
PJ Arends6-Mar-07 11:56
professionalPJ Arends6-Mar-07 11:56 

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.