how can I print text onto a picturebox so that the text fits within the picturebox width. the picture box width will vary so the text size must as well.
been stuck on this for way too long and am in need of any advice!!
Printable View
how can I print text onto a picturebox so that the text fits within the picturebox width. the picture box width will vary so the text size must as well.
been stuck on this for way too long and am in need of any advice!!
Here is one solution. It's as blinky as a label, but if you're gonna BitBlt something on the form instead of doing the backcolor changing thing, then you'll have something working.
VB Code:
Option Explicit Private Sub Form_Resize() Picture1.Move 0, 0, ScaleWidth, ScaleHeight End Sub Private Sub Picture1_Resize() Dim A As Long, B As Long, Temp As String Picture1.CurrentY = 0 Picture1.BackColor = Picture1.BackColor Temp = "Mary has a little lamb which is a very white and small and eats all the grass it can see" Do Until Temp = "" A = InStr(B + 1, Temp, Chr(32)) If A < 1 Then Picture1.Print Temp: Exit Do If Picture1.TextWidth(Left$(Temp, A - 1)) > Picture1.ScaleWidth Then If B < 1 Then Picture1.Print Temp: Exit Do Else Picture1.Print Left$(Temp, B - 1) Temp = Mid$(Temp, B + 1) B = 0 End If Else B = A End If DoEvents Loop End Sub
And if you want to center the text:
(note! this code is unoptimized! it's possible to speed up this code a bit...)
VB Code:
Option Explicit Private Sub Form_Resize() Picture1.Move 0, 0, ScaleWidth, ScaleHeight End Sub Private Sub Picture1_Resize() Dim A As Long, B As Long, Temp As String Picture1.CurrentY = 0 Picture1.BackColor = Picture1.BackColor Temp = "Mary has a little lamb which is a very white and small and eats all the grass it can see" Do Until Temp = "" A = InStr(B + 1, Temp, Chr(32)) If A < 1 Then Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2: Picture1.Print Temp: Exit Do If Picture1.TextWidth(Left$(Temp, A - 1)) > Picture1.ScaleWidth Then If B < 1 Then Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2 Picture1.Print Temp: Exit Do Else Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Left$(Temp, B - 1))) / 2 Picture1.Print Left$(Temp, B - 1) Temp = Mid$(Temp, B + 1) B = 0 End If Else B = A End If DoEvents Loop End Sub
I guess changing that code that it does right alignment isn't too hard :rolleyes:
And one that doesn't blink!
- Make a new project
- Include two pictureboxes
- Picture1.AutoRedraw = True
- Picture1. Visible = False
- Change fontsize, colors etc.
- Include the code and run!
VB Code:
Option Explicit Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Sub Form_Resize() Picture2.Move 0, 0, ScaleWidth, ScaleHeight Picture1.Move 0, 0, ScaleWidth, ScaleHeight End Sub Private Sub Picture1_Resize() Dim A As Long, B As Long, Temp As String Picture1.CurrentY = 0 Picture1.BackColor = Picture1.BackColor Temp = "Mary has a little lamb which is a very white and small and eats all the grass it can see " Do Until Temp = "" A = InStr(B + 1, Temp, Chr(32)) If A < 1 Then Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2: Picture1.Print Temp: Exit Do If Picture1.TextWidth(Left$(Temp, A - 1)) > Picture1.ScaleWidth Then If B < 1 Then Temp = Trim$(Temp) Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2 Picture1.Print Temp: Exit Do Else Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Left$(Temp, B - 1))) / 2 Picture1.Print Left$(Temp, B - 1) Temp = Mid$(Temp, B + 1) B = 0 End If Else B = A End If DoEvents Loop BitBlt Picture2.hDC, 0, 0, Picture2.ScaleX(Picture2.ScaleWidth, Picture2.ScaleMode, vbPixels), Picture2.ScaleY(Picture2.ScaleHeight, Picture2.ScaleMode, vbPixels), Picture1.hDC, 0, 0, vbSrcCopy End Sub