Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    191

    [VB.NET]Linee dinamiche...

    Ciao a tutti ho un piccolo problema...Devo disegnare delle linee dinamicamente, cioè clicco una prima volta e fisso il primo punto, poi muovendomi devo vedere la linea muoversi e poi con un secodno click fisso il secondo punto della linea...
    Sono da poco passato a vb.net...In vb6 tutto questo lo facevo con il seguente codice:
    codice:
    Dim oldX As Single, oldY As Single
    'Array Linee
    Dim arrX1(1000) As Single
    Dim arrX2(1000) As Single
    Dim arrY1(1000) As Single
    Dim arrY2(1000) As Single
    Dim iNumLinee As Integer
    
    Private Sub Form_Load()
    oldX = -1: oldY = -1
    End Sub
    
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If oldX <> -1 Then
    Picture2.PaintPicture Picture1.Image, 0, 0
    Else
    'Aggiunge le coordinate della linea negli array delle linee
    arrX1(iNumLinee) = oldX
    arrX2(iNumLinee) = X
    arrY1(iNumLinee) = oldY
    arrY2(iNumLinee) = Y
    iNumLinee = iNumLinee + 1
    End If
    oldX = X
    oldY = Y
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If oldX = -1 Then Exit Sub
    Picture1.PaintPicture Picture2.Image, 0, 0
    Picture1.Line (oldX, oldY)-(X, Y), RGB(0, 0, 0)
    End Sub
    Ho provato a riadattarlo in vb.net in questo modo ma il risultato è pessimo...
    codice:
    Public Class Form1
        Dim oldX As Single, oldY As Single
        'Array Linee
        Dim arrX1(1000) As Single
        Dim arrX2(1000) As Single
        Dim arrY1(1000) As Single
        Dim arrY2(1000) As Single
        Dim iNumLinee As Integer
        Dim bm As Bitmap
    
        Private Sub Picture1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture1.MouseDown
            If oldX <> -1 Then
                bm = New Bitmap(Picture1.Size.Width, Picture1.Size.Height, Picture1.CreateGraphics())
            Else
                'Aggiunge le coordinate della linea negli array delle linee
                arrX1(iNumLinee) = oldX
                arrX2(iNumLinee) = e.X
                arrY1(iNumLinee) = oldY
                arrY2(iNumLinee) = e.Y
                iNumLinee = iNumLinee + 1
            End If
            oldX = e.X
            oldY = e.Y
        End Sub
    
        Private Sub Picture1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture1.MouseMove
            If oldX = -1 Then Exit Sub
            Picture1.Image = (bm)
            Picture1.CreateGraphics.DrawLine(Pens.Black, oldX, oldY, e.X, e.Y)
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            oldX = -1 : oldY = -1
        End Sub
    End Class

    Qualcuno saprebbe aiutarmi?....Grazie mille a tutti...

  2. #2
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    53
    Cosa significa che il risultato è pessimo?

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    191
    Significa intanto che muovendosi la linea si vede molto male, se ci si ferma non si vede e se si clica per fissarla non si vede...

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Come al solito si capisce ben poco da quello che scrivi. Comunque sia per disegnare una linea singola:

    codice:
       Dim pushed As Boolean
        Dim myPoint As PointF
        Dim endPoint As PointF
        Dim bmp As Bitmap
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            bmp = New Bitmap(Me.Width, Me.Height)
        End Sub
    
        Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            pushed = True
            myPoint = New PointF(e.X, e.Y)
        End Sub
    
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            If pushed Then
                endPoint = New PointF(e.X, e.Y)
                Me.Invalidate()
            End If
        End Sub
    
        Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
            pushed = False
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Me.BackColor)
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            g.DrawLine(Pens.Black, myPoint, endPoint)
            e.Graphics.DrawImage(bmp, 0, 0)
        End Sub
    Sbagliare è umano, perseverare è diabolico.

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Una soluzione che ho complicato al massimo per disegnare più linee insieme..


    codice:
        Dim pushed As Boolean
        Dim myPoint As PointF
        Dim endPoint As PointF
        Dim count As Integer
        Dim bmp As Bitmap
        Dim myList As New ArrayList
        Dim myLine As Line
    
        Public Structure Line
            Public startPoint As PointF
            Public endPoint As PointF
    
            Sub New(ByValstartPoint As PointF, ByVal endPoint As PointF)
                Me.startPoint = startPoint
                Me.endPoint = endPoint
            End Sub
        End Structure
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            bmp = New Bitmap(Me.Width, Me.Height)
        End Sub
    
        Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            pushed = True
            myLine = New Line(New PointF(e.X, e.Y), New PointF(e.X, e.Y))
            myList.Add(myLine)
        End Sub
    
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            If pushed Then
                myLine = CType(myList(count), Line)
                myLine.endPoint = New Point(e.X, e.Y)
                myList.RemoveAt(count)
                myList.Insert(count, myLine)
                Me.Invalidate()
            End If
        End Sub
    
        Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
            myLine = New Line(myLine.startPoint, New PointF(e.X, e.Y))
            ' myList.Add(myLine)
            pushed = False
            count += 1
            Me.Invalidate()
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Me.BackColor)
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            If myList.Count > 0 Then
                For Each linea As Line In myList
                    g.DrawLine(Pens.Black, linea.startPoint, linea.endPoint)
                Next
            Else
                g.DrawLine(Pens.Black, myLine.startPoint, myLine.endPoint)
            End If
            e.Graphics.DrawImage(bmp, 0, 0)
        End Sub
    Sbagliare è umano, perseverare è diabolico.

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Dimenticavo:

    codice:
     Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
            ' MyBase.OnPaintBackground(e)
        End Sub
    Sbagliare è umano, perseverare è diabolico.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.