是否可以在GUI中创建一个对象,其位置我可以通过光标位置(点击时拖动)通过将其“位置”属性设置为光标位置来定义?我应该使用什么功能?
答案 0 :(得分:4)
您可以使用SELECTMOVERESIZE功能打开GUI对象的移动和调整大小。然后,您只需用鼠标单击并拖动对象即可。就这么简单:
set(hObject,'ButtonDownFcn','selectmoveresize');
如果您的GUI对象是uicontrol object,那么不是那么简单,在这种情况下,您必须通过将'Enable'
property设置为'off'
或'inactive'
来禁用该对象为了使'ButtonDownFcn'
函数执行而不是'Callback'
函数。即使您没有为对象定义回调,也是如此。
您可能还需要在GUI中添加一种方法来打开和关闭对象的移动和调整大小,可能是您可以选择的额外按钮或菜单项。为了说明如何使用按钮执行此操作,这是一个简单的示例,它创建一个带有可编辑文本框的图形和一个按钮,可以打开和关闭移动和调整可编辑文本框大小的功能:
function GUI_example
hFigure = figure('Position',[100 100 200 200],... %# Create a figure
'MenuBar','none',...
'ToolBar','none');
hEdit = uicontrol('Style','edit',... %# Create a multi-line
'Parent',hFigure,... %# editable text box
'Position',[10 30 180 160],...
'Max',2,...
'String',{'(type here)'});
hButton = uicontrol('Style','pushbutton',... %# Create a push button
'Parent',hFigure,...
'Position',[50 5 100 20],...
'String','Turn moving on',...
'Callback',@button_callback);
function button_callback(hSource,eventData) %# Nested button callback
if strcmp(get(hSource,'String'),'Turn moving on')
set(hSource,'String','Turn moving off'); %# Change button text
set(hEdit,'Enable','inactive',... %# Disable the callback
'ButtonDownFcn','selectmoveresize',... %# Turn on moving, etc.
'Selected','on'); %# Display as selected
else
set(hSource,'String','Turn moving on'); %# Change button text
set(hEdit,'Enable','on',... %# Re-enable the callback
'ButtonDownFcn','',... %# Turn off moving, etc.
'Selected','off'); %# Display as unselected
end
end
end
注意:虽然文档将'Selected'
property列为只读,但我能够毫无问题地修改它。它必须是文档中的拼写错误。
答案 1 :(得分:2)
您可以在GUI中创建一个不可见的轴,并在那里绘制您想要的任何对象。然后,您可以使用文件交换中的DRAGGABLE来允许在整个地方拖动对象。