How to get the screen size where the current uifigure is located?

4 次查看(过去 30 天)
I want to capture the current uifigure while running, and I have test the function with this demo code.
there is one issue should to be solved: the Y postion from the demo code is base on the left top corner, but the Y position get from uifigure.positon is based on the left bottom corner. So it need to be converted to suit for the code. So my question is how to get the screen size where the active uifigure is located in the multiple screens?
(get(0,'screensize') can't get the secondary screen size)
other answers which can capture the active uifigure is also welcome!

回答(1 个)

Ganesh
Ganesh 2024-5-30
I understand that you intend to get screen size of the screen where the active "uifigure" is located. As you have pointed out already, the function "get(0,'screensize')" is intended to only get the screensize of the primary screen and not the secondary screens.
As a workaround, you can first find the positions of all the monitors and then use the monitor position and the "uifigure" position to locate which screen the "uifigure" has been plotted. Once you do that, you can retrieve the screensize by indexing the respective screen. You may follow the below steps to achieve the same:
  1. Use get(groot,'MonitorPositions') to get the monitor positions.
  2. Use uifigure.Position to get the position of the uifigure
  3. Use a for-loop to calculate which monitor the figure is being displayed on. A template of the code has been provided below.
for i = 1:size(monitorPositions, 1)
monitorLeft = monitorPositions(i, 1);
monitorBottom = monitorPositions(i, 2);
monitorRight = monitorLeft + monitorPositions(i, 3);
monitorTop = monitorBottom + monitorPositions(i, 4);
figLeft = figPos(1);
figBottom = figPos(2);
% Check if the uifigure is within this monitor's bounds
if figLeft >= monitorLeft && figLeft <= monitorRight && ...
figBottom >= monitorBottom && figBottom <= monitorTop
% Adjust the Y-coordinate for the capture based on the monitor's dimensions
adjustedBottom = monitorTop - (figBottom + figPos(4));
disp(['Figure is on Monitor ', num2str(i), ' with adjusted Y-position: ', num2str(adjustedBottom)]);
break;
end
end
Kindly adjust the code according to the required logic.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by