Results 1 to 10 of 10

Thread: [RESOLVED] Help with CREATEFONT API

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Resolved [RESOLVED] Help with CREATEFONT API

    I have a small app I built, to fundamentally get some rotated text in a Picturebox, and then crop the picturebox to just fit the bounds of the text.

    This does work.

    I use the CreateFont API to establish a rotated font.
    I assign that font to the Picturebox.
    I set the picturebox currentx and currenty, the use "Print" to draw the text.
    This does work (at least I get rotated text in the Picturebox).

    Now the problem.
    For a testing environment, I created a simple form, with some user input
    objects, so a person could select a different font, size, forecolor, bold, italics, etc.
    These objects all have labels identifying them, and then there are text boxes, lists, and checkboxes to specify the options.

    There is a command button to actually perform the work.

    So, run the app... either in development mode, or compiled to an .exe.
    Click on the "Doit" button.... great, it rotates the text.
    Just do this a few times, repeating the same stuff.
    Now click on the "Bold" checkbox. Then "Doit". You might have to "Doit" a few times.
    BANG... the font for the Bold checkbox now changes to bolded font.
    And I think the fontname/face also is changed.
    Now click on Italics checkbox. Then Doit... the Italics checkbox changes to bolded font.
    Keep doing this with all check boxes... they change to bolded font.
    Eventually the "Doit" button also changes.

    If you are in development mode, and you then stop the program from running, the controls/objects on the form are STILL BOLDED font.
    You actually have to close the project, and start it up again.

    If you run the .exe, the same thing happens.

    Something is walking all over something else.

    I am running Vb6 (Visual Studio 6), with latest upgrades, Vista Ultimate,
    on a pretty powerful (dual core, quad, 2.4GHZ, 4GB, 500GBDualRaid).

    Can anyone figure out if the CreateFont API might be causing this?

    By the way, I also do a BitBlt to copy the desired area of the picturebox to another picturebox.

    Might THIS be causing the problem ?

    I attach the project code for anyone wanting to look at it.

    Thanks
    Attached Files Attached Files

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

    Re: Help with CREATEFONT API

    When stuff like that happens, it is usually an indication you have memory leaks. I see one that you have which is probably causing your problems...

    Picture1 is where you are selecting a memory font into. While the font is selected, you are modifying the size of Picture1. The leak occurs then. When you get enough leaks, you sometimes see this in the fonts and not just your app, could be for all apps eventually.

    Why is the code leaking memory/resources? Here is why.
    The Picture1 object has its AutoRedraw set to True. When this is the case, when the DC is modified, VB goes and creates a new DC. Modifying DCs can happen when control is resized, .CLS, etc. What happend to the old DC? It was destroyed. What happend to the font you selected into that DC? It was leaked.

    To verify what I am talking about, try this simple experiment in a new project:
    Code:
    Private Sub Command1_Click()
       Picture1.AutoRedraw=False
       Debug.Print Picture1.hDC: Picture1.Cls: Debug.Print Picture1.hDC, " DCs are same"
       Picture1.AutoRedraw=True
       Debug.Print Picture1.hDC: Picture1.Cls: Debug.Print Picture1.hDC, " DCs changed"
    End Sub
    So to fix the problem... Simply remove and destroy the memory font before you modify Picture1's 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

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Re: Help with CREATEFONT API

    LaVolpe,
    Thanks for your response. Very Enlightening.

    But I think I have a problem with your recommendation.

    Yes, Picture1 has Autoredraw set to true.
    So if I change it to false, my problem still exists.

    So I try to move my code around to remove and destroy the memory font
    before I modify the Picture1 properties.
    But I can't do that because I have to set the forecolor, before I actually do the Print to the picture.

    If I set picture2 Autoredraw to false, then I never see the results in it.

    So I can't figure out how to remove/destroy the font BEFORE I set at least one property.

    Can you look further at my code, and suggest how I can do this ?

    Thanks very much.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with CREATEFONT API

    I'm sure LaVolpe has the perfect solution, but this seems to fix your code, I used the same sort of fix in my attachement in your other thread.
    Code:
        ' ..............
        If lst_font.ListIndex <> -1 Then sFontname = lst_font.List(lst_font.ListIndex)
            
        Picture1.ScaleMode = vbPixels
        picwidthinpixels = Picture1.ScaleWidth
        PixelstoTwips = picwidthintwips / picwidthinpixels
        
        newfont = CreateFont(FontSize * 2, 0, _
               Rotateangle, Rotateangle, _
               iBold, _
               bItalics, bUnderscore, _
               bStrikethru, 0, 0, _
               16, 0, 0, sFontname)
        oldfont = SelectObject(Picture1.hDC, newfont)
    
        Textlength = Picture1.TextWidth(Rotatetext) + 2    'add 2 to make sure it is a bit longer
        Textheight = Picture1.Textheight(Rotatetext) + 1    'add 1 to make sure it is a bit taller
        
        DeleteObject SelectObject(Picture1.hDC, oldfont)
        
        Picture1.Width = (Textlength * PixelstoTwips) + (2 * (Textheight * PixelstoTwips)) 'make it a bit bigger
        Picture1.Height = Picture1.Width
        Picture1.ScaleMode = vbTwips
            
            newfont = CreateFont(FontSize * 2, 0, _
               Rotateangle, Rotateangle, _
               iBold, _
               bItalics, bUnderscore, _
               bStrikethru, 0, 0, _
               16, 0, 0, sFontname)
        oldfont = SelectObject(Picture1.hDC, newfont)
            
        Dim YFactor
        ' .................

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

    Re: Help with CREATEFONT API

    Antithesus, the solution is simple as EdgeMeal points out: Remove the font, change your picture1 props as needed, reselect the font (the DC will have changed by then). With EdgeMeal's post above, it is not necessary to recreate the font twice. Simply unselect it and the reselect it later.
    Code:
        oldFont = SelectObject(Picture1.hDC, newFont)
        ... draw your text
        SelectObject Picture1.hDC, oldFont
        ... change picture1 properties
        oldFont = SelectObject(Picture1.hDC, newFont)
        ... do other things
        DeleteObject SelectObject(Picture1.hDC, oldFont)
        ...
    Edited: The font only needs to be selected into the DC just before you draw the text and can be removed anytime afterwards, before the DC is destroyed.
    Last edited by LaVolpe; Oct 20th, 2008 at 07:44 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}

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with CREATEFONT API

    Quote Originally Posted by LaVolpe
    The font only needs to be selected into the DC just before you draw the text and can be removed anytime afterwards, before the DC is destroyed.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    89

    Thumbs up Re: Help with CREATEFONT API

    Wow, thanks you guys... Great Stuff.

    Edgemeal,
    I took your changes and implemented them, but with LaVolpe's information that I don't have to create the font twice.

    It works great.

    Thanks you guys... PERFECT !!!

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with CREATEFONT API

    Quote Originally Posted by Antithesus
    It works great.

    Thanks you guys... PERFECT !!!
    Ya not bad, now if you could just implement what Lord Orwell was talking about in your other thread it would be a lot faster. I've used the API he's talking about but have no clue on using it to help figure out text size on angles.

  9. #9
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: Help with CREATEFONT API

    LaVolpe,

    I need to write a short routine to draw rotated text on a PictureBox.
    Can you please give me the API declaration. Further I might be OK.

    Thanks
    PK

  10. #10
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Help with CREATEFONT API

    Quote Originally Posted by Peekay View Post
    LaVolpe,

    I need to write a short routine to draw rotated text on a PictureBox.
    Can you please give me the API declaration. Further I might be OK.

    Thanks
    PK
    You've bumped a 14 year old thread, and LaVolpe no longer posts here.

    https://www.vbforums.com/showthread....0300-FYI-Adios

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