Re: Subclassing
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 01 Jun 2007 12:24:47 -0400
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:
Joseph M. Newcomer [MVP]
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
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Subclassing
- From: Joseph M . Newcomer
- Re: Subclassing
- Prev by Date: Re: Rounding of the double
- Next by Date: Re: Rounding of the double
- Previous by thread: Re: How to set focus to the previous app that had it?
- Next by thread: Re: Subclassing
- Index(es):
Relevant Pages
|