简体   繁体   中英

Scaling of font size with control size of a VBA user form control

I want to resize a VBA user form when it is displayed on a screen with different resolution. There does not seem to be a built in function for that. Therefore use some code which multiplies the Top, Left, Width, Height and Font.Size with the same factor, say 50%. However, this changes the appearance of the control: the text does not fit anymore in the area. This has nothing to do with the VBA code, because you get the same when you do the scaling by Hand:

  • Create an Option Button with Width 120, Height 24, Font Tahoma 16, Caption "abcdefghijklm" on a user form in Excel 2010.
  • Copy the Button and change properties: Width 60, Height 12, Font Tahoma 8. Now part of the text is missing (the letter m) and the bottom of the letters is invisible.

I guess this is because the Control contains not only caption text but also the option button itself and some margins? Anyway my question is: How can I calculate the optimal scaling factor for the caption font.

Thats not gonna be easy to fix using Tahoma so don't use it, use a mono-spaced font instead.

Find the width of a CHAR as a ratio of its pt (6,7,8,10,12,14,16 etc)

Ratio seems to be about 1.8 for Dejavu Sans Mono

then use

(width of button - margins)  / len(text) = width of letter
 width of letter * ptToFontRatio = pt size

Button 120px wide with 13 chars as above

(120-9)/13 = 8.53
 8.53 * 1.8=  15.3  = closest smaller pt = 14pt

Button 60px wide with 13 chars as above

(60-9)/13 = 3.92
3.92 * 1.8 = 7.056  - closest smaller pt =  7pt

Note: its always gonna be approx when working these out so round things down to suit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM