读书人

VB2008怎么使用GDI+双缓冲技术不胜感

发布时间: 2012-03-13 11:21:10 作者: rapoo

VB2008如何使用GDI+双缓冲技术,不胜感激
最近工作中做小软件用到GDI+双缓冲,本人是菜鸟不知道如何使用,想把图形画在picturebox上,由于要画动态图形,需要刷新,可是图像屏幕一直闪烁。望老鸟们不吝赐教,最好给个使用实例,菜鸟不胜感激。谢谢

[解决办法]
我是通过重写某个方法 我以前加载LISTVIEW的时候也不停的闪烁 然后我就重写了那个方法 改了一个属性
代码如下

C# code
//用一个新的CS文件  重载  class DoubleBuffer : ListView    {        public DoubleBuffer()        {            SetStyle(ControlStyles.DoubleBuffer |ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);            UpdateStyles();        }    }
[解决办法]
根据4楼所写的VB.NET代码

'用一个新的CS文件 重载
VB.NET code
Class DoubleBuffer    Inherits ListView    Public Sub New()        SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)        UpdateStyles()    End SubEnd Class
[解决办法]
多缓冲GDI+。

Private PaintBitmap As New Bitmap(1024, 768)
Private TempBitmap As New Bitmap(1024, 768)
Private PaintGraphics As Graphics = Graphics.FromImage(PaintBitmap)
Private TempGraphics As Graphics = Graphics.FromImage(TempBitmap)
Private WindowGraphics As Graphics
Private IsStart As Boolean
Private DrawPoints() As Point
Private nBrush As Brush

Private Function GetRect(ByVal Index As Integer) As Rectangle
Dim x As Integer
Dim y As Integer
Dim w As Integer
Dim h As Integer
If DrawPoints(0).X > DrawPoints(Index).X Then
x = DrawPoints(Index).X
Else
x = DrawPoints(0).X
End If

If DrawPoints(0).Y > DrawPoints(Index).Y Then
y = DrawPoints(Index).Y
Else
y = DrawPoints(0).Y
End If

w = Math.Abs(DrawPoints(0).X - DrawPoints(Index).X)
h = Math.Abs(DrawPoints(0).Y - DrawPoints(Index).Y)

Return New Rectangle(x, y, w, h)

End Function

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
WindowGraphics = Me.CreateGraphics
PaintGraphics.Clear(Me.BackColor)
TempGraphics.Clear(Me.BackColor)
End Sub

Private Sub Form3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
ReDim DrawPoints(0)
DrawPoints(0) = New Point(e.X, e.Y)
IsStart = True
nBrush = New System.Drawing.Drawing2D.HatchBrush(52 * Rnd(), Color.FromArgb(&HFF000000 + &HFFFFFF * Rnd()), Color.FromArgb(&HFF000000 + &HFFFFFF * Rnd()))
End If
End Sub

Private Sub Form3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If IsStart Then
ReDim Preserve DrawPoints(DrawPoints.Length)
DrawPoints(DrawPoints.Length - 1) = New Point(e.X, e.Y)
Dim r1 As Rectangle
Dim i As New System.Drawing.Drawing2D.GraphicsPath
i.AddLines(DrawPoints)
r1 = Rectangle.Round(i.GetBounds)
TempGraphics.DrawImage(PaintBitmap, r1, r1, GraphicsUnit.Pixel)
Dim r2 As Rectangle
r2 = GetRect(DrawPoints.Length - 1)
TempGraphics.FillRectangle(nBrush, r2)
WindowGraphics.DrawImage(TempBitmap, r1, r1, GraphicsUnit.Pixel)
End If
End Sub

Private Sub Form3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If IsStart AndAlso e.Button = Windows.Forms.MouseButtons.Left Then


IsStart = False
ReDim Preserve DrawPoints(DrawPoints.Length)
DrawPoints(DrawPoints.Length - 1) = New Point(e.X, e.Y)
Dim r1 As RectangleF
Dim i As New System.Drawing.Drawing2D.GraphicsPath
i.AddPolygon(DrawPoints)
r1 = i.GetBounds
Dim r2 As Rectangle
r2 = GetRect(DrawPoints.Length - 1)
PaintGraphics.FillRectangle(nBrush, r2)
WindowGraphics.DrawImage(TempBitmap, r1, r1, GraphicsUnit.Pixel)
End If

End Sub

Private Sub Form3_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Not WindowGraphics Is Nothing Then
WindowGraphics.DrawImage(PaintBitmap, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel)
End If
End Sub

读书人网 >VB Dotnet

热点推荐