收录日期:2020/10/25 07:02:06 时间:2010-09-07 02:37:03 标签:c++,wxwidgets,wxtextctrl

In wxWidgets, how can I detect when the cursor position changed in a wxTextCtrl? I looked at the manual and couldn't find any event relating to it, but maybe there is another way I've missed.

Since a wxTextCtrl inherits from wxWindow, you can bind the following mouse events with that control: http://docs.wxwidgets.org/2.8/wx_wxmouseevent.html#wxmouseevent

I think the one you're interested in is EVT_MOTION(func). However, I have never used this event so I can't say for sure if it is only raised on mouse motion inside the window it is binded to (which would be the more interesting approach) or global mouse movement.

I'm fairly certain he means the insertion point cursor, in which case, there isn't a wxWidgets event for changes in it's position. Additionally, while wxTextCtrl::SetInsertionPoint() and wxTextCtrl::SetInsertionPointEnd() are virtual methods that can be overwritten, this only catches situations where it's changed programmatically and not through various actions depending on the native platform involvement (which are likely why this event doesn't exist in wxWidgets currently).

The best solution I can think of to portably catch all insertion point changes right now is to continuously poll the insertion point position with a timer. You could derive your own wxTextCtrl with this functionality built in that generates your own custom event if you need this feature in more than one location.

My personal recommendation would be to look for an alternative solution to your problem that doesn't require knowing the insertion point position at all times. Are you sure that you actually need to know the insertion point position and not just when the value in the text control changes?