gpt4 book ai didi

user-interface - Matlab:类似于 "relative"位置与 uicontrol/axis;调整大小时保持固定边距

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

我目前非常头疼的是让一个小的 GUI 工作得很好,它不是用 GUI 编辑器创建的,而是以编程方式创建的!到目前为止,我所拥有的是如下内容:

hFig = figure();
set(hFig, 'Position', [300 200 500 400]);
plot((1:10).^2, '*-r');

% Größe des Plots so anpassen, dass links Platz für Buttons
ap = get(gca, 'TightInset');
fp = get(gcf, 'Position');
set(gca, 'Position', [160/fp(3), 30/fp(4), (fp(3)-180)/fp(3), (fp(4)-60)/fp(4)]);

uicontrol('Style', 'pushbutton', 'String', 'foo', 'Position', [15 fp(4)-60 110 30]);
uicontrol('Style', 'pushbutton', 'String', 'bar', 'Position', [15 fp(4)-100 110 30]);

尝试调整它的大小:它“看起来”不一样,这意味着 uicontrol 框不会保持在相同的相对位置,并且从轴到图形窗口的边距会变大。我想要实现的是:

有一个带有给定位置(x/y,宽度和高度)的图形窗口,里面有一个绘图。该图将有一个标题以及 x 和 y 的标签。将绘图设置为高度和宽度,使 TightInset 在每个方向上加上与图形窗口一样大的特定像素大小(例如 TightInset + 10px)的边距;除了在左侧留出 150px 的自由空间来放置一些 uicontrol 按钮,并让它们保持在相同的位置:这与能够从顶部/左侧给出位置相同(顶部 = 20,左侧 = 10)底部/左侧。

非常感谢!

最佳答案

好吧,终于找到了我想要的可行解决方案 :-) 希望它对对此感兴趣的人有所帮助:

主脚本文件:

p = [300 300 1000 600];
fixedMargins = [250 0 0 0]; % [left, top, right, bottom]
f = figure('Position', p, 'Color', [0.9 0.9 0.9]);
plot(-10:10, (-10:10).^3, '*-r');
set(f, 'ResizeFcn', {@resizeCallback, gca, fixedMargins, {@myuiFunc, f, 40, 50}});

title('bla')
xlabel('foooooooooo')
ylabel('barrrrrrr')

调整大小回调函数:

% Need to pass the handle of the axis to modify (hAx) AND to pass the
% desired margins as second extra callback argument:
% [left, top, right, bottom]!
function resizeCallback(hFig, ~, hAx, fixedMargins, func)
% Disable automatic rezising
set(hAx, 'Units', 'pixels');

% Figure-Size
fp = get(hFig, 'Position');

% Calculate Position of the axis
margin = get(hAx, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1];

% Position to fill the figure minus the TightInset-Margin
newPos = [0 0 fp(3:4)] - margin;

% Change position based on margins
newPos(1) = newPos(1) + fixedMargins(1);
newPos(3) = newPos(3) - fixedMargins(1) - fixedMargins(3);
newPos(2) = newPos(2) + fixedMargins(4);
newPos(4) = newPos(4) - fixedMargins(2) - fixedMargins(4);

% Set new position
set(hAx, 'Position', newPos);

% Call UI-Func
if(nargin == 5)
f = func{1};
args = func(2:end);
f(args{:});
end
end

您可以传递在调整图形窗口大小时要调用的任何函数,例如更新图中的内容。在我的示例中,它是 myuiFunc(),如下所示:

function myuiFunc(hFig, left, top)
persistent handles;

if(~isempty(handles))
delete(handles);
handles = [];
end

fp = get(hFig, 'Position');
h1 = uicontrol('Style', 'pushbutton', 'String', 'Foo','Position', [left fp(4)-top 100 35]);
h2 = uicontrol('Style', 'pushbutton', 'String', 'Bar','Position', [left fp(4)-top-50 100 35]);
handles = [h1 h2];
end

我喜欢它 :) 希望你也喜欢!

编辑:无需编辑 resizeCallback 函数!如果您只是将所需的边距传递给它应该可以工作,并且如果您愿意,还可以添加一个带有参数的函数句柄,每次调整大小都会调用该函数句柄!

关于user-interface - Matlab:类似于 "relative"位置与 uicontrol/axis;调整大小时保持固定边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8510932/

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