【发布时间】:2011-09-21 02:33:28
【问题描述】:
是否可以在 GUI 中创建一个对象,我可以通过将其“Position”属性设置为光标位置来定义其位置(单击时拖动)?我应该使用什么功能?
【问题讨论】:
标签: oop user-interface function matlab cursor-position
是否可以在 GUI 中创建一个对象,我可以通过将其“Position”属性设置为光标位置来定义其位置(单击时拖动)?我应该使用什么功能?
【问题讨论】:
标签: oop user-interface function matlab cursor-position
您可以使用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 列为只读,但我可以毫无问题地对其进行修改。一定是文档中的错字。
【讨论】:
您可以在您的 GUI 中创建一个不可见的轴,并在其中绘制您想要的任何对象。然后,您可以使用文件交换中的DRAGGABLE 来允许将对象拖到各处。
【讨论】: