Re: Subclassing



This can be interpreted as follows:

If you use raw Window subclassing, you are replacing the function pointer that is normally
used by a pointer to your own function. However, you have only replaced that one pointer.

Suppose I create a subclass with 2*sizeof(LPVOID) window-extra bytes, which for purposes
of this discussion we will say is in Win32 and therefore would be 8 bytes.

My class handler uses those 8 bytes to hold pointers of my devising.

Now you come along and want to extend my class by adding your own handler in the chain.
You use either instance subclassing or global subclassing. However, since you are
extending my class, you are stuck with the extra bytes I provided. You can't add more,
and you can't use the bytes I supplied unless you use them in a way consistent with
whatever I am doing.

In a C sense, what I have is a struct:
struct MyStuff { LPVOID a; LPVOID b; }

if you want to use my struct, you cannot change the meaning of a or b, or use them in ways
that would affect my module. You can create
struct YourStuff {
MyStuff stuff;
int x;
int y;
};

but you can't change my struct. In the case of a window, it is not possible to extend the
implied struct that the class-extra or window-extra bytes represent.

So the way to add new things of your own to a class is to use SetProp. For example, when
I did a custom subclass in Win32, I would do something like

SetProp(hwnd, _T("WndProc"), (HANDLE)GetWindowLong(hwnd, GWL_WNDPROC));
SetWindowLong(hwnd, GWL_WNDPROC, myproc);

to call it, I would do
LRESULT myproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{ /* msg */
case WM_NCDESTROY:
RemoveProp(hwnd, _T("WndProc"));
break;
... stuff
} /* msg */
WNDPROC proc = (WNDPROC)GetProp(hWnd, _T("WndProc"));
if(proc != NULL)
return CallWindowProc(proc, hwnd, msg, wParam, lParam);
return DefWindowProc(hwnd, msg, wParam, lParam);
}

If you need to store anything else, such as a struct of your own, you can use
SetProp/GetProp/RemoveProp to manage the additional space.
joe

On Fri, 1 Jun 2007 15:46:45 +0000 (UTC), tom wiesner wrote:


Also can somebody better explain or extend the following statement made at
the below link?

Global subclassing has the same limitations as instance subclassing. The
application should not attempt to use the extra bytes for either the class
or the window instance unless it knows exactly how the original window procedure
is using them. If data must be associated with a window, the window's properties
list should be used in the same way as in instance subclassing.

Tom

Hello ,

I wish to know subclassing in win32/mfc/atl. Please provide links to
the same.

The below is the excerpt from MSDN at
http://msdn2.microsoft.com/en-us/library/ms997565.aspx

Microsoft suggests that you not subclass the control classes. Windows
is responsible for the controls it supplies, and aspects of the
controls might change from one version of Windows to the next. If your
application must subclass a control supplied by Windows, it may need
to be updated when a new version of Windows is released.

I wish to know whether the above imply that I cannot subclass an edit
control even though i know that I have done so by deriving the
subclassed class from CEdit?

Tom


Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Notification when a window is subclassed
    ... Java has a single window, on which it paints all the images that look like buttons, etc., ... I have a Java UI application that hosts a few third-party COM/ ... An ActiveX control that currently has the focus ... Note that whomever is subclassing it has failed to properly call the superclass (which is ...
    (microsoft.public.vc.mfc)
  • Re: Write onto Active-X Control
    ... Here's an example that demonstrates subclassing in VB including managing the WM_PAINT message: ... The way Windows works is using the principle of windows which encapsulate the functionality of whatever the window ... however since there are hundreds of messages that the window may potentially need to respond to, there is a default window proc call ... Since VB5 we've had the AddressOf operator which returns a pointer to the function enabling us to use API calls the require function ...
    (microsoft.public.vb.winapi.graphics)
  • Re: Ellipse() on XP
    ... You might want to get a good book about Windows programing and read about both subclassing and superclassing because you are apparently not familiar with these terms. ... This doesn't influence behaviour of a base class so it is *not* "Subclassing an entire window class" as you say. ... You notice that on Win32 systems this will change behaviour for each window of that class within the calling process only. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Subclassing
    ... this has nothing to do with MFC subclassing and therefore is ... If you use raw Window subclassing, you are replacing the function pointer that is normally ... what I have is a struct: ... Microsoft suggests that you not subclass the control classes. ...
    (microsoft.public.vc.mfc)
  • Re: Random AccessViolationExceptions
    ... A popular mistake is subclassing a window with a dialog procedure pointing in a DLL, then having the DLL unloaded somehow, resulting in a call to invalid memory. ... using (WaitOper waitOper = new WaitOper()) { ... No. Managed code cannot cause access violations, unless you use P/Invoke or unsafe operations, or the framework itself contains a bug. ...
    (microsoft.public.dotnet.languages.csharp)