Question

My user-control is intended to operate as a smooth vertical text scroller. It renders the text (to be scrolled) to control's surface only once, using TextRenderer.DrawText. It then starts a timer, which upon each of its ticks, Bit-Blits (BitBlt) the entire client-rectangle one pixel up. control's device-context is both the source and the destination of the BitBlt operation, as follows:

Protected Sub handleTimerTick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim oG As Graphics = Me.CreateGraphics()
    Dim sHdc As IntPtr = oG.GetHdc()
    Dim iRes As Integer = BitBlt(sHdc, 0, 0, Me.ClientRectangle.Size.Width, Me.ClientRectangle.Size.Height, sHdc, 0, 1, SRCCOPY)
    oG.ReleaseHdc(sHdc)
    oG.Dispose()
End Sub

This successfully accomplishes the desired scroll effect, BUT only if my control's background is a solid-color (e.g. Me.BackColor = Color.Gray).

If I set a picture as control's background, BitBlt scrolls the background along with the text displayed over it. Ofcourse, I'd like only the text to be scrolled, and the background image to remain static.

I have found the following thread, which suggests using TransparentBlt instead of BitBlt where the background is a solid uniform color to be ignored : How to copy with BitBlt?

The solution suggested there would not be suitable to the case on hand, in which a colorful background is used.

Please note that the text itself is of a solid uniform color.

Your advice would be much appreciated. If it is of any matter, I'm using VB.NET 2005.

Was it helpful?

Solution

If the background is supposed to stay fixed while the text moves around on it, I would suggest that you maintain two separate bitmaps: one that contains the background image, and a second one that contain the text on a transparent background.

Then, on each timer tick, you draw the background bitmap first and then draw the text bitmap at the desired offset position.

I don't understand why you need to P/Invoke the BitBlt function for this. What's wrong with the equivalent GDI+ function, wrapped by the .NET Framework already as Graphics.DrawImage?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top