Welcome Guest!
 VB.NET Helper
 Previous Message All Messages Next Message 
VB .NET Helper Newsletter  Rod Stephens
 Apr 09, 2009 10:50 PDT 

Special thanks to Steve Smerker for posting a review of my database book
on the Barnes and Noble Web site:

    
http://search.barnesandnoble.com/Beginning-Database-Design-Solutions/Rod-Stephens/e/9780470385494/?itm=1#TABS

----------
This week I've posted Visual Basic 6 and Visual Basic .NET versions of a
simple drawing program that lets you show or hide a grid and turn grid
snapping on and off. If you compare them, you'll see many of the biggest
differences between drawing in Visual Basic Classic and Visual Basic
.NET.
----------
Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***

Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========

    VB.NET Contents:
1. Updated HowTo: Make a scribble application with VB .NET that
automatically redraws when necessary
2. New HowTo: Make a simple drawing program with a "snap to" grid and
rules in Visual Basic .NET
3. New Tip: Visual Basic Data Type Characters

    Both Contents:
4. New Links
==========
++++++++++
<VB.NET>
++++++++++
==========
1. Updated HowTo: Make a scribble application with VB .NET that
automatically redraws when necessary
http://www.vb-helper.com/howto_net_scribble_autoredraw.html
http://www.vb-helper.com/HowTo/howto_net_scribble_autoredraw.zip

When the form loads or is resized, the program calls the AllocateBitmap
subroutine. This routine makes a Bitmap to fill the form, making it no
smaller than it was previously. It makes a Graphics object to draw on
the Bitmap and sets the PictureBox's Image property to the Bitmap to
display it. (Thanks to Tom Erik Voll for this suggestion.)

In the MouseDown event handler, the program sets m_Drawing to True and
saves the current point.

In the MouseMove event handler, the program verifies that m_Drawing is
True. If it is, the code draws a line on the Bitmap from the previously
saved point to the current point. It then sets the PictureBox's Image
property to the Bitmap to display the revised image. It finishes by
saving the new current point.

In the MouseUp event handler, the program sets m_Drawing to False.


Private m_Drawing As Boolean
Private m_LastX As Integer
Private m_LastY As Integer

Private m_Bitmap As Bitmap
Private m_Graphics As Graphics

' Allocate the first Bitmap.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    AllocateBitmap()
End Sub

' Make a new Bitmap to draw on.
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Resize
    AllocateBitmap()
End Sub

' Make a new Bitmap to draw on.
Private Sub AllocateBitmap()
    ' Get the new desired size.
    Dim wid As Integer = Me.ClientSize.Width
    Dim hgt As Integer = Me.ClientSize.Width

    ' See if we already have a Bitmap.
    If (m_Bitmap Is Nothing) Then
        ' Make a Bitmap.
        m_Bitmap = New Bitmap(wid, hgt)
    Else
        ' See if this is smaller than the previous size.
        If wid < m_Bitmap.Width Then wid = m_Bitmap.Width
        If hgt < m_Bitmap.Height Then hgt = m_Bitmap.Height

        ' If it's the same size as before, skip it.
        If (wid = m_Bitmap.Width) And (hgt = _
            m_Bitmap.Height) Then Exit Sub

        ' Make a new Bitmap.
        Dim bm As New Bitmap(wid, hgt)

        ' Copy the old picture into the new one.
        Using gr As Graphics = Graphics.FromImage(bm)
            gr.DrawImage(m_Bitmap, 0, 0, m_Bitmap.Width, _
                m_Bitmap.Height)
        End Using

        ' Save the new picture.
        m_Bitmap = bm
    End If

    ' Make a Graphics object to draw on the Bitmap.
    m_Graphics = Graphics.FromImage(m_Bitmap)

    ' Make the PictureBox show the Bitmap.
    picCanvas.Image = m_Bitmap
End Sub

' Start scribbling.
Private Sub picCanvas_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseDown
    m_Drawing = True
    m_LastX = e.X
    m_LastY = e.Y
End Sub

' Continue scribbling.
Private Sub picCanvas_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseMove
    If m_Drawing Then
        ' Draw the new line.
        m_Graphics.DrawLine(Pens.Black, m_LastX, m_LastY, _
            e.X, e.Y)

        ' Display the result.
        picCanvas.Image = m_Bitmap

        ' Save the latest point.
        m_LastX = e.X
        m_LastY = e.Y
    End If
End Sub

' Stop scribbling.
Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseUp
    m_Drawing = False
End Sub
==========
2. New HowTo: Make a simple drawing program with a "snap to" grid and
rules in Visual Basic .NET
http://www.vb-helper.com/howto_net_rulers_grid.html
http://www.vb-helper.com/HowTo/howto_net_rulers_grid.zip

When you press the mouse down on the main drawing canvas, the code
starts drawing. It sets m_Drawing to True so it can remember that it is
drawing. It calls SnapToGrid to snap the current point to the nearest
grid point and saves that point. It invalidates the drawing area to
redraw and calls ShowMousePosition to show the mouse position in the
rulers usin the "drawing" color.

' Start drawing a line.
Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
    ' Only on left mouse down.
    If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
    m_Drawing = True

    ' Start drawing.
    m_X1 = e.X
    m_Y1 = e.Y
    SnapToGrid(m_X1, m_Y1)
    m_X2 = m_X1
    m_Y2 = m_Y1

    picCanvas.Invalidate()
    ShowMousePosition(m_X2, m_Y2)
End Sub

Subroutine SnapToGrid snaps a point to the nearest grid location. It
determines how many times the grid's size fits into the available canvas
size, rounding to the nearest integer. It then multiplies that number by
the grid size so get the nearest grid location.

' Snap the point to the nearest grid location.
Private Sub SnapToGrid(ByRef X As Integer, ByRef Y As Integer)
    ' If grid snap is off, do nothing.
    If Not m_SnapToGrid Then Exit Sub

    Dim ix As Integer = CInt(X / m_GridX)
    Dim iy As Integer = CInt(Y / m_GridY)
    X = ix * m_GridX
    Y = iy * m_GridY
End Sub

Subroutine ShowMousePosition saves a point's coordinates and invalidates
the two rulers to make them redraw.

' Show the mouse position on the rulers.
Private Sub ShowMousePosition(ByVal X As Integer, ByVal Y As Integer)
    m_MouseX = X
    m_MouseY = Y
    picTopRuler.Invalidate()
    picLeftRuler.Invalidate()
End Sub

The main canvas's MouseMove evnet handler saves the current mouse
position, snaps it to the grid, shows the mouse position, and redraws
the main canvas to show the new line being drawn.

' Continue drawing.
Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
    m_X2 = e.X
    m_Y2 = e.Y
    SnapToGrid(m_X2, m_Y2)

    ' Show the mouse position on the rulers.
    ShowMousePosition(m_X2, m_Y2)

    ' Redraw.
    If m_Drawing Then picCanvas.Invalidate()
End Sub

The MouseUp event handler shows the mouse position in the non-drawing
color. It then saves the new line segement and redraws to show it in the
non-drawing color.

' Finish drawing.
Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    ' Show the mouse position in the non-drawing color.
    ShowMousePosition(m_X2, m_Y2)

    ' Save the new line.
    Dim new_index As Integer = m_Points1.Length
    ReDim Preserve m_Points1(new_index)
    ReDim Preserve m_Points2(new_index)
    m_Points1(new_index) = New PointF(m_X1, m_Y1)
    m_Points2(new_index) = New PointF(m_X2, m_Y2)

    picCanvas.Invalidate()
End Sub

The main canvas's Paint event handler clears the canvas. Then if
m_ShowGrid is True, it draws the grid. It then draws any existing line
segments and finally, if a new line is being drawn, it draws that line.

' Draw the lines.
Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
    e.Graphics.Clear(picCanvas.BackColor)

    ' Draw the grid.
    If m_ShowGrid Then
        For x As Integer = 0 To picCanvas.ClientSize.Width Step m_GridX
            For y As Integer = 0 To picCanvas.ClientSize.Height Step
m_GridY
                e.Graphics.DrawLine(m_PenGrid, x, y, x + 0.5F, y + 0.5F)
            Next y
        Next x
    End If

    ' Draw existing lines.
    For i As Integer = 0 To m_Points1.Length - 1
        e.Graphics.DrawLine(m_PenOldLine, m_Points1(i), m_Points2(i))
    Next i

    ' Draw the new line.
    If m_Drawing Then
        e.Graphics.DrawLine(m_PenNewLine, m_X1, m_Y1, m_X2, m_Y2)
    End If
End Sub

The top ruler's Paint event handler clears the control. It then draws
the tick marks using the grid's spacing and giving greater length to
every 5 and 10 tick marks. Finally it draws the mouse's position in an
appropriate color. The Left ruler's Paint event handler works similarly.

' Draw the top ruler.
Private Sub picTopRuler_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles picTopRuler.Paint
    e.Graphics.Clear(picTopRuler.BackColor)

    Dim y1 As Integer = picTopRuler.ClientSize.Height
    Dim y2 As Integer = (2 * picTopRuler.ClientSize.Height) \ 3
    Dim y3 As Integer = picTopRuler.ClientSize.Height \ 3
    Dim y4 As Integer = 0
    Dim x As Integer = 0
    For i As Integer = 0 To picTopRuler.ClientSize.Width \ m_GridX
        If i Mod 10 = 0 Then
            e.Graphics.DrawLine(m_PenGrid, x, y1, x, y4)
        ElseIf i Mod 5 = 0 Then
            e.Graphics.DrawLine(m_PenGrid, x, y1, x, y3)
        Else
            e.Graphics.DrawLine(m_PenGrid, x, y1, x, y2)
        End If
        x += m_GridX
    Next i

    ' Show the mouse position.
    If m_Drawing Then
        e.Graphics.DrawLine(m_PenRulerDrawing, m_MouseX, y1, m_MouseX,
0)
    Else
        e.Graphics.DrawLine(m_PenRulerNormal, m_MouseX, y1, m_MouseX, 0)
    End If
End Sub

Download the example to see additional details such as how the menus let
you show or hide the grid and turn grid snapping on and off.

This example is fairly basic and there are lots of other features you
could add such as zooming, panning, drawing other shapes, and so forth.
==========
3. New Tip: Visual Basic Data Type Characters
http://www.vb-helper.com/tip_type_characters.html

I always have trouble finding this information because the search
keywords are so general they return too many hits of the wrong type. So
here's my own version that I can hopefully find more easily.

Data Type Characters

The following table lists Visual Basic's type characters. For example,
suppose you declare a variable using this code.

    Dim x!

Then x is a Single.

Data Type Character
Decimal @
Double #
Integer %
Long &
Single !
String $

I don't recommend using these type characters because I think they make
the code harder to understand but it's nice to have this table so you
can understand other people's code.

Literal Type Characters

You can use literal type characters to give a literal a specific type.
For example, the following sets variable x equal to a Single value
rather than the Integer which is the default for literal values with
this format.

    Dim x As Object = 10S

The following table lists the literal type characters.

Data Type Character
Char C
Decimal D
Double R
Integer I
Long L
Short S
Single F
UInteger UI
ULong UL
UShort US
==========
++++++++++
<Both>
++++++++++
==========
4. New Links

InvestInTech: Tips for Working With Microsoft Software
http://www.investintech.com/content/tipsmicrosoftsoftware
A brief description of Exchange and Sharepoint, plus links to more
informationn about them and .NET programming in general.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.