www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+util/showContextMenu.m

    function showContextMenu(hMenu, Loc, hComponent)
%showContextMenu Show a context menu at a specified location
%
%  showContextMenu(HMENU, LOC, HCOMP) shows the context menu HMENU at the
%  location LOC in the coordinate system of HCOMP.  If HCOMP is not
%  specified then the location is taken as being in the corrent coordinate
%  system already.

%  Copyright 2010-2013 The MathWorks, Inc.

if ~isempty(hMenu) && isgraphics(hMenu,'uicontextmenu')
    % Set the menu's position to the current point, defined by the Bottom Left
    % of the control + the event point within the control
    if nargin>2
        % Convert point [1 1] in the target component frame into our figure
        % ref frame in order to work out the current delta between them,
        % then remove that delta from the input location to bring it into
        % the figure ref frame.
        pt = mbcgui.util.convertLocation([1 1], hComponent);
        delta = (pt - [1 1]);
        Loc = Loc - delta;
    end
    
    % Execute the callback before showing menu
    menuCallback = get( hMenu, 'Callback' );
    if ~isempty( menuCallback )
        xregcallback( menuCallback, hMenu )
    end
    set(hMenu, 'Position', Loc, 'Visible', 'on');
end