Click here to Skip to main content
15,891,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
I am working on an application which draws a simple dot grid. I would like the mouse to snap between the points on the grid, eventually to draw lines on the grid.

I have a method which takes in the current mouse location (X,Y) and calculates the nearest grid coordinate.

When I create an event and attempt to move the mouse to the new coordinate the whole system becomes jerky. The mouse doesn't snap smoothly between grid points.


i have done snaping one point to other point but i want to snap 1st point to second point same like second point to third point also but if i done second point to third point at that time forst point to second point is removing.


Code:

VB
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
    Public Const strApplicationTitle As String = "Snap Demo"
    Public frmSnap As Form1
    Public ptSnap, ptStart, ptEnd As Point
    Public Sub New()
        Me.Text = "Snap points"
        Me.ClientSize = New Size(800, 600)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.DoubleBuffered = True
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        e.Graphics.Clear(Color.Black)
        For row As Integer = 20 To 780 Step 20
            For col As Integer = 20 To 580 Step 20
                e.Graphics.DrawEllipse(Pens.Blue, New Rectangle(row - 2, col - 2, 4, 4))
            Next
        Next
        e.Graphics.DrawLine(Pens.Red, ptStart, ptEnd)
    End Sub
    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        Dim x As Integer = CInt(e.X / 20) * 20
        Dim y As Integer = CInt(e.Y / 20) * 20
        ptStart = New Point(x, y)
        ptSnap = New Point(x, y)
        Windows.Forms.Cursor.Position = Me.PointToScreen(ptSnap)
    End Sub
    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim x As Integer = CInt(e.X / 20) * 20
            Dim y As Integer = CInt(e.Y / 20) * 20
            ' must be some delta away from original snap point
            If (x < ptSnap.X - 15 Or x > ptSnap.X + 15) Or (y < ptSnap.Y - 15 Or y > ptSnap.Y + 15) Then
                ptSnap = New Point(x, y)
                ptEnd = New Point(x, y)
                Me.Invalidate(False)
                Windows.Forms.Cursor.Position = Me.PointToScreen(ptSnap)
            End If
        End If
    End Sub


    Public Sub main()
        Try
            frmSnap = New Form1
            Application.Run(frmSnap)
        Catch ex As Exception
            MessageBox.Show(ex.Message, strApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            frmSnap.Dispose()
        End Try
    End Sub
End Class
Posted
Updated 28-Apr-14 21:20pm
v3

1 solution

It doesn't seem as though you're saving the line that has been drawn.
When the mouse moves you're updating the global vars "ptStart" and "ptEnd" which you use to draw the line on the screen. When you draw the next line you reset those to vars. What I sagest is that you add two point arrays (or a 2 dimensional point array) and on the "MouseUp" event you add a new point(ptStart, ptEnd) to that array. Then in your "OnPaint" method you do a loop through that array and draw a line for point. I hope this helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900