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.
- Drawing in WP7: #1 Getting Started and Line Shape
- Drawing in WP7: #2 Drawing shapes with finger
- Drawing in WP7: #3 Understanding Path shapes
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
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
