|
-
Jun 6th, 2005, 08:17 AM
#1
Thread Starter
Hyperactive Member
Letting the user create a line>? {RESOLVED}
Hello everyone!
I was wondering.
Is it possible to make a feature on my form that will allow me to create a line?
So that when the user clicks on a command button, it will allow him to draw a Line - (The same kind of Line as the Line in the component's section)
Does anyone know how I could do this?
Thank you all and have a great day!
Stilekid007
Last edited by stilekid007; Jun 6th, 2005 at 10:18 AM.
-
Jun 6th, 2005, 08:45 AM
#2
Fanatic Member
Re: Letting the user create a line>?
Here's something I just knocked up to give you a starter hopefully:
Start a new project and a command button. paste this code into the form.
VB Code:
Option Explicit
Private m_InDrawMode As Boolean
Private m_ActivateDrawmode As Boolean
Private m_myLine As Line
Private Sub Command1_Click()
m_ActivateDrawmode = True
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static LineNumber As Long
If m_ActivateDrawmode Then
'create a new line and initialise it at the current mouse pointer
LineNumber = LineNumber + 1
Set m_myLine = Me.Controls.Add("VB.Line", "Line" & CStr(LineNumber))
m_myLine.X1 = X
m_myLine.X2 = X
m_myLine.Y1 = Y
m_myLine.Y2 = Y
m_myLine.Visible = True
m_InDrawMode = True
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m_InDrawMode Then
'move the end point of the line
m_myLine.X2 = X
m_myLine.Y2 = Y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
m_ActivateDrawmode = False
End Sub
now run the project and click the command button and then click on the form and hold the mouse down to draw the line.
-
Jun 6th, 2005, 09:03 AM
#3
Re: Letting the user create a line>?
You can also change these two subs of Blade's code to allow for quicker drawing of lines. Just click the command button to start drawing mode and click it again to return to normal.
VB Code:
Private Sub Command1_Click()
If (Not m_InDrawMode) Then _
m_ActivateDrawmode = Not m_ActivateDrawmode
End Sub
'---
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
End Sub
-
Jun 6th, 2005, 09:12 AM
#4
Fanatic Member
Re: Letting the user create a line>?
heh, cool! nice change
makes the button act like a toggle on/off for drawing.
-
Jun 6th, 2005, 09:14 AM
#5
Thread Starter
Hyperactive Member
Re: Letting the user create a line>?
Hello to the both of you.
I appreciate you help!
That works nicely for drawing the Line.
Can I ask one more question?
Is there a way so that after I draw the first line - it will create a second line EXACTLY the same size and angle? (But so that the user can still move the line around once it is drawn?
Or maye even letting the user draw the first line and then he can draw a second line but the angle will be the same angle as the first one?
Does any of this make sense?
Thank you once again!
Stilekid007
-
Jun 6th, 2005, 09:43 AM
#6
Re: Letting the user create a line>?
I almost have it... its just that the slope is changing slightly as the mouse moves .... GRRR...
Last edited by penagate; Jun 6th, 2005 at 09:45 AM.
Reason: Would ya believe, wrong smiley,...
-
Jun 6th, 2005, 09:45 AM
#7
Thread Starter
Hyperactive Member
Re: Letting the user create a line>?
lol thank you and sorry about that sudden change
-
Jun 6th, 2005, 09:58 AM
#8
Re: Letting the user create a line>?
Here we go. I just figured it out, I was using X instead of X - .X1.
Basically it is y2=mx+c where m=(y2-y1)/(x2-x1) and c = y1.
VB Code:
Option Explicit
Private m_InDrawMode As Boolean
Private m_ActivateDrawmode As Boolean
Private mblnFirstLineDone As Boolean
Private m_Lines() As Line
Private Sub Command1_Click()
If (Not m_InDrawMode) Then _
m_ActivateDrawmode = Not m_ActivateDrawmode
End Sub
Private Sub Form_Load()
ReDim m_Lines(0)
Me.ScaleMode = vbPixels
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (m_ActivateDrawmode And Not m_InDrawMode) Then
'create a new line and initialise it at the current mouse pointer
If (mblnFirstLineDone) Then
ReDim Preserve m_Lines(UBound(m_Lines) + 1)
Else
mblnFirstLineDone = True
End If
Set m_Lines(UBound(m_Lines)) = Me.Controls.Add("VB.Line", "Line" & CStr(UBound(m_Lines)))
With m_Lines(UBound(m_Lines))
.X1 = X
.X2 = X
.Y1 = Y
If (UBound(m_Lines)) Then
.Y2 = X + m_Lines(UBound(m_Lines) - 1).Y2 / m_Lines(UBound(m_Lines) - 1).X2
Else
.Y2 = Y
End If
.Visible = True
End With
m_InDrawMode = True
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m_InDrawMode Then 'move the end point of the line
With m_Lines(UBound(m_Lines))
' If it is the first line, allow user to change angle
If (UBound(m_Lines) = 0) Then
.Y2 = Y
Else
' If it is a subsequent line, force the angle
' to be the same
With m_Lines(0) ' Slope is of 1st line
' y = mx + c
' m = (y2-y1)/(x2-x1) + c
' ... y = ((y2-y1)/(x2-x1))x + c
Dim m As Double, c As Double
m = ((.Y2 - .Y1) / (.X2 - .X1))
c = m_Lines(UBound(m_Lines)).Y1
' x1 = (X - x0)
m_Lines(UBound(m_Lines)).Y2 = _
(m * (X - m_Lines(UBound(m_Lines)).X1)) + c
End With
End If
.X2 = X
End With
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
End Sub
Last edited by penagate; Jun 6th, 2005 at 10:01 AM.
Reason: Code came out ugly
-
Jun 6th, 2005, 10:11 AM
#9
Thread Starter
Hyperactive Member
Re: Letting the user create a line>?
Oh wow PERFECT!
That works so awsome!
Thank you so much for that great code guys!
I will rate you! 
Stilekid007
-
Jun 6th, 2005, 10:15 AM
#10
Re: Letting the user create a line>?
 Originally Posted by stilekid007
Oh wow PERFECT!
That works so awsome!
Thank you so much for that great code guys!
I will rate you!
Stilekid007 
No worries
I'm glad high school maths came in useful for something for once
-
Jun 6th, 2005, 10:17 AM
#11
Thread Starter
Hyperactive Member
Re: Letting the user create a line>?
haha! Yea every once in awhile you find a place to use em. not often though LOL
Alright thanks again!
Stilekid007
-
Jun 6th, 2005, 10:22 AM
#12
Thread Starter
Hyperactive Member
Re: Letting the user create a line>? {RESOLVED}
Oh hey,
I forgot... Is there any simple code to remove the lines once they are drawn?
-
Jun 6th, 2005, 10:26 AM
#13
Re: Letting the user create a line>? {RESOLVED}
Yeah, add another Command button (Command2)
VB Code:
Private Sub Command2_Click()
Dim i As Long
For i = 0 To UBound(m_Lines)
Me.Controls.Remove "Line" & CStr(i)
Set m_Lines(i) = Nothing
Next i
ReDim m_Lines(0)
mblnFirstLineDone = False
End Sub
-
Jun 6th, 2005, 10:36 AM
#14
Thread Starter
Hyperactive Member
Re: Letting the user create a line>? {RESOLVED}
THANKS MAN!
-
Jun 17th, 2005, 02:49 PM
#15
Junior Member
Re: Letting the user create a line>? {RESOLVED}
let suppose i want to creat a line by clicking the mouse on the screen,when i click my mouse then the clicked area becomes the first parameter of the line and then the application prompts from the user that how much long do u want to create a line (as a last parameter.), i m asking this becuase i m going to creat a 3D home designing software.and i want to make it as efficient as there are in the market.for example the user clicks the screen area (after selecting the wall object from list) that becomes the first parameter of that wall and program prompts for last parameter.
regards.
-
Jun 17th, 2005, 06:30 PM
#16
Thread Starter
Hyperactive Member
Re: Letting the user create a line>? {RESOLVED}
Hey, I just got back from work - so I will try to write a reply in the morning to ya. I know how to do it ( I think ) 
Stilekid007
-
Jun 20th, 2005, 04:00 PM
#17
Junior Member
Re: Letting the user create a line>? {RESOLVED}
sir plz help me and also tell me that the code that u have mentioned before in thread is drawing line on form but i changed the form and placed there picture box but the line is starting from form not from picture box... regards
-
Jun 20th, 2005, 04:24 PM
#18
Thread Starter
Hyperactive Member
Re: Letting the user create a line>? {RESOLVED}
Ok, so you have a form and the pic box is on it and you want the line to go over the pic box and the form also. The problem your having is the Line is going under the pic box? Am I right in this asumption?
Stilekid007
-
Jun 20th, 2005, 06:04 PM
#19
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|