Hallo allerseits.
Ich habe in einem Programm eine Stelle, an der sich die Schriftgröße eines Panels dynamisch der Länge des der Caption-Eigenschaft zugewiesenen Textes anpasst, also so, dass die Schrift immer so groß wie möglich ist, aber nie am Rand rausragt. Dazu erstelle ich ein TBitmap, mit dessen Canvas.Font-Eigenschaft ich dann rumexperimentieren lasse, um am Ende die Länge des Textes in Pixeln mit der Width des Panels vergleichen zu können. Die eigentliche Funktion ist nicht das Problem, sondern das TBitmap: Nachdem ich mit "FontBitmap" gearbeitet habe, möchte ich es mit "FontBitmap.Free" wieder auflösen, aber durch diese Programmzeile wird aus irgendeinem mir schleierhaften Grund eine Zugriffsverletzung verursacht. Ich bin mir sicher, dass sie durch das ".Free" zustande kommt, denn wenn ich die Zeile rauskommentiere, kommt keine Fehlermeldung.
Wodurch könnte die Zugriffsverletzung verursacht werden? Und wie ist das überhaupt mit ".Free", braucht es das unbedingt? Was ist, wenn man es weglässt und der Speicher nicht wieder freigegeben wird? Häufen sich dann Speicherbelegungen von unbenutzen TBitmaps?
Hier der Code (FontBitmap ist noch unter var als TBitmap definiert):
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
| FontBitmap := TBitmap.Create; FontBitmap.Canvas.Font := Form2.Panel1.Font;
If Screen.Width = 800 then FontBitmap.Canvas.Font.Size := 16 Else If Screen.Width = 1280 then FontBitmap.Canvas.Font.Size := 20 Else FontBitmap.Canvas.Font.Size := 18; While (FontBitmap.Canvas.Textwidth(Frage) > Form2.Panel1.Width+2) and (FontBitmap.Canvas.Font.Size > 12) do FontBitmap.Canvas.Font.Size := FontBitmap.Canvas.Font.Size - 1; Form2.Panel1.Font.Size := FontBitmap.Canvas.Font.Size; FontBitmap.Canvas.Font := Form2.Panel2.Font; If Screen.Width = 800 then FontBitmap.Canvas.Font.Size := 14 Else If Screen.Width = 1280 then FontBitmap.Canvas.Font.Size := 18 Else FontBitmap.Canvas.Font.Size := 16; While ((FontBitmap.Canvas.Textwidth(Antwort1) > Form2.Panel2.Width+2) or (FontBitmap.Canvas.Textwidth(Antwort2) > Form2.Panel3.Width+2) or (FontBitmap.Canvas.Textwidth(Antwort3) > Form2.Panel4.Width+2)) and (FontBitmap.Canvas.Font.Size > 12) do FontBitmap.Canvas.Font.Size := FontBitmap.Canvas.Font.Size - 1; Form2.Panel2.Font.Size := FontBitmap.Canvas.Font.Size; Form2.Panel3.Font.Size := FontBitmap.Canvas.Font.Size; Form2.Panel4.Font.Size := FontBitmap.Canvas.Font.Size;
FontBitmap.Free; |
Moderiert von
Christian S.: Code- durch Delphi-Tags ersetzt