|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
相信大家都知道,MATLAB提供了一种非常方便的控制方式,利用ButtonDownFcn并配合Figure对象所提供的WindowButtonDownFcn(控制当鼠标有按键被单击时所执行的操作)、 WindowButtonMotionFcn ( 控制鼠标移动时所执行的操作)、 WindowButtonUpFcn(控制当鼠标被释放时所执行的操作),来完成鼠标控制的工作,下面给大家两个运用这些命令的小例子,以供大家参考!
1.WindowButtonDownFcn
当用户用鼠标在空白处点击时,出现欢迎对话框
-
- >> uicontrol(h,'style','text','position',[80,100,100,20],'string','请在空白处单击一下')
- >> h=figure ('color',[1 1 0],'position',[400 300 200 200],...
- 'name','Demo','menu','figure','WindowButtonDownFcn',...
- 'msgbox(''欢迎光临论坛'',''Window Message'',''help'')');
- >> uicontrol(h,'style','text','position',[80,100,100,20],'string',...
- '请任意单击一下')
复制代码
2.综合例子---实现画笔功能
程序代码1
-
- function mouse(action)
- switch action
- case 'start'
- %当光标移动时 执行'move'的操作?
- set(gcbf,'windowbuttonmotionfcn','mouse move');
- %当光标移动时 执行'stop'的操作?
- set(gcbf,'windowbuttonupfcn','mouse stop');
- case 'move'
- %获得当前鼠标的坐标
- point = get(gca,'CurrentPoint');
- %画出X与Y得坐标值
- line(point(:,1),point(:,2),'clipping','on',...
- 'erasemode','background','marker','o');
- case 'stop' %当鼠标键被释放时,不执行任何操作
- set(gcbf,'windowbuttonmotionfcn','');
- set(gcbf,'windowbuttonupfcn','');
- end
复制代码
-
- function varargout = matlab(varargin)
- % MATLAB M-file for matlab.fig
- % MATLAB, by itself, creates a new MATLAB or raises the existing
- % singleton*.
- %
- % H = MATLAB returns the handle to a new MATLAB or the handle to
- % the existing singleton*.
- %
- % MATLAB('CALLBACK',hObject,eventData,handles,...) calls the local
- % function named CALLBACK in MATLAB.M with the given input arguments.
- %
- % MATLAB('Property','Value',...) creates a new MATLAB or raises the
- % existing singleton*. Starting from the left, property value pairs are
- % applied to the GUI before painter_OpeningFunction gets called. An
- % unrecognized property name or invalid value makes property application
- % stop. All inputs are passed to matlab_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
- % Copyright 2002-2003 The MathWorks, Inc.
- % Edit the above text to modify the response to help matlab
- % Last Modified by GUIDE v2.5 03-Oct-2009 17:01:36
- % Begin initialization code - DO NOT EDIT
- gui_Singleton = 1;
- gui_State = struct('gui_Name', mfilename, ...
- 'gui_Singleton', gui_Singleton, ...
- 'gui_OpeningFcn', @matlab_OpeningFcn, ...
- 'gui_OutputFcn', @matlab_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 matlab is made visible.
- function matlab_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 matlab (see VARARGIN)
- % Choose default command line output for matlab
- handles.output = hObject;
- % Update handles structure
- guidata(hObject, handles);
- % UIWAIT makes matlab wait for user response (see UIRESUME)
- % uiwait(handles.figure1);
- % --- Outputs from this function are returned to the command line.
- function varargout = matlab_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 axes background.
- function axes1_ButtonDownFcn(hObject, eventdata, handles)
- % hObject handle to axes1 (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- mouse start
复制代码
步骤是需要利用GUIDE添加一个Static和一个 Text Axes,然后添加Callback函数即可,即可实现如下的画笔功能。 |
|