Change mouse cursor when mouse passes static text to MATLAB

I want the mouse cursor to change when the mouse is on static text(without clicking on it, only changing the area of ​​static text). I found these java cod in undocumented-matlab:

jb = javax.swing.JButton;
jb.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

I copied these codes to CreareFcnand ButtonDownFcnfrom static text, but nothing changed, and everything was by default. How can I do this and where should I put these codes in static text?

Thank.

0
source share
1 answer

You can achieve this using the mouse movement 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 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

, if switch-case ( , , - ).

UndocumentedMatlab. MATLAB .

GUIDE, . . , lbl handles.tag_of_statictxt h ( gcf gcbo). How to create the callback automatically in GUIDE

+3

All Articles