Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle control's notifications when subclassing a control? Such as EN_CHANGE?

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.

like image 677
EFanZh Avatar asked Oct 15 '25 08:10

EFanZh


2 Answers

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.

like image 127
BrendanMcK Avatar answered Oct 16 '25 21:10

BrendanMcK


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.

like image 42
casablanca Avatar answered Oct 16 '25 21:10

casablanca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!