This is a mirror of official site: http://jasper-net.blogspot.com/

Simple Mapping of WndProc to your Specific Class' WndProc

| Monday, June 14, 2010
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.

Read more: Codeproject

Posted via email from .NET Info

0 comments: