How to determine size of checkbox

15 Ansichten (letzte 30 Tage)
Jan
Jan am 20 Mai 2013
I am having some troubling positioning a checkbox in a UI. I have (programmatically) added a number of rows of checkboxes to a figure. On my PC with windows XP (Matlab 2011b) I see that these are alligned nicely. I use a height of 15 pixels (as also indicated in this thread: http://www.mathworks.com/matlabcentral/answers/10123-calculate-the-size-needed-for-a-uicontrol) E.g. 10 rows of checkboxes for a total vertical size of 150 pixels.
However, on another PC with Windows 7 (also 2011b), I see that the checkboxes overlap. They are not fully displayed. Some trial and error indicates that the size of these checkboxes is 18 pixels. Another difference I noted is that the ScreenPixelsPerInch property of the root object is set to 116. On the Windows XP computer it is set to 96. Could that explain the difference? At http://www.mathworks.com/matlabcentral/newsreader/view_thread/303963 someone has encountered the same difference in DPI setting. I can see that chaning the ScreenPixelsPerInch settings changes the font size and button size. The size of the checkbox seems unaffected.
My question: on what is the size of the checkbox control depending? How can I determine how much space I need to fit a number of checkboxes?
Update I'm now starting to wonder if this is something Windows 7 specific. I use the 'outerposition' property to place multiple figures next to each other without overlapping, but also not leaving any space between them. This works fine on Windows XP. However, when looking closely at the figures on my Windows 7 machine, I noticed the borders are larger and overlapping. Does Matlab on Windows 7 draw certain UI elements (e.g. borders, checkboxes) in a different way?
Another Update I think I'm getting closer to a solution. I found that the Windows display setting "text size DPI" has something to do with this. Normally, this settings is set to 100% (96 dpi). This is reflected in the ScreenPixelsPerInch attribute of the root object in Matlab and it gives a checkbox height of 15 pixels. I tried various settings and saw the following:
Windows text size setting - Windows DPI - Matlab DPI - Checkbox size
100 % - 96 - 96 - 15 pixels
125 % - 120 - 116 - 18 pixels
150 % - 144 - 116 - 22 pixels
200 % - 192 - 116 - 22 pixels
As can be seen, for the 125% setting, Matlab scales nicely. For the 150% setting, the checkbox size is scaled properly (1.5 * 15 ~ 22). However, this is not reflected in the ScreenPixelsPerInch attribute of Matlab, which stays at 116.
Thus, I have an (partial) answer: use only 100 and 125 % settings in Windows. Does anybody know how to detect the large windows DPI settings (>125%)? It seems not detectable by looking at the ScreenPixelsPerInch attribute.

Akzeptierte Antwort

Jan
Jan am 10 Jun. 2013
To answer my own question:
One can use the following code which uses only Matlab routines:
dpi = get(0, 'ScreenPixelsPerInch');
if dpi >= 116
h = 18;
else
h = 15;
end
For the cases I tested, the ScreenPixelsPerInch property only returns 96 or 116. This does not cover all cases. Therefore, the following solution uses a Java method:
dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
if dpi < 120
h = 15;
elseif dpi < 144
h = 18;
else
h = 22;
end
During my testing, I only encountered these 3 checkbox sizes. The range of resolutions tested is between 96 and 288 DPI.

Weitere Antworten (2)

Jan
Jan am 10 Jun. 2013
The size of checkboxes and popupmenus is controlled by the operating system. While most of the settings can be determined by the Windows-API function GetSystemMetrics, finding out the exact dimensions is not trivial, see e.g. http://stackoverflow.com/questions/1164868/how-to-get-size-of-check-and-gap-in-check-box. Whenn different OS', fonts and screen resolutions come into play, a 100% exact solution is not possible in my opinion.
  3 Kommentare
Malcolm Lidierth
Malcolm Lidierth am 12 Jun. 2013
@Jan and/or Jan (?)
Nor sure whether you are using MATLAB uicontrols or Java/javacomponent.
"The size of checkboxes and popupmenus is controlled by the operating system". Not quite. Uicontrols in MATLAB are Java Swing objects - so controlled by the Java look-and-feel. While that will be OS-dependent unless altered from the default, the L&F is not restrained to respect OS defaults.
For better consistency between Windows XP/7 you could use the Nimbus L&F if that is acceptable - but I would change the L&F as early after MATLAB startup as possible: Java allows the L&F to be set at JVM startup - if changed after that using the Java UIManager, problems can arise when the wrong L&F UI delegate is called. You may get away with it - but there will always be a risk of a subsequent run-time exception.
get(0, 'ScreenPixelsPerInch') returns the wrong answer on some MATLAB versions when on some OSs (~R2010/R2011 - I forget the details). java.awt.Toolkit.getDefaultToolkit().getScreenResolution() will give the correct answer, but not necessarily the one used by all MATLAB TMW code.
Expect a difference in pixel position when comparing MATLAB and Java positions. I suspect out-by-1 errors converting between MATLAB (unity-based) and Java (zero-based) positions in some TMW code, but those can add up to greater errors through multiple calls. Some of TMWs mouse position code allows a +/-5 pixel tolerance.
This can be an issue with using javacomponent - the MATLAB hgcontainer and the AWT/Swing component may be offset from each other.
While ScreenPixelsPerInch is settable in the MATLAB root- but does that really make any sense unless you also change monitors/settings?
Regards
Malcolm
Jan
Jan am 12 Jun. 2013
Bearbeitet: Jan am 12 Jun. 2013
@Jan: Yes, the Windows-API function can be called through a MEX functions.
@Malcolm: You are right. I was too sloppy with the claim, that Java is a part of the OS already.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 12 Jun. 2013
Do it the hard and reliable way: Create a checkbox, capture the screen, use the image processing toolbox to find the edges. Finding the top-left and bottom right pixels with a color different from the figures background should even work without the IPT.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by