Wednesday, August 23, 2006

Subclassing

Subclassing is something new I learnt today. To me it looked like a naughty method of inheritance. In simple term subclassing is a technique using which one can intercept the messages send to a window. Once captured these message can be processed, forwarded or cancelled. I call it a naughty method of inheritance since it allows us to tweak/add the functionality of existing windows/controls without rewriting from scratch.

Every window has a message processing function. What we need to do is that replace this with our message processing function. We do this by calling SetWindowLong function and setting the new WNDPROC.

In our managed code, we can derive a class from MessageWindow and override WndProc. This class has a hwnd property (the window handle). We can call this handle in our new message processing function (the one we make in native code), to pass messages to the managed code.

For more: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_subclas3.asp

I used it to capture the smartphone's "Escape" key.

No comments: