我创建了一个类,它为构造图添加了功能。此类为 WindowMouseMotion
事件创建一个监听器;但是,为了触发此事件,我必须为图形的 WindowButtonMotionFcn
属性添加一个虚拟回调函数。我首先检查此属性是否已填充。如果不是,那么我将它设置为一个不执行任何操作的虚拟回调函数。
我可以不检查属性是否已经设置,而是简单地将这个虚拟回调添加到任何现有回调中吗?回调属性是否可以调用多个函数?
编辑
当使用 handle.listener
方法处理下面给出的 WindowButtonMotionEvent
事件时,一定要使用 eventdata.CurrentPoint
访问当前鼠标位置。在以这种方式处理 WindowButtonMotionEvent
事件之前,图窗的 CurrentPoint
属性不会更新。
最佳答案
A related article可以在客座博主 Matt Whitaker 的 Yair Altman 未记录的 MATLAB 博客上找到。您指的是回调链,并引用博客:
Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!
幸运的是,在那篇文章中似乎有一个替代解决方案。使用此处发布的代码片段,我能够获得在鼠标移动时执行的函数,而无需设置 'WindowButtonMotionFcn'
。我像这样向当前图形添加了一个监听器:
myListener = handle.listener(gcf,'WindowButtonMotionEvent',...
@(hSource,eventData) disp('hello'));
当我在窗口中移动鼠标时显示消息 hello
。
关于matlab - 是否可以在 WindowButtonMotionFcn 中使用多个回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116969/