是否可以在 WindowButtonMotionFcn 中使用多个回调? [英] Is it possible to use multiple callbacks in the WindowButtonMotionFcn?

查看:28
本文介绍了是否可以在 WindowButtonMotionFcn 中使用多个回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,为建筑图形添加功能.此类为 WindowMouseMotion 事件创建一个监听器;但是,要触发此事件,我必须为图形的 WindowButtonMotionFcn 属性添加一个虚拟回调函数.我首先检查此属性是否已填充.如果不是,那么我将它设置为一个什么都不做的虚拟回调函数.

I created a class which adds functionality to a figure on construction. This class creates a listener for the WindowMouseMotion event; however, to get this event to fire I had to add a dummy callback function for the figure's WindowButtonMotionFcn property. I first check if this property is already populated. If it isn't then I set it to a dummy callback function that does nothing.

我可以简单地将此虚拟回调添加到任何现有回调中,而不是检查该属性是否已设置?一个回调属性是否可以调用多个函数?

Instead of checking if the property is already set or not, can I simply add this dummy callback to any existing callbacks? Is it possible for a callback property to call multiple functions?

编辑

当使用handle.listener方法处理下面给出的WindowButtonMotionEvent事件时,一定要使用eventdata.CurrentPoint来访问当前鼠标位置.在以这种方式处理 WindowButtonMotionEvent 事件之前,图窗的 CurrentPoint 属性不会更新.

When using the handle.listener approach to handle the WindowButtonMotionEvent event given below, be sure to use eventdata.CurrentPoint to access the current mouse position. The CurrentPoint property of the figure does not get updated before handling the WindowButtonMotionEvent event in this manner.

推荐答案

A相关文章可以在 Yair Altman 的 Undocumented MATLAB 博客上找到,来自客座博主 Matt Whitaker.您所指的是回调链,并引自博客:

A related article can be found on Yair Altman's Undocumented MATLAB blog, from guest blogger Matt Whitaker. What you are alluding to is callback chaining, and quoting from the blog:

坦率地说,写过代码以前处理回调锁链,我宁愿戳自己用叉子在眼睛里!

Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!

幸运的是,该文章中似乎有替代解决方案.使用发布在那里的代码片段,我能够获得一个在鼠标移动时执行的函数而不必设置'WindowButtonMotionFcn'.我为当前图形添加了一个监听器,如下所示:

Luckily, there appears to be an alternative solution in that article. Using a snippet from the code posted there, I was able to get a function to execute on mouse movement without having to set the 'WindowButtonMotionFcn'. I added a listener to the current figure like so:

myListener = handle.listener(gcf,'WindowButtonMotionEvent',...
                             @(hSource,eventData) disp('hello'));

当我在窗口中移动鼠标时显示消息 hello.

And the message hello was displayed when I moved the mouse in the window.

这篇关于是否可以在 WindowButtonMotionFcn 中使用多个回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆