概述
和C++平台一样,MATLAB也能在GUI设计中写入鼠标事件
**功能:**拖动鼠标左键时,在Axes区域画线,松开时不画,再次点击时画线…
少数几个需要用到的函数和属性
·WindowButtonDownFcn回调函数
·Selectiontype属性(判断单击/双击,左键/右键)
·Normal左键单击
·Alt右键单击(Ctrl+左键)
·Open双击
·Extend中键(Shift+左键)
由于Line对象的父对象为Axes,故而在GUI界面中添加一个Axes对象。
首先需要判断:
1:我们是否按下了鼠标左键
:2:然后在WindowsButtonMotionFcn回调函数中来选择是否绘制曲线。
由于WindowButtonFcn和WindowButtonMotionFcn是两个不同的回调函数,它们之间的信息需要共享(如判断鼠标是否按下),可以使用全局变量global …
打开WindowButtonFcn的回调函数,判断是否按下了鼠标左键,通过窗口的SelectionType属性判断鼠标左键的行为,以全局变量global将(是否按下了鼠标左键)的信息传递出去。
按下鼠标左键,SelectionType值为normal
为了下一次在移动鼠标时可以绘制坐标曲线,因此需要将当前鼠标左键移动的位置记录下来,可以使用Axes的CurrentPoint属性,并将该句柄pos1保存到全局变量(后续也会用到)
pos1 = get(handles.axes1,'CurrentPoint');
该回调函数的代码为:
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown ;
if strcmp(get(gcf,'SelectionType'),'normal')
ButtonDown = 1;
pos1 = get(handles.axes1,'CurrentPoint');
end
然后添加界面的鼠标移动响应函数
通过全局变量ButtonDown判断是否按下了鼠标左键,按下了为1,并记录当前鼠标的位置。
然后绘制一一条线宽为4的曲线
如下为绘图的第一个点:
linr([pos1(1,1) pos(1,1)],[pos1(1,2) pos(1,2)],'LineWidth',4);
在下一次响应鼠标的移动函数时,上次响应时鼠标所处的位置为绘画位置的初始点
pos1 = pos ;
然后在OpeningFcn中定义全局变量并且初始化为空
function Mouse_line_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Mouse_line (see VARARGIN)
global ButtonDown ps1
ButtonDown=[];
ps1=[];
% Choose default command line output for Mouse_line
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
运行结果:
能绘图,但是无法停下,鼠标移动,曲线跟着到处跑
原因:
没有设置松开鼠标时的回调函数
解决方法:
设置松开鼠标左键WindowButtonUpFcn的操作的回调函数
松开鼠标的回调函数:
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown
ButtonDown = 0;
运行结果:
附上全代码:
function varargout = Mouse_line(varargin)
% MOUSE_LINE MATLAB code for Mouse_line.fig
% MOUSE_LINE, by itself, creates a new MOUSE_LINE or raises the existing
% singleton*.
%
% H = MOUSE_LINE returns the handle to a new MOUSE_LINE or the handle to
% the existing singleton*.
%
% MOUSE_LINE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MOUSE_LINE.M with the given input arguments.
%
% MOUSE_LINE('Property','Value',...) creates a new MOUSE_LINE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Mouse_line_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Mouse_line_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Mouse_line
% Last Modified by GUIDE v2.5 22-Oct-2020 15:06:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Mouse_line_OpeningFcn, ...
'gui_OutputFcn', @Mouse_line_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Mouse_line is made visible.
function Mouse_line_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Mouse_line (see VARARGIN)
global ButtonDown ps1
ButtonDown=[];
ps1=[];
% Choose default command line output for Mouse_line
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Mouse_line wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Mouse_line_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown pos1;
if strcmp(get(gcf,'SelectionType'),'normal')
ButtonDown = 1;
pos1 = get(handles.axes1,'CurrentPoint');
end
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown pos1;
if ButtonDown==1
pos=get(handles.axes1,'CurrentPoint');
line([pos1(1,1) pos(1,1)],[pos1(1,2) pos(1,2)],'LineWidth',4);
pos1 =pos;
end
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown
ButtonDown = 0;
===OVER
最后
以上就是无心钥匙为你收集整理的Matlab 实现GUI界面相应鼠标事件的全部内容,希望文章能够帮你解决Matlab 实现GUI界面相应鼠标事件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复