问题
I want mouse cursor changing when mouse is on a static text
(not clicking on it,only changing on the area of static text). I found these java cods in undocumented-matlab
:
jb = javax.swing.JButton;
jb.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
I copied these codes in CreareFcn
and ButtonDownFcn
of static text
but nothing changed and everything was as default. How can I do this and where should I put these codes in static text
?
Thanks.
回答1:
You can achieve this using a mouse motion listener as follows:
function init()
%// Initialize the figure with a listener:
h = figure('WindowButtonMotionFcn',@windowMotion,'Pos',[400,400,200,200]);
%// Add a "static" text label:
col = get(h,'color');
lbl = uicontrol('Style','text', 'Pos',[10,160,120,20], ...
'Background',col, 'HorizontalAlignment','left');
drawnow;
setptr(gcf, 'fleur'); %// Optional, set default pointer.
function windowMotion(varargin)
cursor_pos = get(h,'CurrentPoint');
set(lbl,'String',sprintf('Mouse position: %d, %d',cursor_pos));
drawnow;
pos = get(lbl,'position'); %// This doesn't need to happen every time,
%// it's here for the sake of demonstration.
if (cursor_pos(1)>pos(1) && cursor_pos(1)<pos(1)+pos(3)) && ...
(cursor_pos(2)>pos(2) && cursor_pos(2)<pos(2)+pos(4))
setptr(gcf, 'hand'); %// Change to this cursor if pointer is inside
%// the element.
else
setptr(gcf, 'fleur'); %//otherwise (re)change to default
end
end
end
Please note that instead of an if
, this could be a switch-case
type of selection (in case you want your cursor to change differently for different UI elements).
This code was based on this post on UndocumentedMatlab. You can find some more information on mouse pointer modification in MATLAB here.
Edit
To create this callback automatically in GUIDE see picture below. Please note that you will need to change lbl
to handles.tag_of_statictxt
and h
to your current figure's handle (that is usually returned by gcf
or gcbo
).

来源:https://stackoverflow.com/questions/25228843/change-mouse-courser-when-mouse-is-passing-the-static-text-in-matlab