2016-06-08 34 views
5

我从公司站点转移到另一个站点。在任何一天,我可能只有我的笔记本电脑或多达四台显示器。使用多台显示器时,我不知道选择哪一台显示器用于MATLAB主GUI(双击matlab.exe时启动的主GUI)。这取决于可用显示器的分辨率。在多显示器配置中确定MATLAB的显示器

我用利用程序生成的图形用户界面的脚本(不是GUIDE),似乎MATLAB弹出起来总是第一个监视器上。我已经研究了一点点,发现通过使用p = get(gcf, 'Position')set(0, 'DefaultFigurePosition', p)movegui命令来找到所选择的显示器的GUI,但只有在事先知道要使用哪个显示器时才能使用。

有没有办法找出在其监测的主要MATLAB GUI启动并有其他的小图形用户界面弹出一台监视器上?

回答

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 

几点注意事项:

  • Root properties documentation
  • 输出值“0”表示有问题。
  • 可能有更简单的方法来获得“RootPane”;我使用了一种我有良好经验的方法。
  • 这将只识别其中一个显示器,以防您的MATLAB窗口跨越多个显示器。如果需要此功能,您可以使用com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth等查找MATLAB窗口的其他角落,并使用它们进行相同的测试。
  • 我没有与第一有效监控后循环的打破打扰被发现,因为它假定:1)只有一个显示器是有效的。 2)循环将要处理的监视器总量很小。
  • 对于勇敢者,可以使用多边形执行检查(即inpolygon)。
相关问题