Help with drawing?

G

Guest

Hi guys,

I have the following code which is supposed to draw a grid in a panel
consisting of a specified number of squares. I can get it to draw the grid
and activate the autoscroll feature, however I cant get it to scroll
properly. I dont want to make the grid a bitmap and move that as the grid
can get very large and I want to keep memory and performance as high as
possible.

Is there an easy way to get the grid to to scroll so that I can see the
entire image?

Cheers for any help

Niels

///

'Form1 need to be sized at "Size(328, 344)" and Requires a Panel.
'This Prog Demonstrates drawing a graphical grid on a panel
'then if the grid is too large activating the scroll bars to
'scroll the image around - unfortunateley the scroll doesnt work.
'Niels Jensen 25-Jul-2005

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Location = New System.Drawing.Point(8, 8)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(304, 304)
Me.Panel1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 317)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim GridImage As Graphics = Panel1.CreateGraphics
Dim i As Integer
Dim countX = 30
Dim countY = 30
Dim SquareSize As Integer = 20
Dim linelengthx As Integer = SquareSize * countX
Dim linelengthy As Integer = SquareSize * countY
' Dim NOOFX As Integer = countx
' Dim NOOFY As Integer = county

'Draw The Grid
For i = 0 To countX
GridImage.DrawLine(Pens.Black, _
i * SquareSize, 0, _
i * SquareSize, linelengthy)
Next i
For i = 0 To countY
GridImage.DrawLine(Pens.Black, _
0, i * SquareSize, _
linelengthx, i * SquareSize)
Next i

'calculate scrollbars
If Panel1.Height <= linelengthy Then
Panel1.AutoScroll = True
Panel1.AutoScrollMinSize = New Size(linelengthx, linelengthy)
Else
Panel1.AutoScroll = False
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Panel1.Size = New Size(304, 304)
Panel1.Location = New Point(8, 8)
End Sub
End Class
 
R

Robin Tucker

Here is some goofy code for your paint method. What it does is render the
content of the panel to a bitmap (for double buffering so it doesn't
flicker). The important thing to note is (1) in your example, you didn't
take account of the panel scroll position, which is given with
Panel1.AutoScrollPosition and (2) in my solution with double bufferring, I'm
just offsetting the grid by taking Mod of the square size with the
autoscroll position. To make your example work right away without double
buffering, just add AutoScrollPosition.X to the X coords in the first loop
and AutoScrollPosition.Y to the Y coords in the second loop.

Also, remember to clear the control first (GridImage.Clear) or it won't
"animate".

Hope this helps.


Dim theBitmap As New Bitmap(Panel1.Width, Panel1.Height)
Dim theBitmapG As Graphics = Graphics.FromImage(theBitmap)

theBitmapG.Clear(System.Drawing.SystemColors.Control)

For i = 0 To countX
theBitmapG.DrawLine(Pens.Black, _
(Panel1.AutoScrollPosition.X Mod SquareSize) + (i * SquareSize), 0,
_
(Panel1.AutoScrollPosition.X Mod SquareSize) + (i * SquareSize),
linelengthy)
Next i


For i = 0 To countY
theBitmapG.DrawLine(Pens.Black, _
0, (Panel1.AutoScrollPosition.Y Mod SquareSize) + (i * SquareSize),
_
linelengthx, (Panel1.AutoScrollPosition.Y Mod SquareSize) + (i *
SquareSize))
Next i

GridImage.DrawImage(theBitmap, 0, 0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top