"ComputerSmith" <EMAIL REMOVED> wrote
> I was asked by a friend to write a "simple" app that I am unsure how to
> proceed with. Any and all help is appreciated.
This doesn't cover all the details, but it may get you started down the
path. It creates the nodes with numbered text (you'll have to get the
text from the user). I took a different tact with the mouse buttons,
use one to create a new item, and the other to move it.
To try it out, add an Image control to a new form and set its Index
property to 0.
Then add a cl*** to the project and paste in the code below in their
respective modules...
Have fun!
LFS
' [ Cl***1 ] ------------------------------------------------
Option Explicit
Public X As Long
Public Y As Long
Public Text As String
Public Handle As Image
Public Kids As New Collection
Friend Sub RenderLines(Surface As Variant)
Dim Item As Cl***1
'Connect lines first, then do circles/text
For Each Item In Kids
Surface.Line (X, Y)-(Item.X, Item.Y), vbBlack
Item.RenderLines Surface
Next
End Sub
Friend Sub RenderText(Surface As Variant)
Dim Item As Cl***1
' draw the circles and text
Surface.Circle (X, Y), 500, vbBlack
Surface.CurrentX = X - Surface.TextWidth(Text) \ 2
Surface.CurrentY = Y - Surface.TextHeight(Text) \ 2
Surface.Print Text;
For Each Item In Kids
Item.RenderText Surface
Next
Handle.Move X - 500, Y - 500, 1000, 1000
End Sub
Public Function Locate(ByVal Index As Long) As Cl***1
Dim kid As Cl***1
Dim test As Cl***1
' reutrns the cl*** whose handle has a certain Index
If Handle.Index = Index Then
Set Locate = Me
Else
For Each kid In Kids
Set test = kid.Locate(Index)
If Not test Is Nothing Then
Set Locate = test
Exit For
End If
Next
End If
End Function
' [ Form1 ] -------------------------------------------------------
Option Explicit
Private Node As Cl***1
Private Drawing As Boolean
Private StartX As Long, StartY As Long
Private OldX As Long, OldY As Long
Private StartNode As Cl***1
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
DropIt Source, X, Y
End Sub
Private Sub Form_Load()
Me.FillStyle = vbFSSolid
Me.FillColor = vbWhite
Me.DrawWidth = 2
Set Node = NewItem(ScaleWidth \ 2, ScaleHeight \ 2, "Root", Image1(0))
End Sub
Private Sub Form_Paint()
Node.RenderLines Me
Node.RenderText Me
End Sub
Private Sub Image1_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
Dim img As Image
Set img = Node.Locate(Index).Handle
DropIt Source, img.Left + X, img.Top + Y
End Sub
Private Sub Image1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'Select Draw Mode
Drawing = True
DrawMode = vbInvert
DrawStyle = vbDot
'Store start values
Set StartNode = Node.Locate(Index)
StartX = StartNode.X
StartY = StartNode.Y
OldX = X + StartNode.Handle.Left
OldY = Y + StartNode.Handle.Top
Else
OldX = X
OldY = Y
Node.Locate(Index).Handle.Drag vbBeginDrag
End If
End Sub
Private Sub Image1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim XX As Long, YY As Long
If Drawing Then
' Convert Image click to Form coordinates
XX = X + StartNode.Handle.Left
YY = Y + StartNode.Handle.Top
'Erase old line
Line (StartX, StartY)-(OldX, OldY), vbBlack
'Draw new line
Line (StartX, StartY)-(XX, YY), vbBlack
OldX = XX
OldY = YY
End If
End Sub
Private Sub Image1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Drawing Then
Drawing = False
DrawMode = vbCopyPen
DrawStyle = vbSolid
' Create a new Image control, make it visible
Load Image1(Image1.Count)
Image1(Image1.Count - 1).Visible = True
' Add a new item to the node
Node.Locate(Index).Kids.Add NewItem(X + StartNode.Handle.Left, _
Y + StartNode.Handle.Top, CStr(Image1.Count - 1), _
Image1(Image1.Count - 1))
Refresh
End If
End Sub
Private Function NewItem(ByVal X As Long, ByVal Y As Long, Text As String, Handle As Image) As Cl***1
Set NewItem = New Cl***1
With NewItem
.X = X
.Y = Y
.Text = Text
Set .Handle = Handle
End With
End Function
Private Sub DropIt(Source As Image, ByVal X As Long, ByVal Y As Long)
Dim This As Cl***1
' Drops item
Set This = Node.Locate(Source.Index)
This.X = X - (OldX - 500)
This.Y = Y - (OldY - 500)
Source.Drag vbEndDrag
Refresh
End Sub
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----