Win32API: How to determine if EN_CHANGE was due to user action, not software action?

I find that this situation arises from time to time, and I have never had a truly reliable universal solution.

I have a control - in this example, an EDIT control in a dialog box. I want to take action in response to the user - and only the user - modifies the contents of the edit control.

The edit control can be installed programmatically - for example. when the dialog is configured, there may be an initial value placed in the edit box. Or, when the user selects an item from the list, this selection text can be placed in the edit box.

But when the user changes the contents of the edit field, I need to know this and respond (in this case, I want to remove the selection from the corresponding list).

I am currently looking at what control has focus, and only considering that EN_CHANGE is "from the user" if the edit control has focus.

This works fine under Windows 7. This one does not work under XP (I have not tested Vista yet).

In XP, if the edit box has focus, but the user clicks on the list view and in the list view the control edits its contents, then I get a notification from the edit control that claims that there is still focus (:: GetFocus () = = HWND edit control). But this incorrect state does not occur in Win7.

This is a multi-level interface, so I cannot change the notification handler as a list. He receives a change of choice and updates the edit field without my involvement or the ability to really intervene, except to receive notifications from both of them.

Any thoughts on how to solve the puzzle β€œIs this a control notification from the user” for a long time?

+4
source share
2 answers

You can always keep track of LVM_ITEMCHANGING , LVM_ITEMCHANGED and EN_MSGFILTER . If the editing window changes between LVM_ITEMCHANGING and LVM_ITEMCHANGED without EN_MSGFILTER between them, you can assume that the user has not changed the item. Or just check if there are any items selected when EN_CHANGE triggered, and if not or the text does not match the selected item, suppose this is a custom edit.

Or use ES_MULTILINE (from EN_CHANGE documentation):

The EN_CHANGE notification is not sent when the ES_MULTILINE style is used and the text is sent via WM_SETTEXT.

+1
source

I would suggest using the correct message. EN_CHANGE is too general, you want to know if the user typed text or nested text. So why not subclass control and watch WM_KEYPRESS messages?

In addition, you can set the flag in your other code that sets the edit control content. You could assume that everything your repeat wndproc member does is a software change.

You are not looking for something really safe, are you? If you just want to exclude given content requests that are simple enough. If you want to distinguish between user action and software modeling of user keystrokes, this is much more complicated.

+1
source

Source: https://habr.com/ru/post/1300439/


All Articles