6

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

我使用的脚本利用了以编程方式生成的 GUI(不是通过 GUIDE),而且似乎 MATLAB 总是在第一台显示器上弹出它们。我进行了一些研究,发现可以使用 、 和 命令将 GUI 定位到选择的监视器p = get(gcf, 'Position')set(0, 'DefaultFigurePosition', p)但这movegui只有在我事先知道要使用哪个监视器时才有效。

有没有办法找出主 MATLAB GUI 在哪个监视器上启动并在同一监视器上弹出其他小 GUI?

4

2 回答 2

5

我们可以使用一些 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

几点注意事项:

  • 根属性文档
  • 输出值为“0”表示有问题。
  • 可能有更简单的方法来获取“RootPane”;我使用了一种我有很好经验的方法。
  • 如果您的 MATLAB 窗口跨越多个监视器,这只会识别其中一个监视器。如果需要此功能,您可以使用com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidthetc. 来找到 MATLAB 窗口的其他角落,并对它们进行相同的测试。
  • 在找到第一个有效监视器后,我没有费心打破循环,因为假设:1)只有一个监视器是有效的。2)循环必须处理的监视器的总量很小。
  • 对于勇敢的人,可以使用多边形(即inpolygon)进行检查。
于 2016-06-08T16:55:46.947 回答
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
于 2018-12-24T15:20:55.627 回答