R
Rick
I've created a simple control that loads an image, and allows a user
to "Pan" this image. If the image is too large for the screen, the
user is able to drag the image around the screen in order to see the
parts of the image otherwise off the screen. I'm getting an
incredible amount of flicker in the image when repainting it and I'd
love for some to look over the code below and see if there's something
I can do eliminate the flicker.
Rick
Public Class PanningImage
Private m_Image As Bitmap
Private m_LastPoint As Point
Private m_StartPoint As Point
Private m_Current As Point
Public Property Image() As Bitmap
Get
Return m_Image
End Get
Set(ByVal value As Bitmap)
m_Image = value
m_Current.X = 0
m_Current.Y = 0
Me.Invalidate()
End Set
End Property
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
m_Image = New Bitmap(20, 20)
m_LastPoint = New Point(0, 0)
m_Current = New Point(0, 0)
m_StartPoint = New Point(0, 0)
End Sub
Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
Dim rect As New Rectangle
Dim left As Integer
Dim top As Integer
top = m_Current.Y
left = m_Current.X
pe.Graphics.Clear(Me.BackColor)
pe.Graphics.DrawImage(m_Image, left, top)
End Sub
Protected Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)
End Sub
Private Sub PanningImage_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
m_StartPoint.X = e.X
m_StartPoint.Y = e.Y
m_LastPoint.X = m_Current.X
m_LastPoint.Y = m_Current.Y
End Sub
Private Sub PanningImage_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim xDistance As Integer = m_StartPoint.X - e.X
Dim yDistance As Integer = m_StartPoint.Y - e.Y
Me.m_Current.X = m_LastPoint.X - xDistance
Me.m_Current.Y = m_LastPoint.Y - yDistance
If Math.Abs(xDistance) > 3 And Math.Abs(yDistance) > 3 Then
Me.Invalidate()
End If
End If
End Sub
End Class
to "Pan" this image. If the image is too large for the screen, the
user is able to drag the image around the screen in order to see the
parts of the image otherwise off the screen. I'm getting an
incredible amount of flicker in the image when repainting it and I'd
love for some to look over the code below and see if there's something
I can do eliminate the flicker.
Rick
Public Class PanningImage
Private m_Image As Bitmap
Private m_LastPoint As Point
Private m_StartPoint As Point
Private m_Current As Point
Public Property Image() As Bitmap
Get
Return m_Image
End Get
Set(ByVal value As Bitmap)
m_Image = value
m_Current.X = 0
m_Current.Y = 0
Me.Invalidate()
End Set
End Property
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
m_Image = New Bitmap(20, 20)
m_LastPoint = New Point(0, 0)
m_Current = New Point(0, 0)
m_StartPoint = New Point(0, 0)
End Sub
Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
Dim rect As New Rectangle
Dim left As Integer
Dim top As Integer
top = m_Current.Y
left = m_Current.X
pe.Graphics.Clear(Me.BackColor)
pe.Graphics.DrawImage(m_Image, left, top)
End Sub
Protected Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)
End Sub
Private Sub PanningImage_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
m_StartPoint.X = e.X
m_StartPoint.Y = e.Y
m_LastPoint.X = m_Current.X
m_LastPoint.Y = m_Current.Y
End Sub
Private Sub PanningImage_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim xDistance As Integer = m_StartPoint.X - e.X
Dim yDistance As Integer = m_StartPoint.Y - e.Y
Me.m_Current.X = m_LastPoint.X - xDistance
Me.m_Current.Y = m_LastPoint.Y - yDistance
If Math.Abs(xDistance) > 3 And Math.Abs(yDistance) > 3 Then
Me.Invalidate()
End If
End If
End Sub
End Class