zhenghui 发表于 2009-10-11 09:49

Matlab鼠标控制的两个GUI例子(附代码)

相信大家都知道,MATLAB提供了一种非常方便的控制方式,利用ButtonDownFcn并配合Figure对象所提供的WindowButtonDownFcn(控制当鼠标有按键被单击时所执行的操作)、WindowButtonMotionFcn ( 控制鼠标移动时所执行的操作)、 WindowButtonUpFcn(控制当鼠标被释放时所执行的操作),来完成鼠标控制的工作,下面给大家两个运用这些命令的小例子,以供大家参考!

1.WindowButtonDownFcn

当用户用鼠标在空白处点击时,出现欢迎对话框


>> uicontrol(h,'style','text','position',,'string','请在空白处单击一下')
>> h=figure ('color',,'position',,...
      'name','Demo','menu','figure','WindowButtonDownFcn',...
      'msgbox(''欢迎光临论坛'',''Window Message'',''help'')');
>> uicontrol(h,'style','text','position',,'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
= 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函数即可,即可实现如下的画笔功能。

ChaChing 发表于 2009-10-12 01:03

回复 楼主 zhenghui 的帖子

不错, 是原创吗?
若是请注明!

[ 本帖最后由 ChaChing 于 2009-10-12 15:02 编辑 ]

ChaChing 发表于 2009-10-12 15:15

第一例顺序有误?!
页: [1]
查看完整版本: Matlab鼠标控制的两个GUI例子(附代码)