Re: Scrolling text in a stationary label help needed

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



<jcrouse1@xxxxxxxxxxx> wrote in message news:1158715983.824151.230330@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Second, I am trying to figure out how to have the text exit the
left side of the picturebox before it starts scrolling on the right
side again. I am trying to somehow replace the "space$(12)"
in the following example with . . . . .

Using the TextWidth function to get your values you can simply divide the TextWidth of the entire string by the TextWidth of a single space character. That will tell you how many spaces take up the same length as the string. You then add the required number of spaces to the string. However, there is another way of achieving what you are after which does not require you to do that and which actually makes the scrolling loop code a little simpler. Perhaps an explanation of how the existing method works will help:

At the moment the first time the string is printed into the Picture box it is printed at x coordinate zero (using Picture1.CurrentX to set the x coordinate).

When the time comes to move the string to the left by one pixel the following happens. For this explanation we'll assume that the string to be scrolled is "Rum and Coke" (with no spaces just to make the explanation a little easier to understand).

1. Any existing displayed text is cleared using the Cls method.
2. The x coordinate is decremented (it goes from zero to minus 1)
3. The string is printed again at the new x coordinate.

This causes VB to print the string a little to the left, with the "unprinted pixels" that fall to the left of zero simply being clipped (because they are outside the left edge of the picture box).

The above sequence is repeated (x = 0, x = -1, x = -2, x = -3, etc) until we reach a point where the negative value of x is greater in magnitude than the pixel width of the leftmost character in the string ("R"). At this point we change the actual string itself, by "chopping off" its current leftmost character and adding it at the end. In other words, at this point the original string of (say) "Rum and Coke" becomes (um and CokeR). We then set the x coordinate back to zero before printing the new string.

The above sequence is then repeated with the new string, each time decrementing the x coordinate by 1, until we again reach the point where the negative value of x is greater in magnitude than the "new first character" of the string ("u"). We then again change actual string by chopping off the current first character and adding it to the end. This time we end up with "m and CokeRu" (remember, to simplify things for the explanation just so that you can more easily see the result in a printed newsgroup posting we are using a string with no trailing space characters). We again set the x coordinate back to zero and proceed as before. The above stuff simply loops over and over again until we decide to stop it.

If you want to do it slightly differently (so that the entire string rolls off to the left before it starts to appear again at the right) you can simply calculate and add sufficient space characters at the right of the string that are equal in TextWidth to the string itself, as I suggested at the start of this reply. However, for such a display there is an easier way of doing it (easier to understand, that is). All you need to do is to initially display the string at an x coordinate with a positive value that is equal to the pixel width of the picture box, and each time you want to scroll left by a pixel you decrement the x coordinate by 1 and display the string again, but this time instead of stopping when you reach a value with a magnitude greater than the width of the first character you simply keep on decrementing the x coordinate (which initially started with a positive value equal to the width of the picture box) and you keep decrementing it, passing through zero and continuing to decrement it until the negative x coordinate is equal in magnitude to the pixel width of the entire string. You then start again by setting the x coordinate to a positive value equal to the pixel width of the picture box. This method is simpler because at no point does it require you to mess about "chopping characters off from the left and adding them to the right" of the string. You just leave the string exactly in its original state at all times.

Here's an example.

Mike

Option Explicit
Private objDX As New DirectX7
Private objDD As DirectDraw7

Private Sub Form_Load()
Set objDD = objDX.DirectDrawCreate("")
Me.Picture = LoadPicture("c:\tulips.jpg")
End Sub

Private Sub Scroll(x As Long, y As Long, _
picwide As Long, pichigh As Long)
Dim stext As String
Dim wide As Long, xposition As Long
Me.ScaleMode = vbPixels
Me.Width = Screen.Width * 0.9
With Picture1
.Visible = False
.BorderStyle = vbBSNone
.BackColor = RGB(123, 156, 189)
.AutoRedraw = True
.ScaleMode = vbPixels
.FontName = "Times New Roman"
.FontSize = 16
.FontBold = True
.ForeColor = vbBlue
.Cls
stext = "A simple demonstration of some horizontally "
stext = stext & "scrolling text in a picture box."
.Move x, y, picwide, pichigh
DoEvents
.PaintPicture Me.Picture, 0, 0, picwide, pichigh, _
x, y, picwide, pichigh
.Picture = .Image
.Visible = True
End With
wide = Picture1.TextWidth(stext)
Do
For xposition = picwide To -wide Step -1
Picture1.Cls
Picture1.CurrentX = xposition
Picture1.Print stext;
objDD.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, ByVal 0
DoEvents
Next xposition
Loop
End Sub

Private Sub Command1_Click()
Scroll 100, 200, 500, 24
End Sub






.



Relevant Pages

  • Re: When I type fff the first two letters are closer than the last two
    ... the layout is in fact very dependent on resolution. ... lines up with the pixel positions. ... Both the character outline, and the "bounding box" that says how wide ... but if you have a string of the same letter the ...
    (microsoft.public.word.pagelayout)
  • RE: Adding Bound Pictures to an Access Database
    ... when the student's picture is missing it is leaving the ... Private Sub Form_Current ... Dim strPath As String ... Private Function pfValidFile(aFile As String) As Boolean ...
    (microsoft.public.access.modulesdaovba)
  • Re: [PHP] Question regarding fopen
    ... -s stands for small and 0 is the picture index. ... random 8 character string had become a totally different 8 character ... My guess is that the mkdir is failing and the file is going to some weird ...
    (php.general)
  • RE: Adding Bound Pictures to an Access Database
    ... Just add an Else to the If statement that checks for a picture: ... Me.ctlPersonPicture.Picture = strPath ... Private Sub Form_Current ... The Lenfunction returns the number of characters in a string. ...
    (microsoft.public.access.modulesdaovba)
  • RE: Adding Bound Pictures to an Access Database
    ... Dim strPath As String ... Private Function pfValidFile(aFile As String) As Boolean ... except in the case where I don't have a picture for a student. ... you have the Image control, not either of the Object controls. ...
    (microsoft.public.access.modulesdaovba)