필터 지우기
필터 지우기

Respond to the left mouse click button in GUI

조회 수: 14 (최근 30일)
Hienbn
Hienbn 2014년 9월 4일
답변: Geoff Hayes 2014년 9월 4일
Hi everyone,
I'm designing a GUI which having several editboxs (allow input), and a figure_axis(explanation figure corresponding to the clicked editbox). I want to it to display my assigned explanation figure when I click (left click) to editboxs. Is there any way to do this?
Thank you so much!

채택된 답변

Joseph Cheng
Joseph Cheng 2014년 9월 4일
there is a problem with what you're trying to accomplish. here is a quick demo you can run. copy and paste into a mfile called clicktest. Here you can see the text field on the lower right updates the X and Y location of a mouse click but doesn't update when you click on a GUI interface (button, either edit boxes, and even the text field).
function clicktest()
fig = figure
hax = axes('Units','pixels');
surf(peaks)
uicontrol('Style', 'pushbutton', 'String', 'Clear',...
'Position', [20 20 50 20],...
'Callback', 'surf(randi(5,49,49)*peaks)');
% The pushbutton string callback
% calls a MATLAB function
ustring = uicontrol('Style','text',...
'Position',[400 45 120 20],...
'String','clicked objectt')
edit1= uicontrol('Style','edit',...
'Position',[400 95 120 20],...
'String','clicked object')
edit2= uicontrol('Style','edit',...
'Position',[400 115 120 20],...
'String','clicked object')
set(fig,'WindowButtonDownFcn',{@position,ustring});
end
function position(hobject,event,ustring)
C = get(hobject,'CurrentPoint');
set(ustring,'String',num2str(C));
end
I would go with a radio button group that you put next to each text box.

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 9월 4일
It is possible to capture left-mouse button events around an edit box, but it is a little clumsy, and like Joseph indicated, it won't work if you click within the edit box, just around it.
function captureMouseClick
% create the figure
h = figure;
% create the edit widget
edit1h = uicontrol('Style', 'edit',...
'String', 'enter text here',...
'Position', [20 340 100 50],...
'ButtonDownFcn', @edit1ButtonDown);
% the callback to capture the mouse button down event for the edit widget
function edit1ButtonDown(hObject,~)
% get the last mouse click selection type within the figure
seltype = get(h,'SelectionType');
if strcmpi(seltype,'normal')
fprintf('left mouse button pressed!\n');
elseif strcmpi(seltype,'alt')
fprintf('right mouse button pressed!\n');
elseif strcmpi(seltype,'open')
fprintf('double mouse click!\n');
else
fprintf('other - shift and mouse-click!\n');
end
end
end
Seems only to work if you are within a few pixels outside the border of the edit widget. Try it and see what happens!

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by