str*_*its 7 oop matlab user-interface function cursor-position
是否可以在GUI中创建一个对象,其位置我可以通过光标位置(点击时拖动)通过将其"位置"属性设置为光标位置来定义?我应该使用什么功能?
您可以使用SELECTMOVERESIZE函数来打开 GUI 对象的移动和调整大小。然后您只需用鼠标单击并拖动该对象即可。就这么简单:
set(hObject,'ButtonDownFcn','selectmoveresize');
Run Code Online (Sandbox Code Playgroud)
不那么简单的是,如果您的 GUI 对象是uicontrol 对象,在这种情况下,您必须通过将'Enable'
属性设置为'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
Run Code Online (Sandbox Code Playgroud)
注意:虽然文档将该'Selected'
属性列为只读,但我可以毫无问题地修改它。这一定是文档中的拼写错误。
归档时间: |
|
查看次数: |
2066 次 |
最近记录: |