Matlab 實現介面相應滑鼠事件
和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
相關文章
- [VC] 滑鼠事件的響應事件
- jquery實現的右鍵滑鼠點選事件jQuery事件
- jQuery滑鼠雙擊事件簡單介紹jQuery事件
- Qt Creator中滑鼠鍵盤事件的處理實現自定義滑鼠指標QT事件指標
- 滑鼠事件事件
- 滑鼠右鍵點選事件簡單介紹事件
- jQuery模擬實現滑鼠點選事件程式碼例項jQuery事件
- 滑鼠拖拽事件事件
- 差分進化演算法介紹及matlab實現演算法Matlab
- RBF神經網路簡單介紹與MATLAB實現神經網路Matlab
- angular 的滑鼠事件Angular事件
- 監聽滑鼠事件事件
- 滑鼠、鍵盤事件事件
- 事件 滑鼠事件 表單事件 from表單事件
- javaScript事件(五)事件型別之滑鼠事件JavaScript事件型別
- js實現的相容所有瀏覽器的滑鼠中鍵滾動事件JS瀏覽器事件
- 手持裝置點選響應速度,滑鼠事件與touch事件的那些事事件
- AngularJs 鍵盤事件和滑鼠事件AngularJS事件
- jQuery 事件(一) 滑鼠與鍵盤事件jQuery事件
- 簡單介紹Vue實現滑鼠懸浮切換圖片srcVue
- QGraphicsScene中捕捉滑鼠事件CSS事件
- wx模擬滑鼠事件事件
- wx處理滑鼠事件事件
- 禁止滑鼠點選事件事件
- JavaScriptFAQ(十四)——滑鼠事件(一)JavaScript事件
- 瀏覽器滑鼠事件瀏覽器事件
- 面相物件物件
- JQuery4:滑鼠事件和滾動事件jQuery事件
- jQuery實現的文字框預設值感應滑鼠動作jQuery
- 滑鼠進入移出事件事件
- C#窗體--滑鼠事件C#事件
- jquery-中的滑鼠事件jQuery事件
- css禁用滑鼠點選事件CSS事件
- sobel運算元,matlab實現Matlab
- 水晶藍蓮花(Matlab實現)Matlab
- KPCA的matlab實現Matlab
- svpwm的matlab模擬實現Matlab
- 1.21 JQuery4:滑鼠事件與滾動事件jQuery事件