Click here to Skip to main content
Licence CPOL
First Posted 6 Mar 2007
Views 41,873
Bookmarked 26 times

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

By | 11 Jun 2010 | Article
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)

About the Author

AdamMartin

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General64 bit Compliance bug PinmemberDavid Nash4:29 9 Jun '10  
GeneralRe: 64 bit Compliance bug PinmemberAdamMartin17:31 9 Jun '10  
QuestionPlease... Pinmemberbluebillxp16:52 25 Mar '07  
GeneralNice, but... PinmemberMember #14064256:47 8 Mar '07  
GeneralRe: Nice, but... [modified] PinmemberAdamMartin7:15 8 Mar '07  
RantI disagree PinmemberS.H.Bouwhuis23:27 21 Jun '10  
NewsPosted part 2 PinmemberAdamMartin21:19 6 Mar '07  
Generalgood article PinmemberThatsAlok20:33 6 Mar '07  
GeneralYou lost some messages... Pinmemberchengang18:44 6 Mar '07  
GeneralRe: You lost some messages... PinmemberAdamMartin19:28 6 Mar '07  
GeneralRe: You lost some messages... Pinmember~MyXa~5:42 12 Mar '07  
Questionwhat can I benifit from mapping of WndProc to my specific class's WndProc Pinmembervisualcpp_codediscuss17:26 6 Mar '07  
AnswerRe: what can I benifit from mapping of WndProc to my specific class's WndProc PinmemberAdamMartin19:31 6 Mar '07  
AnswerRe: what can I benifit from mapping of WndProc to my specific class's WndProc PinmemberThatsAlok20:32 6 Mar '07  
AnswerRe: what can I benifit from mapping of WndProc to my specific class's WndProc PinmemberAdamMartin20:52 6 Mar '07  
GeneralIt works but ... Pinmembercmk15:01 6 Mar '07  
GeneralRe: It works but ... PinmemberAdamMartin19:24 6 Mar '07  
GeneralRe: It works but ... PinPopularmemberTomaž Štih20:41 6 Mar '07  
GeneralCool PinmemberPJ Arends11:56 6 Mar '07  

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.

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