gpt4 book ai didi

oop - 在 GUI 中拖放

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:10 25 4
gpt4 key购买 nike

是否可以在 GUI 中创建一个对象,通过将其“位置”属性设置为光标位置,我可以通过光标位置定义其位置(单击时拖动)?我应该使用什么功能?

最佳答案

您可以使用 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作为只读的,我可以毫无问题地修改它。这一定是文档中的错字。

关于oop - 在 GUI 中拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6452549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com