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.
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"
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 ?
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"
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.