MATLAB : right click behind the window when closing a figure with a 'ButtonDownFcn'

StackOverflow https://stackoverflow.com/questions/23382657

  •  12-07-2023
  •  | 
  •  

Вопрос

I'm having a specific issue that can be summed up in the little script below. Obviously it's part of a bigger project.

This script (call it 'my_test.m') opens a figure and displays an image. A 'ButtonDownFcn' is set so that the figure is closed when the image is right-clicked.

The behavior that annoys me is that after the figure is closed, the right-click is activated on whatever-is-behind-the-figure. As if there had never been a figure there.

Would you have an idea to prevent this behavior and tell the operating system to ignore the right click ? Thanks in advance.

Configuration:

  • Matlab : R2011b - 64bit
  • Windows 7

Script:

function my_test()
handles.f=figure;
handles.a=imshow(ones(400));
set(handles.a,'ButtonDownFcn',{@image_ButtonDownFcn});
guidata(handles.f,handles);

function image_ButtonDownFcn(hObject, eventdata)
handles=guidata(hObject);
switch lower(get(handles.f, 'selectiontype'))
  case 'alt' % right click
    close(handles.f);
end
Это было полезно?

Решение

Thanks to Lucius Domitius Ahenobarbus and RTL, I modified the code and it works. I just replaced this line :

close(handles.f);

...by this one :

set(handles.f,'WindowButtonUpFcn','close(gcf);');

The idea is to use the 'WindowButtonUpFcn' of the figure (images or axes don't have such callbacks) so that nothing happens on mouse click but then the figure closes itself on mouse click release. There might be neater solutions ?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top