Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

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

3.78/5 (14 votes)
11 Jun 2010CPOL2 min read 1  
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)