当鼠标在MATLAB中传递静态文本时,更改鼠标进程 [英] Change mouse courser when mouse is passing the static text in MATLAB

查看:156
本文介绍了当鼠标在MATLAB中传递静态文本时,更改鼠标进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望鼠标在static text上时更改鼠标光标(不单击它,仅在静态文本区域上更改).我在undocumented-matlab中找到了这些Java鳕鱼:

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));

我将这些代码复制到了static textCreareFcnButtonDownFcn中,但没有任何变化,所有内容均作为默认值.我该怎么办?这些代码应放在static text的什么位置?

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?

谢谢.

推荐答案

您可以使用鼠标运动侦听器来实现此目标,如下所示:

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

请注意,这可能是switch-case类型的选择,而不是if(如果您希望光标针对不同的UI元素而有所不同).

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).

此代码基于这篇UndocumentedMatlab上的帖子.您可以在MATLAB 此处中找到有关鼠标指针修改的更多信息.

This code was based on this post on UndocumentedMatlab. You can find some more information on mouse pointer modification in MATLAB here.

要在GUIDE中自动创建此回调,请参见下图.请注意,您需要将lbl更改为handles.tag_of_statictxt,将h更改为当前图形的句柄(通常由gcfgcbo返回).

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).

这篇关于当鼠标在MATLAB中传递静态文本时,更改鼠标进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆