Win32 Subclassing
Posted: Thu Mar 27, 2003 8:42 am
Hi all,
Ok, before I start I want to say that I have asked this question in other places and have been told that I am misunderstanding window subclassing, but I am convinced that what I am saying is correct, anyway, I know this forum and respect most of you guys here so I know that I can find the truth here.
Anyway, lets set the senario, say I have written some wrapper classes for some win32 common controls. Each control needs to be self sufficient so that it can handle it events automatically without needing the programmer tp pass information to it from the parent window. So in a program there are three common controls, lets say a status bar, tool bar and tree view. As Each control is created it subclasses the parent procedure so it can automatically handle notification events. So lets say we create the status bar, it subclasses the parent and sets its own procedure in place of the original one, then we create the tool bar, it also subclasses the parent, but instead of replacing the original procedure it actually replaces the staus bars procedure, because the status bar already replaced the original one, so now we have a chain or window procedures, the tool bars procedure is called by windows, it does what it needs to do then it passes the messages onto the status bar procedure which does what it needs to do and then the status bar passes messages onto the original procedure. Now say we create the tree view control, it subclasses the parent, but actually it replaces the tool bars procedure, so the chain now starts with the tree view, goes to the tool bar, then to the status bar and finally back to the original procedure.
Ok, so everything is going good, no problems.
But now lets say the program, being dynamic in nature, no longer needs the status bar, so the status bar is destroyed, but before it is destroyed the wrapper classes destructor puts what it believes to be the original window procedure (and in this example it actually is the original) back where it got it from, but the problem is that the tool bar and tree view are behind it in the chain, so the status bar will restore the orignal procedure effectively cutting out the tool bar and tree view controls.
To make sure I am clear I will give a code example
// Create the main window
parentwnd = CreateWindow(....);
// Instantiate and Create the Status Window
CStatusBar status;
status.Create(parentwnd, ....);
// Inside of this create method we do this
OldWndProc = (WNDPROC)SetWindowLong(parentwnd, GWL_WNDPROC, (LONG)statusproc)
// This remembers the original proc
// Instantiate and Create the Tool bar Window
CToolBar tool;
tool.Create(parentwnd, ....);
// Inside of this create method we do this
OldWndProc = (WNDPROC)SetWindowLong(parentwnd, GWL_WNDPROC, (LONG)toolproc)
// This remembers the statusproc
// Instantiate and Create the Tree view Window
CTreeView tree;
tree.Create(parentwnd, ....);
// Inside of this create method we do this
OldWndProc = (WNDPROC)SetWindowLong(parentwnd, GWL_WNDPROC, (LONG)treeproc)
// This remembers the toolproc
So the wndproc chain goes like this
originalproc
we create the status window, chain goes like this
statusproc
originalproc
we create the tool window, chain goes like this
toolproc
statusproc
originalproc
we create the tree window, chain goes like this
treeproc
toolproc
statusproc
originalproc
Now if I destroy the status window it does this
SetWindowLong(parentwnd, GWL_WNDPROC, (long)OldWndProc) // which is originalproc
remember the staus window remembered originalproc
after this the chain goes like this
originalproc
we have effectively cut out the tool and tree windows.
So my question to you is, how can I solve this using good programming practice??
your help is appreciated,
thankyou.
Ok, before I start I want to say that I have asked this question in other places and have been told that I am misunderstanding window subclassing, but I am convinced that what I am saying is correct, anyway, I know this forum and respect most of you guys here so I know that I can find the truth here.
Anyway, lets set the senario, say I have written some wrapper classes for some win32 common controls. Each control needs to be self sufficient so that it can handle it events automatically without needing the programmer tp pass information to it from the parent window. So in a program there are three common controls, lets say a status bar, tool bar and tree view. As Each control is created it subclasses the parent procedure so it can automatically handle notification events. So lets say we create the status bar, it subclasses the parent and sets its own procedure in place of the original one, then we create the tool bar, it also subclasses the parent, but instead of replacing the original procedure it actually replaces the staus bars procedure, because the status bar already replaced the original one, so now we have a chain or window procedures, the tool bars procedure is called by windows, it does what it needs to do then it passes the messages onto the status bar procedure which does what it needs to do and then the status bar passes messages onto the original procedure. Now say we create the tree view control, it subclasses the parent, but actually it replaces the tool bars procedure, so the chain now starts with the tree view, goes to the tool bar, then to the status bar and finally back to the original procedure.
Ok, so everything is going good, no problems.
But now lets say the program, being dynamic in nature, no longer needs the status bar, so the status bar is destroyed, but before it is destroyed the wrapper classes destructor puts what it believes to be the original window procedure (and in this example it actually is the original) back where it got it from, but the problem is that the tool bar and tree view are behind it in the chain, so the status bar will restore the orignal procedure effectively cutting out the tool bar and tree view controls.
To make sure I am clear I will give a code example
// Create the main window
parentwnd = CreateWindow(....);
// Instantiate and Create the Status Window
CStatusBar status;
status.Create(parentwnd, ....);
// Inside of this create method we do this
OldWndProc = (WNDPROC)SetWindowLong(parentwnd, GWL_WNDPROC, (LONG)statusproc)
// This remembers the original proc
// Instantiate and Create the Tool bar Window
CToolBar tool;
tool.Create(parentwnd, ....);
// Inside of this create method we do this
OldWndProc = (WNDPROC)SetWindowLong(parentwnd, GWL_WNDPROC, (LONG)toolproc)
// This remembers the statusproc
// Instantiate and Create the Tree view Window
CTreeView tree;
tree.Create(parentwnd, ....);
// Inside of this create method we do this
OldWndProc = (WNDPROC)SetWindowLong(parentwnd, GWL_WNDPROC, (LONG)treeproc)
// This remembers the toolproc
So the wndproc chain goes like this
originalproc
we create the status window, chain goes like this
statusproc
originalproc
we create the tool window, chain goes like this
toolproc
statusproc
originalproc
we create the tree window, chain goes like this
treeproc
toolproc
statusproc
originalproc
Now if I destroy the status window it does this
SetWindowLong(parentwnd, GWL_WNDPROC, (long)OldWndProc) // which is originalproc
remember the staus window remembered originalproc
after this the chain goes like this
originalproc
we have effectively cut out the tool and tree windows.
So my question to you is, how can I solve this using good programming practice??
your help is appreciated,
thankyou.