In the first part of the series, we will create a simple GUI that will show the current position of the mouse on the GUI.
Step-by-step guide:
- Create new GUI using MATLAB GUIDE, save file as gui_mouse
- Create two static text fields and configure them as fellow
- rename tags to lbl_x and lbl_y
- set strings to 'x loc:' and 'y loc:'
- set FontSize to 18, and resize components
- Configure figure property (double-click on figure)
- rename tag to fig_mouse
- change unit to pixels
- click on callback for WindowButtonMotionFcn and insert the following code
- Save all files and run the GUI or M-file.
pos = get(hObject, 'currentpoint'); % get mouse location on figure
x = pos(1); y = pos(2); % assign locations to x and y
set(handles.lbl_x, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.lbl_y, 'string', ['y loc:' num2str(y)]); % update text for y loc
Note: anytime the mouse is moved over the GUI, it will trigger the WindowButtonMotionFcn on the figure, which will then update the labels with the current location of the mouse pointer on the figure.
good job..
ReplyDeletebut can you show the way to get the mouse location without refer to the windows or screen...i mean if we move the mouse out of the windows program, it will show the position of the mouse.
tq