VB Custom Control Unable to draw outside preset space, cannot draw

Monday, July 7, 2014

Custom control unable to draw outside a preset space.


There is a small box which it is possible to draw in at the top left but it never changes position or size. This means if I set Location or Size to values outside of this box, the image gets clipped.


Control:



Public Class mySprite
Inherits UserControl

Dim _location As System.Drawing.Point
Dim _size As System.Drawing.Size

Dim image as System.Drawing.Image

Public Sub New()
_location = New System.Drawing.Point(0, 0)
_size = New System.Drawing.Size(1, 1)

image = System.Drawing.Image.FromFile("sprite.png")

Invalidate()
End Sub

Overloads Property Location As System.Drawing.Point
Get
Return Location
End Get
Set(ByVal value As System.Drawing.Point)
_location = value
Invalidate()
End Set
End Property

Overloads Property Size As System.Drawing.Size
Get
Return _size
End Get
Set(ByVal value As System.Drawing.Size)
_size = value
ImageBase = ScaleImage(_ImageBase, _size.Width, _size.Height)
Invalidate()
End Set
End Property

Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
'MyBase.OnPaintBackground(e)
End Sub

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As System.Drawing.Graphics = e.Graphics

g.Clear(Color.Transparent)

If Visible Then
g.DrawImage(image, 0, 0)
End If
End Sub
End Class


Usage:



Dim pic As New transPicBox
pic.Location = new System.Drawing.Point(100, 100)
pic.Size = New System.Drawing.Size(100, 100)
pic.Visible = True
Me.Controls.Add(pic)


If anyone can improve title, post or answer, please do so.







http://ift.tt/1vQr1mG