Reputation: 2436
A notification is sent by a control to its parent. When I subclass a control using SetWindowSubclass
, how can I handle the notifications? I don't want to handle them in the parent's window proc. Is there some thing I can do in subclass proc?
If I subclass a Edit control, how to handle EN_CHANGE
notification in the subclass?
Update
This is the subclass proc:
LRESULT CALLBACK MyEditWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
{
default:
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
}
I use
SetWindowSubclass(GetDlgItem(hWnd, ID_MYEDIT), MyEditWindowProc, 0, 0);
to subclass the Edit control.
But which message should I handle? Certainly not WM_NOTIFY
because it's handled by the parent.
Upvotes: 4
Views: 1789
Reputation: 14498
If I subclass a Edit control, how to handle EN_CHANGE notification in the subclass?
Short answer: you can't. If you subclass an edit, you only get to see the messages sent to it; you don't get to listen to or intercept the messages already sent by it (although you can add additional outgoing messages).
If you do need to do this, however, an alternative technique might be to create an intermediate window that wraps the edit, so that the original parent dialog has your wrapper as the child, and your wrapper has the edit as the child. Now your wrapper is positioned to intercept and filter messages going in either direction.
It will have to take care to manually forward all relevant messages, and handle resizing and other housekeeping issues; you get a bunch of that 'for free' with subclassing, but have to deal with it explicitly when wrapping. Also, since mouse/keyboard input will still go to the inner control; if you need to listen in on that, then subclassing will be necessary in addition to wrapping.
Upvotes: 4
Reputation: 37132
EN_CHANGE and the like are always sent to the parent window. You can't handle them in the sub-classed control because they aren't sent to the control, they're sent to the parent.
If you're looking for a way to do a self-contained sub-class of an edit control without having to add code to the parent's window procedure, and you want to handle those notification messages, the only way would be to sub-class the parent window as well.
Upvotes: 2
Reputation: 70701
As far as I know, there is no straightforward solution to this using just the Win32 API. Subclassing lets you process messages sent to a control, not sent by the control. Win32 notifications are sent by a control directly to the parent and I don't think you can change this behaviour.
MFC does something similar to what you want through a feature called message reflection -- the message is still sent to the parent window, but the parent looks for "reflection" handlers in the child window and manually calls these handlers.
You can write something similar to MFC reflection yourself, but it's going to take some effort, so if there's an easier alternative, you should go with that.
Upvotes: 3