|
It worked, Thank you for all the help.
As another side note to other readers I had modified the DrawLine(....) properties to Integers to keep from having an unhandled exception. (exception was from having Single, and Integer mixed together)
WavGraph.CreateGraphics.DrawLine(penColor, CInt(xTwo), CInt(yTwo), CInt(xOne), CInt(yOne))
|
|
|
|
|
I'm really sorry, but I need to know how to be able to draw free hand on a picture box at runtime. I've looked around and tried bits of code but I can't get it to work. It only needs to be in one colour and the width of the pen is not really an issue. Please get back to me when you can.
Thanks for your time.
-- modified at 8:37 Wednesday 31st January, 2007
Thanks Chris, but I need example or source code to help as I am fairly new to this. As an exercise I need to allow the user to draw free handedly on to a picture box.
|
|
|
|
|
1- Create a form and Add a picturebox named PictureBox1
2- Add these codes to your form
------------
Private m_MouseIsDown As Boolean
Private m_Points() As Point
Dim m_path As New Drawing2D.GraphicsPath
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
m_Points = New Point() {New Point(e.X, e.Y)}
m_MouseIsDown = True
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If m_MouseIsDown Then
ReDim Preserve m_Points(m_Points.Length)
m_Points(m_Points.Length - 1) = New Point(e.X, e.Y)
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
m_path.StartFigure()
m_path.AddCurve(m_Points)
m_MouseIsDown = False
Me.PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If m_Points IsNot Nothing Then
Using p As New System.Drawing.Pen(Color.Blue, 2)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.DrawPath(p, m_path)
e.Graphics.DrawCurve(p, m_Points)
End Using
End If
End Sub
------------
3- Run your project
now by downing mouse left key and moving mouse, your freehand drawing is painting.
|
|
|
|
|
wrote: As an exercise I need to allow the user to draw free handedly on to a picture box.
If you just want to draw a single line, then the sample you have been given will work. However, drawing on a picturebox is a waste of time, you can take this sample and remove the picture box, it will still work on a form the same way. To draw on a picture box, you need to draw on a bitmap, and show it in the picture box. The picture box is, in every case, totally superfluous and useless. It just adds a needless layer to what you are doing.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Hi,
I need the user to draw line in panel control placed in form. For that I have defined mousedown, mouseup and mousemove events. But it draws the line continously, I need it to be as like drawing line in paint brush.
Dim oldX, oldY As Integer
Dim m_Down As Boolean
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
oldX = e.X
oldY = e.Y
m_Down = True
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If m_Down = True Then
Dim p As Point
Dim g As Graphics
Dim pn As Pen
p = New Point(e.X, e.Y)
g = Graphics.FromHwnd(Panel1.Handle)
pn = New Pen(Color.Red, 1)
g.DrawLine(pn, oldX, oldY, e.X, e.Y)
pn.Dispose()
g.Dispose()
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
m_Down = False
End Sub
Any help?
Be simple and Be sample.
|
|
|
|
|
Do you mean you want a straight line? Not freehand?
|
|
|
|
|
Keep it one thread, instead of starting a new thread for every post you make.
---
Year happy = new Year(2007);
|
|
|
|
|
When you move the mouse, you need to reset oldX and oldY, so your next line starts at the end of your previous one, not where you were when you first clicked the mouse.
What Guffa said, don't flood the forums, stick to a thread.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
I need to draw a straigt line not the freehand drawing.
Rams.
Be simple and Be sample.
|
|
|
|
|
You want the user to click and drag the mouse to create a line right? The problem your having is that you never remove the old line. Basically every time the mouse moves you draw a new line and the old lines remain. Try adding the following change.
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If m_Down = True Then
Dim p As Point
Dim g As Graphics
Dim pn As Pen
p = New Point(e.X, e.Y)
g = Graphics.FromHwnd(Panel1.Handle)
pn = New Pen(Color.Red, 1)
<code>g.Clear(Panel1.BackColor)</code>
g.DrawLine(pn, oldX, oldY, e.X, e.Y)
pn.Dispose()
g.Dispose()
End If
End Sub I should point out that as I've just recently learned your line will only stay until the control is redrawn. Your line is really only temporary. In addition the way it's done right now will only allow a single line. Every time you try to draw a new line the panel will be cleared. If you want a permanent line/lines you'll have to adjust your code. Either create the actual image on an image object and set the panels backgroundimage to that or you'll have to remember each drawn line and redraw them in the paint event.
|
|
|
|
|
I am trying to acuire data from a bluetooth serial port, do a little processing, and plot a continuous graph. The code which I have written works perfectly at low speeds but breaks down when receiving data at around 70 data lines a second faster than this and the "real time" nature is lost taking increasing lengths of time for the data to appear on screen. I am really enquireing whether this is a limitation in VB and/or the .NET framework and if it is not can anyone suggest a stratergy that could help. For information there are approximatly 80 lines of code to be executed for every incoming data line.
Thanks
James
|
|
|
|
|
Hi James,
I doubt that the limitation you are encountering can be found in any programming language. Generally speaking, 80 lines of code is next to nothing, where processing time is concerned. However if you run it on a machine with a Xeon processor, ofcourse it will be executed much faster than on a machine with a 386. What I mean to say, is that it is near impossible to predict whether your app will encounter this problem on every possible hardware configuration, or whether it specifically will not encounter it on a certain configuration.
Otherwise, you could look into buffering the data lines. After all if you are creating a graph, it is obviously meant for a user to look at. If the graph plots at say 140 points (point per data line) per second, its going to be a blur for the user anyway.
You could on the other hand have your graph plot at real time speed, up until that 70 data lines / sec limit, then level off, and have it take its subsequent data from the buffer. If you like, you could just inform the user that the actual speed is in excess of x.
I hope this helps you,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
Hi,
I would like my users to draw line on form. How could I make my users to draw line in the form.
Any luck?
Thanks in advance,
Rams.
Be simple and Be sample.
|
|
|
|
|
See my answer below. To do that, you need to makange mouse events and control the drawing of a bitmap that you will draw the lines to.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
How to include .Net framework while building setup projects in VB.Net?
Manas
|
|
|
|
|
This should help
http://msdn2.microsoft.com/en-us/library/ms994427.aspx
|
|
|
|
|
|
Here is an example that should work. You just need a form with a picturebox. Copy the code into your project and start drawing.
Public Class Form1
Private IsDrawing As Boolean = False
Private StartPoint As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
IsDrawing = True
StartPoint = e.Location
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If IsDrawing Then
PictureBox1.CreateGraphics.DrawLine(Pens.Black, StartPoint, e.Location)
StartPoint = e.Location
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
IsDrawing = False
End Sub
End Class
|
|
|
|
|
The example you've been given won't work properly. Once you have drawn your image, if you move another window over it, it will disappear. This is why you shouldn't use CreateGraphics for anything except very temporary drawing.
A picturebox is a picturebox, a tool for quickly throwing a bitmap onto a form. It's not a paint program, it's not Powerpoint. If you want to draw on an image, you should draw the image yourself, in your paint handler. You should track mouse movement and mouse events, and use the Invalidate() function to force paint events, your paint event will draw your bitmap, which gets drawn to as you move the mouse.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
hi,
Could you give me the code snippet to do thin in paint event.
Be simple and Be sample.
|
|
|
|
|
Well, the paint event will just draw the image. The code you were shown is basically what I'm talking about, except you should handle mouse move for the form, and when the mouse moves, draw on a bitmap and call Invalidate(), your paint method just draws the bitmap.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Iam using VB2005 on Windows XP. In my Application
I have one child form merged with the parent form menu items.
Both the forms have Toolbar.
On load of the parent form along with the child form at runtime select at some
menu and release the menu.
This selected menu image is overlapping on the toolbar of the parent form and hides the toolbar buttons.This continues on click of each and every menu.
Please, tell me the solution for this
Rekha
|
|
|
|
|
How i can export data from grid to excel
|
|
|
|
|
you can use componentOne tools.I think these r the best tools for this.
Lxcite's Planet
... the ultimate solution
|
|
|
|
|
|