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

    function Loc = convertLocation(Loc,hDest)
%CONVERTLOCATION Convert figure [x y] location to local coordinates.
%
%  LOC = CONVERTLOCATION(LOC, H) converts the [X Y] location pair (LOC) from
%  the coordinate system of the parent figure of H to the coordinates of H.
%  The converted location will always be in pixels.
%
%  Example: convert figure pointer location to uipanel coordinates
%
%     F = figure;
%     P = uipanel(F, 'Units', 'pixels');
%     PLoc = mbcgui.util.convertLocation(get(F, 'CurrentPoint'), P);

%  Copyright 2008-2013 The MathWorks, Inc.

hFig = ancestor(hDest, 'figure');
if hDest==hFig
    return
end

if numel(Loc)==2
    Loc = [Loc 10 10];
elseif any(Loc(3:4)==0)
    Loc(3:4) = 10;
end
    
% Convert the location to pixels in the figure
Loc = hgconvertunits(hFig, Loc, get(hFig, 'Units'), 'pixels', hFig);
Loc = Loc(1:2);

% Move the point into the pixel-coordinates of the destination
DestPos = getpixelposition(hDest, true);
Loc = Loc-DestPos(1:2)+1;