dcsimg
Results 1 to 33 of 33

Thread: [RESOLVED] FYI: StdFont scaling for DPI awareness

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Resolved [RESOLVED] FYI: StdFont scaling for DPI awareness

    Been playing with a new project to help others get their projects per-monitor DPI. One of the annoying things, among many, I had to resolve was scaling VB stdFont objects. Wasn't as simple as Me.Font.Size = Me.Font.Size * scaleRatio

    For others that are playing with per-monitor DPI awareness, here is the simple 'trick'
    Code:
    Dim oFont As IFont ' hidden interface
    Set oFont = Me.Font
    oFont.SetRatio currentDPI, 2540
    For most VB intrinsic controls, need to force font refresh: Set Me.Font = Me.Font; however, OCXs seem to monitor changes and redraw immediately.

    The actual font handle does change, as expected, but the stdFont object does not. This also preserves the Font.Size value. The ScaleRatio method of the IFont interface is simply a scale factor.

    At end of month, hoping to post a sample project (set of classes) in the code bank that could significantly help others with per-monitor awareness.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  2. #2

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    Another FYI and not font related, but DPI related when project is system aware or per-monitor aware.

    The SSTab and its contained controls. Many think/thought that the controls that are in other than the visible tab, were always at a pixel offset of Screen.TwipsPerPixelX * 5000 = 75000 (at 96 DPI). Well, 75000 just happens to be a fixed value and has nothing to do with (TPP).

    To adjust for any DPI, remove the -75000 offset from the control's Left property, scale the remainder to current DPI, then add -75000 back. For Line controls you'll need to scale both the X1 and X2 properties.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,804

    Re: FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by LaVolpe View Post
    75000 just happens to be a fixed value
    Knew that.

    I'm also starting to get worried about that value. Basically, it's going to be interpreted as twips. And, if we assume 96 DPI, basically we're shifting 5,000 pixels to the left (75000 / 1440 * 96). And, we've already got monitors that are 4096 pixels across. If we set two of those side-by-side (which my system could actually do), we could already be in trouble. If we had a sizable form with an SSTab control that auto-sized with the form, and controls that autosized as well, we'd be in trouble. Controls that stretched all the way across (say lines or a long textbox) on non-selected tabs would still be showing.

    (GeForce 1070: Maximum Digital Resolution: 7680x4320@60Hz. 7680 horizontal pixels ... oops, SSTab is in trouble.)
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,654

    Re: FYI: StdFont scaling for DPI awareness

    I quit using SSTab a long time ago anyway. As far as I can tell it is a reworked 3rd party 16-bit control from Sheridan or somebody that only got carried forward into 32-bit VB4 to help people port old 16-bit programs. It is probably just vestigial in VB6 meant only to help people make a first-cut porting to VB5 and VB6, since VB6 replaced the half-hearted problematic VB5 very quickly and was skipped by many shops.

    Typically these things got replacements for use in new programs if a control's paradigm was believed a good fit for Windows.

    In Win32 Common Controls we got Tab controls and the VB5 TabStrip wrapper and VB6 TabStrip clone. These don't have the warts of of SSTab and by avoiding the VB6 cloned version we can avoid Class of the Styles by selecting the CC6 assembly and using the VB5 wrapper around the Windows Tab control.

    It's a little scary that people are still falling into the SSTab "trap for new players" this late in the day. It's a legacy control even in VB6 terms.

  5. #5
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by dilettante View Post
    I quit using SSTab a long time ago anyway. As far as I can tell it is a reworked 3rd party 16-bit control from Sheridan or somebody that only got carried forward into 32-bit VB4 to help people port old 16-bit programs. It is probably just vestigial in VB6 meant only to help people make a first-cut porting to VB5 and VB6, since VB6 replaced the half-hearted problematic VB5 very quickly and was skipped by many shops.

    Typically these things got replacements for use in new programs if a control's paradigm was believed a good fit for Windows.

    In Win32 Common Controls we got Tab controls and the VB5 TabStrip wrapper and VB6 TabStrip clone. These don't have the warts of of SSTab and by avoiding the VB6 cloned version we can avoid Class of the Styles by selecting the CC6 assembly and using the VB5 wrapper around the Windows Tab control.

    It's a little scary that people are still falling into the SSTab "trap for new players" this late in the day. It's a legacy control even in VB6 terms.
    It has been discussed many time I think, but SStab is much better than TabStrip in serveral ways.

  6. #6

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by Elroy View Post
    Knew that.

    I'm also starting to get worried about that value. Basically, it's going to be interpreted as twips. And, if we assume 96 DPI, basically we're shifting 5,000 pixels to the left (75000 / 1440 * 96). And, we've already got monitors that are 4096 pixels across....
    Should someone using the SSTab be concerned with that, then they could deal with it by trapping when tabs are shown/hidden and add an additional offset, maybe in the -Y axis?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: FYI: StdFont scaling for DPI awareness

    This provides better scaling on my VB controls than I was using: because it uses fractional sizes, i.e. was setting size to 8.25

    Is there any way to set a non-integer font size on an API-created Common Control? WM_SETFONT uses a LOGFONT which must be an integer.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,654

    Re: FYI: StdFont scaling for DPI awareness

    In LOGFONT you provide values in logical units, for VB6 windows that is in pixels (mapping mode is MM_TEXT). No, you can't provide subpixel values.

    The magic number 2540 above is actually the number of HIMETRIC (0.01 millimeter) units in an inch.

  9. #9

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by dilettante View Post
    The magic number 2540 above is actually the number of HIMETRIC (0.01 millimeter) units in an inch.
    FYI: The DPI value and HiMetric constant I used in my sample code were the result of looking at stored values of the stdFont object: 72 bytes after ObjPtr(stdFont) is where the 1st param to SetRatio is stored and the second param is at byte offset 76. Once I discovered that, providing the correct parameter values, relative to DPI, was simple.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,654

    Re: FYI: StdFont scaling for DPI awareness

    It doesn't look like the actual values passed matter as long as they define the desired ratio, i.e. pixels per inch.

    Of course for some ratios you want to use different values so that you don't have to worry about rounding/truncation. The actual numbers get used in MulDiv() calls as far as I can tell.

  11. #11

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    @Dilettante.

    If I use IFont.SetRatio to scale size by 175% from 96 DPI, i.e., SetRatio 168, 2540, then I get a new hFont in the IFont interface If I, instead, multiply the stdFont.Size by the same scale factor of 1.75, I also get a new hFont, while the IFont.ScaleRatio for that stdFont stays the same (byte offsets 72 & 76). When I query the LOGFONT structure on both fonts, scaled using different methods, the lfHeight member is identical.

    However, we can get a visually different rendered font in changing DPIs if scaling the stdFont.Size and that size is not always correct. I don't understand why it is that way. I don't know if non-integral twips per pixel (those at 175%, 200% DPI) are part of the reason or not, but suspect so. I decided to find a better workaround because changing DPI from 175% to 100% did not produce the same displayed font at 100% DPI as a font loaded into 100% DPI at startup. It was actually rendered smaller than expected.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,654

    Re: FYI: StdFont scaling for DPI awareness

    What about SetRatio 336, 5080 though? Should get the same result.

    My point was that to avoid truncating or rounding you can change the second term, double it or triple it as required.

    SetRatio 168.5, 2540 doesn't work, but SetRatio 337, 5080 would get you there. Thankfully we probably won't ever face a need for that.


    I think you're right that somewhere in OLE there is a truncation issue. There might even be several places where it happens different ways. Size in Points as a Currency has been making me nervous.

  13. #13

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    I think we agree. In fact, I've been struggling whether to tweak the DPI/HiMetric parameters if a DPI that isn't a multiple of 4 is selected or allow OLE to do its truncating or whatever its defaults are. My initial gut says don't handle those specially because a typical user is not going to go into the system DPI settings and set its percentage to something like 130% or 222%.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,584

    Re: FYI: StdFont scaling for DPI awareness

    This is a neat discovery and looks like considerable progress.

    So not only screen's Width/Height/TwipsPerPixelsX/Y/etc. has to be queried on current form (dependant on it's monitor placement) but all font's used by constituent controls has to be enumerable (registered in a collection) so that these get SetRatio treatment when the form is placed on secondary monitor.

    cheers,
    </wqw>

  15. #15

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    @wqweto. Just FYI about Screen.Width, Screen.Height...

    When project loaded into 175% DPI, TPP is 8 (adjusted by VB) when it should be 8.57 (1440/168). However, VB will return the correct screen's dimensions relative to 8 TPP, i.e., Screen.Width / 8 is correct, for the primary monitor. This is also true for an incoming pixels to twip conversions from outside the project, i.e., X,Y coordinates in control/form events.

    And a 2nd monitor is not the only scenario: changing DPI while project is running, is another scenario. For others, we are discussing per-monitor DPI awareness which needs to be explicitly set for VB projects.

    Edited: That adjusted TPP unfortunately governs the auto-scaling of controls at startup. So if you had a 256x256 (@ 100% DPI) control loaded into 175% DPI, its size should be 448x448 (256*1.75), but is scaled by VB to 480x480 (32 pixels larger). This is something that I feel should be corrected on form load. The form & controls should be down-scaled from 8 to 8.57 TPP (a scale factor of ~0.9333)

    Using 256x256 example, here's what it would look like after down-scaling
    256 @ 100% DPI in Twips is: 3840. 3840 twips / 8 (175% DPI) is 480: wrong
    After down-scaling 3840 * (8 / (1440/168)), 3840 twips becomes 3584. 3584 / 8 = 448: right
    Last edited by LaVolpe; Sep 29th, 2019 at 12:40 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  16. #16

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by fafalone View Post
    This provides better scaling on my VB controls than I was using: because it uses fractional sizes, i.e. was setting size to 8.25

    Is there any way to set a non-integer font size on an API-created Common Control? WM_SETFONT uses a LOGFONT which must be an integer.
    @fafalone. How are you scaling your HFonts? If you are using basically lfHeight * scaleRatio, then that could produce sizes you may feel aren't ideal due to rounding.

    Why? Let's look at 8.75 point size @ 96 DPI and calculate its lfHeight based on Microsoft supplied algorithm
    lfHeight = -(pointSize * DPI) / 72
    lfHeight would be -12 due to rounding from single/double to long

    Now if we scale that to 175%, we get a different lfHeight value depending on the algorithm
    1. lfHeight = lfHeight * 1.75 = -21
    2. lfHeight = -(8.75 * 168) / 72 = -20 after rounding
    The second algorithm doesn't suffer from scaling previously-rounded values but requires knowledge of a pre-scaled point size
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  17. #17

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Just thought I throw these out. Here are a few font scaling routines that should handle nearly any scenario

    Code:
    Private Declare Function GetObjectW Lib "gdi32" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    Private Declare Function CreateFontIndirectW Lib "gdi32" (lpLogFont As Any) As Long
    
    Public Sub ScaleStdFontToDPI(oFont As IFont, ByVal DPI As Long)
    
        ' Pass a stdFont. The scaling is performed by setting the
        '   font's scale ratio vs. changing the font's Size.
    
        If Not oFont Is Nothing Then
            On Error Resume Next
            oFont.SetRatio DPI, 2540&
            If Err Then Err.Clear
            On Error GoTo 0
        End If
    
    End Sub
    
    
    Public Function ScaleHFontPointSizeToDPI(ByVal hFont As Long, ByVal PointSizeAt100Pct As Single, ByVal DPI As Long) As Long
    
        ' Function returns a value used for LOGFONT.lfHeight member or a new font handle
    
        ' If hFont is passed as non-zero, then a new font will be created
        '   Scales a font handle, from 100% DPI, to a specific DPI based on its point size & returns new font.
        '   You are responsible for destruction of both the provided & returned fonts
        
        ' Otherwise, the passed point size is scaled to the passed DPI and that value is returned
    
        ' PointSizeAt100Pct is the font's Size @ 100% DPI
        ' Formula used: -MulDiv(pointSize, toDPI, 72)
        
        Dim cbLen As Long, aData() As Long  ' faux LOGFONTW structure
    
        If DPI <= 0 Then Exit Function
        If hFont = 0 Then
            ScaleHFontPointSizeToDPI = -(PointSizeAt100Pct * DPI) / 72!
        Else
            cbLen = GetObjectW(hFont, 0&, ByVal 0&)
            If cbLen <> 0 Then
                ReDim aData(0 To cbLen \ 4)
                GetObjectW hFont, cbLen, aData(0)
                aData(0) = -(PointSizeAt100Pct * DPI) / 72!
                ScaleHFontPointSizeToDPI = CreateFontIndirectW(aData(0))
            End If
        End If
        
    End Function
    
    Public Function ScaleHFontHeight(ByVal hFont As Long, ByVal ScaleRatio As Single) As Long
        ' scales a font handle, from its current Height by a scale ratio and returns new font
        ' You are responsible for destruction of both the provided & returned fonts
        
        Dim cbLen As Long, aData() As Long  ' faux LOGFONTW structure
        
        If hFont = 0 Or ScaleRatio <= 0! Then Exit Function
        cbLen = GetObjectW(hFont, 0&, ByVal 0&)
        If cbLen <> 0 Then
            ReDim aData(0 To cbLen \ 4)
            GetObjectW hFont, cbLen, aData(0)
            aData(0) = aData(0) * ScaleRatio
            ScaleHFontHeight = CreateFontIndirectW(aData(0))
        End If
        
    End Function
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  18. #18
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Lavolpe, what issue are you trying to address? 1) the multiple monitors DPI awareness, 2) the small font missize that happens when VB6 automatically handles the fonts sizes on each DPI, 3) something else?

  19. #19

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by Eduardo- View Post
    Lavolpe, what issue are you trying to address?
    None, any longer.

    Was looking for solid/best scaling methods to address VB's intrinsic controls, UCs, outside ocxs, and API windows. Thought I'd share this information.

    I'm about to post a separate thread related to O/S window classes that are per-monitor aware and possible ways of dealing with them if your project is per-monitor aware. This could affect every usercontrol writer that hosts window classes via CreateWindowEx. I think Krool will be dealing with this since his CCR UCs host API window classes and his UCs are not per-monitor aware. In that case, in a per-monitor aware project, those API windows render some/most/all of their content at the new DPI, but since the UC isn't DPI aware, it doesn't rescale API window items that it should and that the API window doesn't (some fonts, images, etc). A mess.
    Last edited by LaVolpe; Oct 7th, 2019 at 07:43 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  20. #20
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    What do you think of the automatic scaling (when a form is dragged across monitors) that Windows itself performs when the programs are manifested as DPI aware but not as DPI aware per monitor?
    In normal programs, my my little testing showed that it works fine.
    I didn't test much, I tested dragging forms from a 1920x1080 125% (120 DPI) monitor to a 3840x2160 300% (288 DPI) TV, from one screen to the other. I didn't notice blurring or other issues.

  21. #21

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    There are currently 5 awareness contexts
    1: Unaware
    <dpiAware>false</dpiAware>, <dpiAwareness>unaware</dpiAwareness> or no manifest entries
    2: Unaware with GDI scaling: Win10,v1809 or better
    <gdiScaling>true</gdiScaling>
    3: System aware
    <dpiAware>true</dpiAware>, <dpiAwareness>system</dpiAwareness>
    4: Per-Monitor aware v1: Win8.1 to Win10,v1607 inclusively
    <dpiAware>true/pm</dpiAware>, <dpiAwareness>permonitor</dpiAwareness>
    5: Per-Monitor aware v2: Win10,v1703 or better
    <dpiAwareness>permonitorv2</dpiAwareness>

    When project is system aware and started at a greater DPI, then dragged to 100% DPI monitor, I think the scaling is good but not nearly as good as if it were per-monitor aware. In this case, the O/S down-scales and scaling down almost always looks better than scaling up.

    However, when moving from lower to higher scales, I guess it depends on the destination DPI and fonts. I think scaling quality is related to the scale factor. Scaling at multiples of 100% should produce best quality. Use true-type or open-type fonts.

    I personally feel that every VB project should be manifested to a minimum of either system-aware or unaware-GDI scaled for a more professional appearance. System awareness may require some tweaks in scaling some controls at Form_Load, but is fairly easy to support. Scaling images may pose a challenge to many.

    Per-monitor awareness is a lot tougher, especially if you are using dependency controls that you do not have the source code for. How they handle their internal scaling could potentially change from version to version, if at all. This means needing to have version-specific scaling code, forcing a specific control version with SxS manifests, finding another control, redesign project for forms that will run in different awareness, or not entertaining per-monitor awareness
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  22. #22
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    According to what you describe, my test was a case to not expect the best result, because it was from a lower DPI to a higher one, and the relation from 120 to 288 DPI (125% to 300%) was not an integer ratio. But anyway it worked OK from what I recall, I didn't notice any problem.

    My rules for being DPI aware are quite easy:
    1) Work in twips.
    2) Leave some space around the labels, and use the right alignment.
    3) Scale images (including icons) or use different image sizes according to DPI.
    4) Declare the DPI awareness in the manifest as system aware.
    5) Leave Windows to handle multiple monitors.

    These rules are valid for integer values of TwipsPerPixelX/Y only.
    To handle non integers TwipsPerPixelX/Y, my idea is to launch another exe manifested to use GDI scaling. I say "my idea" because I still never did that in my programs.

    I value simplicity, I would like to give VB6 back the "RADness" it used to have.

  23. #23

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    @Eduardo, handling non-integral DPIs/TPPs requires very little extra code.

    I value simplicity also. However, not to the extent that it handcuffs me to near obsolete behavior. And VB project's default GUI is nearly obsolete. Restricting a form to only system-awareness or lower is going to be to restrictive in the near future, if not so already. I almost liken it to refusing to use manifests for theming of standard controls. But who knows, maybe Microsoft will come out with a magic bullet for DPI awareness.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  24. #24

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    @Eduardo, here are some screenshots. I've included all three in a zip because they have to be viewed full size to appreciate the quality differences. The forum rescales them, so they are not shown herein.

    1. PMaware.PNG is a form loaded @ 150% DPI, then scaled to 100% & 175%. With the exception of graphics (which I did not take care to scale for the samples), the two scaled forms look crisp & extremely good.

    2. SysAware.PNG is a form loaded @ 150% DPI, then scaled to 100% & 175%. You can notice subtle differences in quality when compared to the PMaware.PNG

    3. Unaware.PNG is a form loaded @ 100% DPI, then scaled to 175%. There is a snapshot of one that uses no awareness and one that uses GDI-scaling (Win10, v1809 or better). The GDI-scaled snapshot is leaps & bounds better than no manifest at all.
    Attached Files Attached Files
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  25. #25
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by LaVolpe View Post
    @Eduardo, handling non-integral DPIs/TPPs requires very little extra code.
    Yes? Handling MouseMove/Down/Up's X and Y parameters without the need of a correction factor (*), and being able to use the built-in ScaleX/Y functions of the controls? If you have hacks for those I would agree that it is simple and require very little extra code.

    (*) perhaps it might be fixed by defining a custom Scale for every container, but it would be still cumbersome (setting ScaleWidth and Scaleheight).

    Quote Originally Posted by LaVolpe View Post
    I value simplicity also. However, not to the extent that it handcuffs me to near obsolete behavior. And VB project's default GUI is nearly obsolete. Restricting a form to only system-awareness or lower is going to be to restrictive in the near future, if not so already. I almost liken it to refusing to use manifests for theming of standard controls. But who knows, maybe Microsoft will come out with a magic bullet for DPI awareness.
    I'm been saying that this issue was going to be a serious problem for about 5 years or more, and one of the main reasons why we needed to develop a new VB. No one seemed to care.

    But I'm not saying that what you are doing makes not sense. I just asked what you wanted to do in regard to fonts because VB scales them automatically for programs manifested as system aware.

  26. #26

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by Eduardo- View Post
    But I'm not saying that what you are doing makes not sense. I just asked what you wanted to do in regard to fonts because VB scales them automatically for programs manifested as system aware.
    System aware is a piece of cake -- I'm interested in per-monitor aware, which is why font scaling is needed.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  27. #27
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by LaVolpe View Post
    System aware is a piece of cake -- I'm interested in per-monitor aware, which is why font scaling is needed.
    OK, that was my original question.

  28. #28
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,584

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by Eduardo- View Post
    . . . and one of the main reasons why we needed to develop a new VB. No one seemed to care.
    No one can. It's still a question of abilities and free time.

    Most of the capable people here cannot spend the time required to implement something of this magnitude unless compensated.

    Off-topic: What happened to the last crowdsourcing effort? Many thought it was looking promising. . .

    cheers,
    </wqw>

  29. #29
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,901

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    ....
    Last edited by Eduardo-; Oct 8th, 2019 at 04:48 AM.

  30. #30
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by LaVolpe View Post
    @fafalone. How are you scaling your HFonts? If you are using basically lfHeight * scaleRatio, then that could produce sizes you may feel aren't ideal due to rounding.

    Why? Let's look at 8.75 point size @ 96 DPI and calculate its lfHeight based on Microsoft supplied algorithm
    lfHeight = -(pointSize * DPI) / 72
    lfHeight would be -12 due to rounding from single/double to long

    Now if we scale that to 175%, we get a different lfHeight value depending on the algorithm
    1. lfHeight = lfHeight * 1.75 = -21
    2. lfHeight = -(8.75 * 168) / 72 = -20 after rounding
    The second algorithm doesn't suffer from scaling previously-rounded values but requires knowledge of a pre-scaled point size
    Wait lfHeight is pixels, not 'point size'?

    I was taking the StdFont, already scaled by the SetRatio call you posted, and
    .LFHeight = -MulDiv(CLng(CrRoundUp(Font.SIZE)), GetDeviceCaps(GetDC(0), LOGPIXELSY), 72)

    Scaling it as you suggest there instead produces the same exact size as what the command buttons were coming out as, which is what I was after. Thanks!

  31. #31

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Quote Originally Posted by fafalone View Post
    Wait I was taking the StdFont, already scaled by the SetRatio call you posted, and
    .LFHeight = -MulDiv(CLng(CrRoundUp(Font.SIZE)), GetDeviceCaps(GetDC(0), LOGPIXELSY), 72)
    ... Thanks!
    Ah, the rounding up in your calculation was causing you the problems?

    And as far as lfHeight being pixels? Strictly speaking, lfHeight is a "logical units" value that gets converted to "device units" by the font mapper.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  32. #32
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Rounding up was better than not rounding up but not as good as the new way.

    e.g. the target size--the font size applied to the VB controls after .SetRatio, would be equivalent to -17, then rounding up would produce -18, not rounding up -16, then finally the new way would get -17 as well.

  33. #33

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,465

    Re: [RESOLVED] FYI: StdFont scaling for DPI awareness

    Yep, rounding in general. Problem when using MulDiv or VB equivalent -- parameters are rounded to Long, as needed, before multiplication.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width