Results 1 to 4 of 4

Thread: print text on picturebox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Edmonton, Alberta Canada
    Posts
    192

    print text on picturebox

    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!!

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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:
    1. Option Explicit
    2. Private Sub Form_Resize()
    3.     Picture1.Move 0, 0, ScaleWidth, ScaleHeight
    4. End Sub
    5. Private Sub Picture1_Resize()
    6.     Dim A As Long, B As Long, Temp As String
    7.     Picture1.CurrentY = 0
    8.     Picture1.BackColor = Picture1.BackColor
    9.     Temp = "Mary has a little lamb which is a very white and small and eats all the grass it can see"
    10.     Do Until Temp = ""
    11.         A = InStr(B + 1, Temp, Chr(32))
    12.         If A < 1 Then Picture1.Print Temp: Exit Do
    13.         If Picture1.TextWidth(Left$(Temp, A - 1)) > Picture1.ScaleWidth Then
    14.             If B < 1 Then
    15.                 Picture1.Print Temp: Exit Do
    16.             Else
    17.                 Picture1.Print Left$(Temp, B - 1)
    18.                 Temp = Mid$(Temp, B + 1)
    19.                 B = 0
    20.             End If
    21.         Else
    22.             B = A
    23.         End If
    24.         DoEvents
    25.     Loop
    26. End Sub

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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:
    1. Option Explicit
    2. Private Sub Form_Resize()
    3.     Picture1.Move 0, 0, ScaleWidth, ScaleHeight
    4. End Sub
    5. Private Sub Picture1_Resize()
    6.     Dim A As Long, B As Long, Temp As String
    7.     Picture1.CurrentY = 0
    8.     Picture1.BackColor = Picture1.BackColor
    9.     Temp = "Mary has a little lamb which is a very white and small and eats all the grass it can see"
    10.     Do Until Temp = ""
    11.         A = InStr(B + 1, Temp, Chr(32))
    12.         If A < 1 Then Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2: Picture1.Print Temp: Exit Do
    13.         If Picture1.TextWidth(Left$(Temp, A - 1)) > Picture1.ScaleWidth Then
    14.             If B < 1 Then
    15.                 Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2
    16.                 Picture1.Print Temp: Exit Do
    17.             Else
    18.                 Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Left$(Temp, B - 1))) / 2
    19.                 Picture1.Print Left$(Temp, B - 1)
    20.                 Temp = Mid$(Temp, B + 1)
    21.                 B = 0
    22.             End If
    23.         Else
    24.             B = A
    25.         End If
    26.         DoEvents
    27.     Loop
    28. End Sub


    I guess changing that code that it does right alignment isn't too hard

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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:
    1. Option Explicit
    2. 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
    3. Private Sub Form_Resize()
    4.     Picture2.Move 0, 0, ScaleWidth, ScaleHeight
    5.     Picture1.Move 0, 0, ScaleWidth, ScaleHeight
    6. End Sub
    7. Private Sub Picture1_Resize()
    8.     Dim A As Long, B As Long, Temp As String
    9.     Picture1.CurrentY = 0
    10.     Picture1.BackColor = Picture1.BackColor
    11.     Temp = "Mary has a little lamb which is a very white and small and eats all the grass it can see "
    12.     Do Until Temp = ""
    13.         A = InStr(B + 1, Temp, Chr(32))
    14.         If A < 1 Then Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2: Picture1.Print Temp: Exit Do
    15.         If Picture1.TextWidth(Left$(Temp, A - 1)) > Picture1.ScaleWidth Then
    16.             If B < 1 Then
    17.                 Temp = Trim$(Temp)
    18.                 Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Temp)) / 2
    19.                 Picture1.Print Temp: Exit Do
    20.             Else
    21.                 Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Left$(Temp, B - 1))) / 2
    22.                 Picture1.Print Left$(Temp, B - 1)
    23.                 Temp = Mid$(Temp, B + 1)
    24.                 B = 0
    25.             End If
    26.         Else
    27.             B = A
    28.         End If
    29.         DoEvents
    30.     Loop
    31.     BitBlt Picture2.hDC, 0, 0, Picture2.ScaleX(Picture2.ScaleWidth, Picture2.ScaleMode, vbPixels), Picture2.ScaleY(Picture2.ScaleHeight, Picture2.ScaleMode, vbPixels), Picture1.hDC, 0, 0, vbSrcCopy
    32. End Sub

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