在多监视器配置中确定MATLAB的监视器

wrrgggsh  于 7个月前  发布在  Matlab
关注(0)|答案(2)|浏览(106)

我经常从一个公司搬到另一个公司。在任何一天,我可能只有我的笔记本电脑或多达四个显示器。对于多个显示器,我不知道我将选择哪个显示器用于MATLAB主GUI(双击matlab.exe时启动的主GUI)。这取决于可用显示器的分辨率。
我使用的脚本利用编程生成的GUI(而不是通过GUIDE),似乎MATLAB总是在第一个监视器上弹出它们。我已经研究了一点,发现通过使用p = get(gcf, 'Position')set(0, 'DefaultFigurePosition', p)movegui命令将GUI定位到所选的监视器,但这只有在我事先知道我要使用哪个监视器时才有效。
有没有办法找出主MATLAB GUI在哪个监视器上,并在同一监视器上弹出其他小GUI?

kmpatx3s

kmpatx3s1#

我们可以使用一些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”表示有问题。
  • 也许有一个更简单的方法来获得“根”;我用了一种我很有经验的方法。
  • 如果MATLAB窗口跨越多个显示器,则这将仅识别其中一个显示器。如果需要此功能,可以使用com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth等。找到MATLAB窗口的其他角落,并对它们进行相同的测试。
  • 在找到第一个有效的监视器后,我没有费心打破循环,因为它假设:**1)**只有一个显示器有效。**2)**循环必须处理的监视器总量很小。
  • 对于勇敢的人来说,可以用多边形进行检查(即inpolygon)。
sxissh06

sxissh062#

Thnx Dev-iL,工作几乎完美,我添加了一些利润率'赶上'窗口时,稍微离屏,或在我的经验,只是最大化。Posting my edit:

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

相关问题