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.
ps: This is Divils code, modified, from another thread.
Thank You!
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.
ps: This is Divils code, modified, from another thread.
Thank You!