我从公司网站到另一个网站移动很多。在任何一天,我可能只有我的笔记本电脑或多达四台显示器。有了多个显示器,我不知道我将选择使用哪个显示器用于MATLAB主GUI(双击matlab.exe时启动的主GUI)。这取决于可用监视器的分辨率。
我使用的脚本利用程序生成的GUI(不是GUIDE),似乎MATLAB总是在第一台显示器上弹出它们。我已经研究了一下,发现使用p = get(gcf, 'Position')
,set(0, 'DefaultFigurePosition', p)
和movegui
命令将GUI定位到选择的监视器,但这只有在我事先知道哪个时才能工作我希望使用的显示器。
有没有办法找出主MATLAB GUI在哪台显示器上运行,并在同一台显示器上弹出其他小GUI?
答案 0 :(得分:4)
我们可以使用一些Java技巧来获取当前的监视器;请参阅以下评论代码:
function mon = q37705169
%% Get monitor list:
monitors = get(groot,'MonitorPositions'); % also get(0,'MonitorPositions');
%% Get the position of the main MATLAB screen:
pt = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getLocationOnScreen;
matlabScreenPos = [pt.x pt.y]+1; % "+1" is to shift origin for "pixel" units.
%% Find the screen in which matlabScreenPos falls:
mon = 0;
nMons = size(monitors,1);
if nMons == 1
mon = 1;
else
for ind1 = 1:nMons
mon = mon + ind1*(...
matlabScreenPos(1) >= monitors(ind1,1) && matlabScreenPos(1) < sum(monitors(ind1,[1 3])) && ...
matlabScreenPos(2) >= monitors(ind1,2) && matlabScreenPos(2) < sum(monitors(ind1,[2 4])) );
end
end
很少注意到:
com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth
等查找MATLAB窗口的其他角落,并使用它们进行相同的测试。inpolygon
)。答案 1 :(得分:0)
Thnx Dev-iL几乎完美地工作,当稍微偏离屏幕时,或者根据我的经验,我最大化地添加了一些边距来“捕获”窗口。 发布我的修改:
function mon = getMatlabMainScreen()
%% Get monitor list:
monitors = get(groot,'MonitorPositions'); % also get(0,'MonitorPositions');
%% Get the position of the main MATLAB screen:
pt = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getLocationOnScreen;
matlabScreenPos = [pt.x pt.y] + 1; % "+1" is to shift origin for "pixel" units.
%% Find the screen in which matlabScreenPos falls:
mon = 0;
nMons = size(monitors,1);
if nMons == 1
mon = 1;
else
marginLimit = 100;
margin =0;
while ~mon
for ind1 = 1:nMons
mon = mon + ind1*(...
matlabScreenPos(1) + margin >= monitors(ind1,1) && matlabScreenPos(1) < sum(monitors(ind1,[1 3])) + margin && ...
matlabScreenPos(2) + margin >= monitors(ind1,2) && matlabScreenPos(2) < sum(monitors(ind1,[2 4])) + margin );
end
margin = margin + 1;
if margin > marginLimit
break;
end
end
end