Click here to Skip to main content
13,150,178 members (60,486 online)
Click here to Skip to main content
Add your own
alternative version

Stats

65.4K views
32 bookmarked
Posted 6 Mar 2007

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

, 11 Jun 2010
Rate this:
Please Sign up or sign in to vote.
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.

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:

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.

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.

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)

Share

About the Author

AdamMartin
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
General64 bit Compliance bug Pin
David Nash9-Jun-10 4:29
memberDavid Nash9-Jun-10 4:29 
GeneralRe: 64 bit Compliance bug Pin
AdamMartin9-Jun-10 17:31
memberAdamMartin9-Jun-10 17:31 
QuestionPlease... Pin
bluebillxp25-Mar-07 16:52
memberbluebillxp25-Mar-07 16:52 
GeneralNice, but... Pin
Member #14064258-Mar-07 6:47
memberMember #14064258-Mar-07 6:47 
GeneralRe: Nice, but... [modified] Pin
AdamMartin8-Mar-07 7:15
memberAdamMartin8-Mar-07 7:15 
RantI disagree Pin
S.H.Bouwhuis21-Jun-10 23:27
memberS.H.Bouwhuis21-Jun-10 23:27 
NewsPosted part 2 Pin
AdamMartin6-Mar-07 21:19
memberAdamMartin6-Mar-07 21:19 
Generalgood article Pin
ThatsAlok6-Mar-07 20:33
memberThatsAlok6-Mar-07 20:33 
GeneralYou lost some messages... Pin
chengang6-Mar-07 18:44
memberchengang6-Mar-07 18:44 
GeneralRe: You lost some messages... Pin
AdamMartin6-Mar-07 19:28
memberAdamMartin6-Mar-07 19:28 
GeneralRe: You lost some messages... Pin
~MyXa~12-Mar-07 5:42
member~MyXa~12-Mar-07 5:42 
Questionwhat can I benifit from mapping of WndProc to my specific class's WndProc Pin
visualcpp_codediscuss6-Mar-07 17:26
membervisualcpp_codediscuss6-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
memberAdamMartin6-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
memberThatsAlok6-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
memberAdamMartin6-Mar-07 20:52 
GeneralIt works but ... Pin
cmk6-Mar-07 15:01
membercmk6-Mar-07 15:01 
GeneralRe: It works but ... Pin
AdamMartin6-Mar-07 19:24
memberAdamMartin6-Mar-07 19:24 
GeneralRe: It works but ... Pin
Tomaž Štih6-Mar-07 20:41
memberTomaž Štih6-Mar-07 20:41 
GeneralCool Pin
PJ Arends6-Mar-07 11:56
memberPJ 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 11 Jun 2010
Article Copyright 2007 by AdamMartin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid