VB.NET Redrawing and Bit-Bliting

LukeDash

New member
Joined
Jun 29, 2003
Messages
1
In VB.NET, I need to Refresh the screen after i have just blitted a bitmap to the screen, clearing it. Then I blit the bitmap a little to the right of it, making movement using a timer control. The Refresh command makes the screen flicker. Here is the code I use now:

Offscreen bitmap
Dim offscreen As New Bitmap("CharRestMini.jpg") Insert your own bitmap here
Dim i As Integer
Dim a As Integer


Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
Private Declare Function CreateCompatibleDC Lib "gdi32" Alias "CreateCompatibleDC" (ByVal hdc As IntPtr) As IntPtr
Private Declare Function DeleteDC Lib "gdi32" Alias "DeleteDC" (ByVal hdc As IntPtr) As Integer
Private Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr

Private Const SRCCOPY As Integer = &HCC0020 (DWORD) dest = source
Private Const SRCPAINT As Integer = &HEE0086
Private Const SRCAND As Integer = &H8800C6

Create the target graphics and get an hDC from it
Dim targetGraphics As System.Drawing.Graphics = Me.CreateGraphics
Dim targethDC As IntPtr = targetGraphics.GetHdc()


Create an offscreen dc and select our bitmap in to it
Dim offscreenhDC As IntPtr = CreateCompatibleDC(targethDC)
Dim oldObject As IntPtr = SelectObject(offscreenhDC, offscreen.GetHbitmap())




Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton.Click
Go2()
End Sub

this code i am going to use when i am finished with bitblt
Select our bitmap out of the dc and delete it
SelectObject(offscreenhDC, oldObject) In Pending
DeleteDC(offscreenhDC) In Pending

Release the target hDC
targetGraphics.ReleaseHdc(targethDC) In Pending
targetGraphics.Dispose() In Pending

Protected Sub Homefield_OnKeyDown(ByVal Sender As Object, _
ByVal kea As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown

If (kea.KeyCode = Keys.F) Then
Refresh I need a better way of doing this
Go2()
End If

End Sub



Private Sub Homefield_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
i = 5
a = 5
End Sub


Public Function Go2()
BitBlt(targethDC, (0 + offscreen.Width) + i, (0 + offscreen.Height) + a, Me.Width, Me.Height, offscreenhDC, 0, 0, SRCAND)
i += 5
a += 5
End Function

End Class

Should I save to an offscreen bitmap before I blit? Please can someblody give me the code to refresh the screen, in any way possible.:D

ps: This is Divils code, modified, from another thread.

Thank You!
 
In the Constructor for the Form, you can do this:
Code:
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)
Which causes the drawing of the window to be done entirely by you (except for the border and titlebar), but as long as you do all your drawing code in the Paint event, it should be flickerless. Whenever you want to refresh the window, do
Code:
Me.Invalidate
as its much faster than Refresh.


Also, if speed is not crucial, you might want to look into the GDI+. Its not as fast as standard GDI32 (its not *slow* by any means, its just not as fast as GDI32), but its built for .NET so its much more flexible and easy to use.
 
Back
Top