Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

  • *Experts*
Posted

In the Constructor for the Form, you can do this:

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

Me.Invalidate

as it's much faster than Refresh.

 

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...