Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Really Stuck- How do you do freehand line drawing to the screen?

36 views
Skip to first unread message

OldFriend99

unread,
Jun 6, 2003, 12:03:43 PM6/6/03
to
I know how to do shapes and straight lines, ect but how do you allow the
user to click on the screen and draw free handed lines that twist and curve
or go strainght ect?

any samples would be appreciated

Ryan Trudelle-Schwarz

unread,
Jun 6, 2003, 2:33:02 PM6/6/03
to
"OldFriend99" <oldfr...@hotmail.com> wrote

> I know how to do shapes and straight lines, ect but how do you allow the
> user to click on the screen and draw free handed lines that twist and
curve
> or go strainght ect?

Could you store the points the mouse traverses in a GraphicsPath?


james

unread,
Jun 6, 2003, 4:40:29 PM6/6/03
to
Here's a VB.NET sample for drawing on a Picturebox. Just add a Picturebox
Control to a form and just below the " Inherits System.Windows.Forms.Form
Put this line:
Dim bTracking As Boolean

'Then Create the following Routine: AddPoint:

Private Sub AddPoint(ByVal e As System.Windows.Forms.MouseEventArgs)

Dim g As Graphics = PictureBox1.CreateGraphics()

p = System.Drawing.Pens.Black

g.DrawLine(p, e.X, e.Y, e.X + 2, e.Y + 2)

g.Dispose()

End Sub

'Then the following in the Picturebox MouseDown event:

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

bTracking = True

AddPoint(e)

End Sub

' this in the Picturebox's MouseMove event:

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

If bTracking Then

AddPoint(e)

End If

End Sub

And finally this in the Picturebox's MouseUp event:

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

If bTracking Then

AddPoint(e)

bTracking = False

End If

End Sub

In the Picturebox's Properties window set the Height and Width to whatever
size you want

(at least large enough to have a drawing area ) and your all set to go.

You can change the Pen's drawing colors , width etc. using several different
things. But, this should give you some ideas.

james


"Ryan Trudelle-Schwarz" <ryan.pu...@mamanze.com> wrote in message
news:O4IhQoFL...@TK2MSFTNGP10.phx.gbl...

Bob Powell [MVP]

unread,
Jun 8, 2003, 6:21:34 AM6/8/03
to
You need to store the mouse points so that they can be redrawn if the window
is invalidated.

Trapping them in the mousemove is fairly simple.

The following example, after my signature, shows a listing that captures and
draws an array of lines which are in turn built from an array of captured
points.

--
Bob Powell [MVP]
C#, System.Drawing

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm

Get the NEW must-have UI component. The .NET RectTracker
Enables CRectTracker like functionality for WindowsForms.
User defined layouts are a breeze.
http://www.bobpowell.net/recttracker.htm

----------------------------------------------------------------------------
------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FreehandDraw
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

ArrayList Lines = new ArrayList();
ArrayList CurrentArray;

bool Drawing=false;

Point CurrentPos;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(368, 341);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
foreach(ArrayList al in Lines)
{
Point[] linelist = new Point[al.Count];
al.CopyTo(0,linelist,0,al.Count);
e.Graphics.DrawLines(Pens.Black,linelist);
}
}

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
CurrentArray = new ArrayList();
Drawing=true;
CurrentPos = new Point(e.X,e.Y);
CurrentArray.Add(CurrentPos);
}

private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(Drawing)
{
Graphics g = CreateGraphics();
Point p = new Point(e.X,e.Y);
g.DrawLine(Pens.Black,CurrentPos,p);
CurrentArray.Add(p);
CurrentPos = p;
}
}

private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Drawing = false;
Lines.Add(CurrentArray);
}
}
}

"OldFriend99" <oldfr...@hotmail.com> wrote in message
news:#7YhtTEL...@TK2MSFTNGP09.phx.gbl...

John

unread,
Jul 6, 2003, 8:34:47 PM7/6/03
to
Here is a MicroSoft sample code:

"The Scribble sample shows how to develop a Windows Forms MDI application
using Visual Basic and the .NET Framework classes. Scribble is a small
drawing application that lets you draw free-hand drawings using the mouse
and save the images in a file."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbref/html/vbsamwindowsformsscribblesample.asp

This is the Visual Basic.NET sample, they have C and Java code samples also.

Hope this helps :)

T


"OldFriend99" <oldfr...@hotmail.com> wrote in message

news:%237YhtTE...@TK2MSFTNGP09.phx.gbl...

0 new messages