wxWidgets: Detecting click event on custom control

2019-07-24 08:37发布

How to add a click event listener to my custom control made with wxWidgets? The custom control uses wxWindow as the base. On the event list I see

wxEVT_LEFT_DOWN 
wxEVT_LEFT_UP 
wxEVT_LEFT_DCLICK
wxEVT_MIDDLE_DOWN
wxEVT_MIDDLE_UP 
wxEVT_MIDDLE_DCLICK
wxEVT_RIGHT_DOWN 
wxEVT_RIGHT_UP
wxEVT_RIGHT_DCLICK
wxEVT_MOTION
wxEVT_ENTER_WINDOW
wxEVT_LEAVE_WINDOW
wxEVT_MOUSEWHEEL 

But there is no wxEVT_LEFT_CLICK or similar.

2条回答
家丑人穷心不美
2楼-- · 2019-07-24 08:57

In the first instance I recommend inheriting from wxControl not wxWindow, wxControl is designed for that exact purpose and you are less likely to find yourself fighting the system. When I look at a control I am building in my own wxWidgets app, I see that my click handler is attached to wxEVT_LEFT_DOWN. Looking in my copy of Cross Platform GUI Programming with wxWidgets I can see a list of all wxMouseEvents, and there is no wxEVT_LEFT_CLICK. I would suggest wxEVT_LEFT_DOWN is the event to use.

Now after posting I've read Burly's answer and I agree with him, wxWidgets offers the lowest level events and that gives you the maximum amount of control of the user interface you construct for your users.

查看更多
祖国的老花朵
3楼-- · 2019-07-24 09:08

Typically, there is no "click" event (and in the case of wxWidgets - there isn't ). The action of clicking is broken into its two parts: Mouse Down and Mouse Up. Typically what you think of as a "left click" event is actually handled in a "left up" event.

Try it out:

  • Hover over a button (such as the "Add Comment" button this page)
  • Click the left-mouse button down and hold
  • Move the mouse off of the button while holding down
  • Release the left-mouse button
  • Nothing happens!

This time:

  • Hover over the same button
  • Click the left-mouse button down and hold
  • Release the left-mouse button
  • The "click" action you expect is triggered by the up event!
查看更多
登录 后发表回答