Results 1 to 16 of 16

Thread: Adjust font size

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    274

    Adjust font size

    Hello, this question was probably asked many times, but here it goes again, I want to wrap a text to a PictureBox (not a PictureBox to a text), i.e. I would like to know how to indicate the font size to be moderately similar at the height and width of a PictureBox, all this without having to resort to a loop with TextWidth or TextHeight, I'm not looking for an exact precision but one that fits visually pleasing

    how the fontsize scale is interpreted, with respect to a pixel scale
    leandroascierto.com Visual Basic 6 projects

  2. #2
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,131

    Re: Adjust font size

    I always use API and pixels.
    the API GetTextExtentPoint32 will help u get the needed sizes. and we can use the API DrawText or the API TextOut or API ExtTextOut to write the text, after we calculated the sizes.

    another way is using the "Printer" functions, Printer.TextWidth & Printer.TextHeight but Im thinking that you are already doing that?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    274

    Re: Adjust font size

    I think my question was very silly, it was as simple as this what I was looking for, thanks anyway and sorry for the trouble

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Me.ScaleMode = vbPixels
        Me.FontName = "Segoe UI"
    End Sub
    
    Private Sub Form_Resize()
        Cls
        Me.Font.Size = Me.ScaleHeight / 2
        Me.Print "A"
    End Sub
    leandroascierto.com Visual Basic 6 projects

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,453

    Re: Adjust font size

    StdFont.Size is always in Points. Never Pixels.

    You also "Me, Me, Me" a lot but then you omit it from Cls. Weird.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,482

    Re: Adjust font size

    Here's a possible solution, using built-in StdFont scaling
    Code:
        Dim sngRatio As Single, oFont As IFont
    
        Set oFont = Picture1.Font
    
        ' next 2 lines are used should the font have already been scaled at least once, to return to base-line scale
        oFont.SetRatio 96, 2540
        Set Picture1.Font = oFont
    
        sngRatio = Picture1.ScaleHeight / Picture1.TextHeight("Xyz")
        oFont.SetRatio sngRatio * 96, 2540
        Set Picture1.Font = oFont
    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}

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    274

    Re: Adjust font size

    Interesting, i didn't know the SetRatio property of ifont
    question, that represented 2540?
    leandroascierto.com Visual Basic 6 projects

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,482

    Re: Adjust font size

    Quote Originally Posted by LeandroA View Post
    Interesting, i didn't know the SetRatio property of ifont
    question, that represented 2540?
    See this msdn article.
    https://docs.microsoft.com/en-us/win...ifont-setratio

    Just FYI. For DPI aware applications, I use the Font.SetRatio quite a bit, passing its parameters as: DPI, 2540
    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}

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,453

    Re: Adjust font size

    Code:
    Const HIMETRICS_PER_INCH = 2540

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,453

    Re: Adjust font size

    Code:
        Dim sngRatio As Single, oFont As IFont
    
        Set oFont = Picture1.Font
    
        ' next 2 lines are used should the font have already been scaled at least once, to return to base-line scale
        oFont.SetRatio 96, 2540
        Set Picture1.Font = oFont
    
        sngRatio = Picture1.ScaleHeight / Picture1.TextHeight("Xyz")
        oFont.SetRatio sngRatio * 96, 2540
        Set Picture1.Font = oFont
    The lines in red look like errors, functionally no-ops. The two object references already point to the same instance. Set does not make a clone.

    Or have I missed something obvious?

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    2,582

    Re: Adjust font size

    Quote Originally Posted by dilettante View Post
    Code:
        Dim sngRatio As Single, oFont As IFont
    
        Set oFont = Picture1.Font
    
        ' next 2 lines are used should the font have already been scaled at least once, to return to base-line scale
        oFont.SetRatio 96, 2540
        Set Picture1.Font = oFont
    
        sngRatio = Picture1.ScaleHeight / Picture1.TextHeight("Xyz")
        oFont.SetRatio sngRatio * 96, 2540
        Set Picture1.Font = oFont
    The lines in red look like errors, functionally no-ops. The two object references already point to the same instance. Set does not make a clone.

    Or have I missed something obvious?
    I tested when I saw the code and strangely ObjPtr on the PictureBox Font returns a different pointer than ObjPtr on the oFont variable.
    Still, if you change some property on the oFont variable the font os the PictureBox is also updated.
    My guess was that the PictureBox must be doing a copy of the font object but still holding a variable intenally with events to get the changes of the original font object and to apply to them the copy (just a guess).

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,229

    Re: Adjust font size

    Quote Originally Posted by Eduardo- View Post
    I tested when I saw the code and strangely ObjPtr on the PictureBox Font returns a different pointer than ObjPtr on the oFont variable.
    This is normal. Check out this snippet

    Code:
        Dim oFont As StdFont
        Dim pFont As IFont
        Dim pUnk As IUnknown
        Dim pDisp1 As Object
        Dim pDisp2 As Object
        
        Set oFont = New StdFont
        Set pFont = oFont
        Set pUnk = oFont
        Debug.Print ObjPtr(oFont)
        Debug.Print ObjPtr(pFont)
        Debug.Print ObjPtr(pUnk)
        Set pDisp1 = oFont
        Set pDisp2 = pUnk
        Debug.Print ObjPtr(pDisp1)
        Debug.Print ObjPtr(pDisp2)
    I get this

    Code:
     126825932 
     126825928 
     126825928 
     126825932 
     126825932
    PictureBox.Font is declared StdFont, not IFont. The IFont interface is IUnknown based, not IDispatch. StdFont is a coclass, declared with IFontDisp and FontEvents both dispinterfaces. (Dispinterfaces are IDispatch based.)

    cheers,
    </wqw>

  12. #12
    PowerPoster
    Join Date
    Feb 2017
    Posts
    2,582

    Re: Adjust font size

    Ah, OK, I understand. Now I remember that when different interfaces point to the same object, ObjPtr returns different pointers.

    Edit: better to use Is to know whether it is the same object.

    Code:
    Debug.Print ObjPtr(Picture1.Font), ObjPtr(oFont), Picture1.Font Is oFont

  13. #13
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,482

    Re: Adjust font size

    Interesting ... so the "Is" operator is smarter than just comparing object addresses.

    To expand on wqweto's example:

    Code:
    
        Dim oFont As StdFont
        Dim pFont As IFont
    
        Set oFont = New StdFont
        Set pFont = oFont
    
    
        Debug.Print ObjPtr(oFont)   ' Reports the pointer.
        Debug.Print ObjPtr(pFont)   ' Reports ObjPtr(oFont) - 4
    
        Debug.Print oFont Is pFont  ' Reports "True"
    
    
    EDIT: Or, said differently, apparently the "Is" operator is smart enough to figure out if it's the same object, regardless of how many interfaces that object has.
    Last edited by Elroy; Aug 6th, 2020 at 09:31 AM.
    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.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,482

    Re: Adjust font size

    Quote Originally Posted by Elroy View Post
    EDIT: Or, said differently, apparently the "Is" operator is smart enough to figure out if it's the same object, regardless of how many interfaces that object has.
    IS method performs a QueryInterface call. Basically, VB is asking does ThatObject implement/inherit ThisObject or vice versa. In order to be true, at some point ThatObject must have been created/retrieved from ThisObject or vice versa.

    Per COM rules: "It Must Be Possible to Query Successfully for Any Interface on an Object from Any Other Interface."
    Details can be found here, if interested: https://docs.microsoft.com/en-us/win...ace.%20More%20
    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}

  15. #15
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,229

    Re: Adjust font size

    Quote Originally Posted by Elroy View Post
    EDIT: Or, said differently, apparently the "Is" operator is smart enough to figure out if it's the same object, regardless of how many interfaces that object has.
    Is operator first casts to IUnknown and then compares the ObjPtr of these references.

    Check out Rules for Implementing QueryInterface article. The first one is so called Identity rule: "For any given object instance, a call to QueryInterface with IID_IUnknown must always return the same physical pointer value" and this is basicly what the Is operator uses.

    Mind that this rule does *not* hold for IDispatch casts for instance, it's only for IUnknown casts. I.e. an instance of a coclass with two interfaces (IAnimal and IFrog) can when pointers to both these interfaces are cast to (QueryInterface'd for) IDispatch return different "physical pointer value" (a.k.a. addresses, a.k.a ObjPtr).

    Edit: LaVolpe beat me to the link :-))

    cheers,
    </wqw>

  16. #16
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,482

    Re: Adjust font size

    Quote Originally Posted by wqweto View Post
    Is operator first casts to IUnknown and then compares the ObjPtr of these references.
    That actually makes perfect sense. And, the link states...

    For any given object instance, a call to QueryInterface with IID_IUnknown must always return the same physical pointer value.
    ...which basically just affirms what you said.

    All stuff that's good to know that I hadn't really thought about before. I typically use the "Is" operator, but I could have seen myself comparing ObjPtr() values and not thinking anything of it.
    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.

Posting Permissions

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



Click Here to Expand Forum to Full Width