WindowsPhoneGeek

WPAppInfo

Login | Join (Why?)

rss rss rss
logo

Drawing in WP7: #2 Drawing shapes with finger

published on: 3/8/2011 | Views: N/A | Tags: Drawing UI

by WindowsPhoneGeek

This is the second tutorial from the "Drawing in WP7" series of posts. I am going to demonstrate how to draw shapes with finger in a Windows Phone 7 application.

For reference take a look at the previois post here: Drawing in WP7: #1 Getting Started and Line Shape

There are lots of different approaches to dynamic drawing with the finger. The simplest one and may be the most commonly used among Silverlight developers is just to use a Canvas  and its MouseMove and MouseLeftButtonDown handlers. The steps are as follows:

1.) Create a Windows Phone 7  application project and add a Cancas into the MainPage.xaml:

<Canvas x:Name="ContentPanelCanvas" />

2).Subscribe to the Canvas MouseMove and MouseLeftButtonDown events:

public MainPage()
{
    InitializeComponent();
    this.ContentPanelCanvas.MouseMove += new MouseEventHandler(ContentPanelCanvas_MouseMove);
    this.ContentPanelCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(ContentPanelCanvas_MouseLeftButtonDown);
}

3.) We will need two Point variables that we represent  the start and current touch points: currentPoint and oldPoint

private Point currentPoint;
private Point oldPoint;

4.)Add the following code into MouseLeftButtonDown :

void ContentPanelCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    currentPoint = e.GetPosition(ContentPanelCanvas);
    oldPoint = currentPoint;
}

5). Add the following code into MouseMove handler:

void ContentPanelCanvas_MouseMove(object sender, MouseEventArgs e)
{     
    currentPoint = e.GetPosition(this.ContentPanelCanvas);
    
    Line line = new Line() { X1 = currentPoint.X, Y1 = currentPoint.Y, X2 = oldPoint.X, Y2 = oldPoint.Y };
    line.Stroke = new SolidColorBrush(Colors.Purple);
    line.StrokeThickness = 15;
        
    this.ContentPanelCanvas.Children.Add(line);
    oldPoint = currentPoint;
}

6). That’s it just build and run the sample. Here is the result:

   

 I hope that this post was helpful.

You can download the full  source code here.

You can also follow us on Twitter @winphonegeek

Comments

posted by: Enzo Contini on 8/18/2011 11:45:54 AM

Nice ... but I found that you must move the finger slowly and for capture a signature can be not adeguate! You say that different approaches to dynamic drawing with the finger: can you give some more example or links that allows to have better performance?

Thanks Enzo

Problems trying clean the screen

posted by: Benito Mendez on 12/20/2011 2:36:07 PM

Hi!

I'm developing an app for mobile payments. I'm using your code for obtain the costumers signatures. But I'm having a lot of problems trying to create a button for clean the screen.

Please could you help me?

Kind regards, Benito Mendez.

Mouse event

posted by: Leo on 3/30/2012 6:43:30 AM

Hi man,

I just want to know whether we can active mouse event with finger in real phone machine. I mean not in emulator. I don't have a window phone. So I worry about if I use mouse event instead of tap and ManipulationDelta event. I just can run app in my emulator.

Thank you.

WP7 App Developer

posted by: Parasmani on 4/20/2012 3:48:43 PM

Hi Benito Mendez,

You can use the following code to get you canvas cleared:

private void btnClear_Click(object sender, RoutedEventArgs e) { this.ContentPanelCanvas.Children.Clear(); this.ContentPanelCanvas.Children.Add(btnClear); this.ContentPanelCanvas.Children.Add(btnSave); }

Regards Parasmani

Eraser

posted by: Vivek Bhusal on 5/4/2012 4:54:38 AM

Thanks for such a helpful post. But m in problem i can successful draw but i need to erase. i dont want o clear all but use as an eraser...

Regards, Vivek

Add comment to 'Drawing in WP7: #2 Drawing shapes with finger'

Comment

Our Top Articles & Free books

Our Top Tips & Samples