如何检查 uicontextmenu 是否可见或活动
How to check if a uicontextmenu is visible or active
我有:
在 Matlab-GUI 中,我有一个 uicontextmenu 连接到一个绘图 (=axes)。如果我 "activate" 通过鼠标单击(右键),我可以使用通常的 "Callback" 来做一些事情,比如突出显示情节。如果用户 select 是菜单的 uimenu 元素之一,我可以使用此 uimenu 元素的回调并重置突出显示。
但是有个问题,如果用户没有select一个元素。上下文菜单消失了,如果发生这种情况,我找不到找到方法。在我的示例中,突出显示的图保持突出显示。
到目前为止我尝试了什么:
除了阅读文档之外,我还向一些 uimenu 元素的属性添加了监听器,例如:
addlistener(mymenu_element, 'Visible', 'PostSet', @mytest);
但是这个 属性,以及其他的,似乎在任何时候都不会被改变或触动 - 这让我有点吃惊 :o
所以问题是:
有没有一种方法可以在执行 uicontextmenu 之后执行一个函数(或者当上下文菜单 "disappears" 时,你怎么称呼它)?换句话说:如果用户没有 select 上下文菜单的元素,如何识别?
由于您无法收听这些项目(我已经 运行 进行了一些测试并得出了相同的结论)您可以通过以不同的方式创建和管理您的 uicontextmenu 来解决此问题:
function yourFunction
% create a figure
hFig = figure;
% add a listener to the mouse being pressed
addlistener ( hFig, 'WindowMousePress', @(a,b)mouseDown(hFig) );
end
function mouseDown(hFig)
% react depening on the mouse selection type:
switch hFig.SelectionType
case 'alt' % right click
% create a uicontext menu and store in figure data
hFig.UserData.uic = uicontextmenu ( 'parent', hFig );
% create the menu items for the uicontextmenu
uimenu ( 'parent', hFig.UserData.uic, 'Label', 'do this', 'Callback', @(a,b)DoThis(hFig) )
% assign to the figure
hFig.UIContextMenu = hFig.UserData.uic;
% turn visible on and set position
hFig.UserData.uic.Visible = 'on';
hFig.UserData.uic.Position = hFig.CurrentPoint;
% uicontext menu will appear as desired
% the next mouse action is then to either select an item or
% we will capture it below
otherwise
% if the uic is stored in the userdata we need to run the clean up
% code since the user has not clicked on one of the items
if isfield ( hFig.UserData, 'uic' )
DoThis(hFig);
end
end
end
function DoThis(hFig)
% Your code
disp ( 'your code' );
% Clean up
CleanUp(hFig);
end
function CleanUp(hFig)
% delete the uicontextmenu and remove the reference to it
delete(hFig.UserData.uic)
hFig.UserData = rmfield ( hFig.UserData, 'uic' );
end
我有:
在 Matlab-GUI 中,我有一个 uicontextmenu 连接到一个绘图 (=axes)。如果我 "activate" 通过鼠标单击(右键),我可以使用通常的 "Callback" 来做一些事情,比如突出显示情节。如果用户 select 是菜单的 uimenu 元素之一,我可以使用此 uimenu 元素的回调并重置突出显示。 但是有个问题,如果用户没有select一个元素。上下文菜单消失了,如果发生这种情况,我找不到找到方法。在我的示例中,突出显示的图保持突出显示。
到目前为止我尝试了什么:
除了阅读文档之外,我还向一些 uimenu 元素的属性添加了监听器,例如:
addlistener(mymenu_element, 'Visible', 'PostSet', @mytest);
但是这个 属性,以及其他的,似乎在任何时候都不会被改变或触动 - 这让我有点吃惊 :o
所以问题是:
有没有一种方法可以在执行 uicontextmenu 之后执行一个函数(或者当上下文菜单 "disappears" 时,你怎么称呼它)?换句话说:如果用户没有 select 上下文菜单的元素,如何识别?
由于您无法收听这些项目(我已经 运行 进行了一些测试并得出了相同的结论)您可以通过以不同的方式创建和管理您的 uicontextmenu 来解决此问题:
function yourFunction
% create a figure
hFig = figure;
% add a listener to the mouse being pressed
addlistener ( hFig, 'WindowMousePress', @(a,b)mouseDown(hFig) );
end
function mouseDown(hFig)
% react depening on the mouse selection type:
switch hFig.SelectionType
case 'alt' % right click
% create a uicontext menu and store in figure data
hFig.UserData.uic = uicontextmenu ( 'parent', hFig );
% create the menu items for the uicontextmenu
uimenu ( 'parent', hFig.UserData.uic, 'Label', 'do this', 'Callback', @(a,b)DoThis(hFig) )
% assign to the figure
hFig.UIContextMenu = hFig.UserData.uic;
% turn visible on and set position
hFig.UserData.uic.Visible = 'on';
hFig.UserData.uic.Position = hFig.CurrentPoint;
% uicontext menu will appear as desired
% the next mouse action is then to either select an item or
% we will capture it below
otherwise
% if the uic is stored in the userdata we need to run the clean up
% code since the user has not clicked on one of the items
if isfield ( hFig.UserData, 'uic' )
DoThis(hFig);
end
end
end
function DoThis(hFig)
% Your code
disp ( 'your code' );
% Clean up
CleanUp(hFig);
end
function CleanUp(hFig)
% delete the uicontextmenu and remove the reference to it
delete(hFig.UserData.uic)
hFig.UserData = rmfield ( hFig.UserData, 'uic' );
end