Matlab 實現介面相應滑鼠事件

學不好通訊的通訊孩紙發表於2020-10-23

和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

相關文章