Matlab 实现GUI界面相应鼠标事件
和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});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_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==1pos=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界面怎么用,MATLAB的GUI界面基础操作
今天我们来学习如何用MATLAB的GUI界面进行一些简单的操作,因为GUI界面可以很简单的制作界面,按钮系列,直接拖拽即可,而不需要用代码对界面进行编写,只需要对里面的具体功能进行编码,我们今天就运用 ...
- matlab制作GUI界面(1)
matlab制作GUI界面 概述 创建GUI界面 界面设置 静态文本 最后 概述 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的 ...
- 手写数字识别问题(2)——利用matlab搭建GUI界面
经过GUI的学习(详见博客:https://blog.csdn.net/didi_ya/article/details/105357279 ),小白逐渐了解了MATLAB的GUI界面及其搭建.下面是我 ...
- 基于MATLAB软件GUI界面的自制简单电子琴
基于MATLAB软件GUI界面的自制简单电子琴 概要: 本文基于MATLAB及其GUI界面设计了一个简单电子琴.利用MATLAB的GUI控件及相关算法,实现了一些简单的功能.这是作者大一暑期课程< ...
- 基于MATLAB软件GUI界面的可编程电音合成器软件
基于MATLAB软件GUI界面的可编程电音合成器软件 1 总体设计 1.1 设计思路 1.2 软件运行 2 软件功能说明 2.1 软件界面 2.1.1 菜单栏 2.1.2 工具栏 2.1.3 MySy ...
- MATLAB中GUI界面开发上位机
MATLAB是一个十分强大的工具,用来制作串口上位机也是不错的选择.虽然一般的上位机是用的C#或者C++编写的,这两者具有强大的系统操作能力或便捷灵活的通信和控制开发能力.但是对于我这种偏硬小白,ma ...
- PyQt5教程(十二)——实现QQ登录界面(六、实现鼠标拖动界面,鼠标事件)
PyQt5教程(十二)--实现QQ登录界面(五.实现鼠标拖动界面,鼠标事件) 一.实现界面可以随着鼠标进行拖动 1.主要就是对鼠标事件的实现: def mousePressEvent(self, ev ...
- MATLAB之GUI界面创建
初衷 由于课程要求,学习了下创建MATLAB交互界面,最初学习的途径就是在CSDN上搜索:然而参考了几篇博客文章之后,操作时还是一头雾水(拼死挣扎不愿承认自己能力太差:)),后来在同学的帮助下,成功完 ...
- matlab中gui界面如何保存为图片,如何保存MATLAB GUI界面中的图片
如何保存MATLAB GUI界面中的图片 代码来源自网络,作者未知,侵删. 原生支持png,bmp,jpg格式.另外自行添加了清晰度比较高的eps格式(荐). 新建一个按钮,按钮的代码如下. 其中第二 ...
最新文章
- 召唤超参调优开源新神器:集XGBoost、TensorFlow、PyTorch、MXNet等十大模块于一身...
- REPLICAT RORA_1保持 ABENDED状态,无法启动问题处理
- Matlab实用程序--图形应用-双y轴图形的绘制
- python深度学习环境支撑列表 各版本对应关系,Keras,TensorFlow,pytorch ,caffe等。
- 你和985硕博研究生,差了这些东西
- 练习-前程无忧数据爬取
- 前端笔试能查吗_鹅厂微博前端实习面经
- Linux - iptables
- 线程池原理_Java线程池实现原理
- Windows Azure 基本操作手册
- stc 串口收发 c语言,STC15W408AS串口通讯PC乱码
- AR和VR,有哪些知名的开源平台
- Intellij idea -1-解决报错:Error executing Maven. The specified user settings file does not exist: C:\Use
- Mybatis数据框架整合
- Ubuntu断点续存之wget
- 融云钜惠来袭,新客尝鲜首月 2.7 折起,超值套餐 6 折起
- Gym - 101853E E. Maximum Sum (状压DP)
- 移动硬盘\U盘在使用过程中0x80070570 文件或目录损坏且无法读取 CHKDSK 修复方法
- 第三方SIP 软电话xlite注册到CUCM
- 农村中学扩建工程全套CAD施工图哪里找?