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
相關文章
- 怎樣用純CSS實現禁止滑鼠點選事件?CSS事件
- 滑鼠事件事件
- 滑鼠拖拽事件事件
- 差分進化演算法介紹及matlab實現演算法Matlab
- 滑鼠、鍵盤事件事件
- angular 的滑鼠事件Angular事件
- 監聽滑鼠事件事件
- 事件 滑鼠事件 表單事件 from表單事件
- 『OpenCV-Python』滑鼠事件OpenCVPython事件
- 瀏覽器滑鼠事件瀏覽器事件
- QGraphicsScene中捕捉滑鼠事件CSS事件
- 簡單介紹Vue實現滑鼠懸浮切換圖片srcVue
- JQuery4:滑鼠事件和滾動事件jQuery事件
- MFC vc++ 中CTreeContrl如何自定義實現滑鼠單擊或雙擊響應事件 ,即重寫類似於控制元件的響應事件或訊息C++事件控制元件
- 水晶藍蓮花(Matlab實現)Matlab
- sobel運算元,matlab實現Matlab
- 滑鼠進入移出事件事件
- C#窗體--滑鼠事件C#事件
- 1.21 JQuery4:滑鼠事件與滾動事件jQuery事件
- 面相物件物件
- 奇異值分解以及matlab實現Matlab
- 主成分分析及其matlab實現Matlab
- JavaScript滑鼠中鍵滾動事件JavaScript事件
- angualr實現滑鼠拖拽排序功能排序
- HMI人機介面相關介紹及人機介面設計
- 正交幅度調製(QAM)訊號的產生與解調介紹及matlab實現Matlab
- FFT演算法實現與分析MATLABFFT演算法Matlab
- Javascript滑鼠滾輪事件相容寫法JavaScript事件
- tkinter中滑鼠與鍵盤事件(十五)事件
- 簡單介紹Angular單元測試之事件觸發的實現Angular事件
- jQuery事件中on實現繫結多個事件jQuery事件
- 使用Kafka實現事件溯源Kafka事件
- QStyledItemDelegate 和QTreeView實現滑鼠hover訊息QTView
- 12.3 實現模擬滑鼠錄製回放
- JavaScript、CSS實現滑鼠跟隨繁花效果JavaScriptCSS
- 滑鼠懸浮div實現旋轉效果
- .滑鼠點選愛心特效的實現特效
- 蟻群演算法原理及Matlab實現演算法Matlab