Matlab学习-------GUI鼠标事件响应(鼠标划线实例)

(1)打开GUIDE,添加一个坐标轴并保存

(2)添加鼠标响应事件:鼠标按下事件,鼠标运动事件,鼠标松开事件

(3)对相应事件编写程序

function varargout = guide_m(varargin)
% GUIDE_M MATLAB code for guide_m.fig
%      GUIDE_M, by itself, creates a new GUIDE_M or raises the existing
%      singleton*.
%
%      H = GUIDE_M returns the handle to a new GUIDE_M or the handle to
%      the existing singleton*.
%
%      GUIDE_M('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIDE_M.M with the given input arguments.
%
%      GUIDE_M('Property','Value',...) creates a new GUIDE_M or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guide_m_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guide_m_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 guide_m

% Last Modified by GUIDE v2.5 25-Aug-2014 09:00:37

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guide_m_OpeningFcn, ...
                   'gui_OutputFcn',  @guide_m_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 guide_m is made visible.
function guide_m_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 guide_m (see VARARGIN)

% Choose default command line output for guide_m
handles.output = hObject;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%定义两个全局变量
global ButtonDown pos1;
ButtonDown =[];
pos1=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes guide_m wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = guide_m_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'))%判断鼠标按下的类型,mormal为左键
    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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

(4)运行测试(按下鼠标,移动鼠标划线)

(5)部分函数方法介绍

Matlab回调函数

SelectionType

时间: 2024-10-22 16:47:50

Matlab学习-------GUI鼠标事件响应(鼠标划线实例)的相关文章

DuiLib事件分析(一)——鼠标事件响应

最近在处理DuiLib中自定义列表行元素事件,因为处理方案得不到较好的效果,于是只好一层一层的去剥离DuiLib事件是怎么来的,看能否在某一层截取消息,自己重写. 我这里使用CListContainerElementUI行元素,元素中有插入button,平时行元素不显示,鼠标移动上去显示出来,鼠标移走就隐藏button.Duilib自己是不带这个功能的,它有一个鼠标移动上去的热点事件,按理说重写热点事件就好了.但是当时比较急没找到怎么触发的,之后一直没继续走这条思路.后来找到源码事件里面有 vo

Matlab学习----------GUI数据管理

向gui handles结构体中添加新的字段: hbtn=uicontrol('tag','mybtn',... 'style','pushbutton',...%普通的按钮 'callback',{@mybtn_Callback,handles},...%回调函数 'string','用户自定义的按钮',...%按钮上的内容 'units','normalized',... 'position',[0.45 0.5 0.2 0.1]); handles.mybtn=hbtn; handles.

CSS+元素,鼠标事件触发鼠标模形变成手状的形状

|| 版权声明:本文为博主原创笔记,未经博主允许不得转载. CSS+元素,鼠标事件触发鼠标模形变成手状的形状,以及其他样式. 方案一:使用CSS样式改变,鼠标移动到元素上显示手状. 1 cursor:pointer; 方案二:使用JS触发事件改变原样式:鼠标事件onmouseover(鼠标移动到元素上触发事件)触发时设置样式 1 // 使用在元素的标签上的事件 2 // 第一种方式 3 onmouseover="this.style.cursor='mouseHand'" 4 5 //

Matlab学习---------GUI键盘响应事件的学习

键盘响应事件的学习主要知识: 实例:创建一个GUI,添加两个输入框,一个按钮,实现输入数据之后点击按钮进行验证,验证成功之后关闭当前界面打开新的界面: (1)创建一个新的GUI界面:(添加组件并修改属性) (2)右键按钮添加点击事件响应函数: (3)右键界面,添加键盘按键响应函数: function varargout = gui_key(varargin) % GUI_KEY MATLAB code for gui_key.fig % GUI_KEY, by itself, creates a

js拖拽——将鼠标事件响应范围扩大到整个系统桌面

起因: 最近在做一个可拖拽的拓扑图,遇到的这个问题:如果执行拖拽操作的时候鼠标拖动很快,可能会出现鼠标脱离页面乃至浏览器的范围,如果这时松开鼠标按键,那么将不能响应鼠标的onmouseup事件,从而导致onmouseup和onmousemove事件不能被释放掉. 对于这个问题,网上很多相似的文章写了解决办法,但都有些毛病,我在这里总结归纳一下. 解决办法: 方案一:针对ie和火狐浏览器 ie浏览器和Firefox提供了setCapture 和 releaseCapture函数来解决该问题.简单写

c#全局鼠标事件以及鼠标事件模拟

最近在编写Max插件时,其主容器FlowLayoutPanel由于隐藏了滚动条,要实现按住鼠标中键上下拖动的功能,因此尝试了全局鼠标事件.以及鼠标勾子,可惜由于Max不争气?都未能实现,于是代码报废,故将其分享于此. 一.全局鼠标事件,首先构建鼠标事件处理器 public delegate void MouseMovedEvent(); public delegate void MouseMDownEvent(); public delegate void MouseMUpEvent(); pu

鼠标事件之鼠标滑过事件MOUSEOVER

来源地址:http://www.g2room.com/jquery/index.php?p=example%2Fevent%2Fmouseover.html&n=%E9%BC%A0%E6%A0%87%E4%BA%8B%E4%BB%B6%E4%B9%8B%E9%BC%A0%E6%A0%87%E6%BB%91%E8%BF%87%E4%BA%8B%E4%BB%B6MOUSEOVER var i = 0; // 定义当事件触发后执行的方法 function showContent (event){ $(

WPF,强制捕获鼠标事件,鼠标移出控件外依然可以执行强制捕获的鼠标事件

在WPF中,只有鼠标位置在某个控件上的时候才会触发该控件的鼠标事件.例如,有两个控件都注册了MouseDown和MouseUp事件,在控件1上按下鼠标,不要放开,移动到控件2上再放开.在这个过程中,控件1只会触发MouseDown事件,而控件2则只会触发MouseUp事件,鼠标不在控件上他们就收不到对应的鼠标事件.同样的如果某个控件注册了MouseMove事件,当鼠标移动到控件外之后,控件将不会接收到MouseMove事件.但是在很多情况下我们需要在鼠标移动到控件外之后还能接收鼠标事件.例如按住

关于CMainFrm不接收鼠标事件响应原因

CMainFrm即主框架窗口对鼠标的左键和右键在OnLButtonDown中无响应 解决方案: 1.在OnNcLButtonDown中响应.(即非客户区中响应) 参考文献:http://www.codes51.com/itwd/2705919.html 原文地址:https://www.cnblogs.com/zhuluqing/p/9001139.html