Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 9 May 2008

Ink Presenter using Microsoft Silverlight

Posted on 10:18 by Unknown
Hello Everyone !!

After a long gap...office work and all..I am back again.

Here is the sample Silverlight demo, which will be like paintbrush kind of thing.

This will surely give vision to all developers who are planning to do some programming on Silverlight.

Demo is created using following Tools.

1. Windows Vista (OS)

2. Visual Studio 2008 (Beta 2)

3. Silverlight SDK 1.1

Initial Setup : First you need to install Visual Studio 2008 (Beta 2) . You can download it from Microsoft Web site. Then you need to install Silverlight SDK 1.1, Once you installed Silverlight SDK . You can see that a Template will get placed in New Project section of Visual Studio as shown below.











Once you create a Silverlight Project with appropriate name and location, You can see the Silverlight Project structure in Solution Explorer of Visual Studio. It contents “Silverlight.js” file which is responsible to Run any silverlight application. First job of Silverlight.js is to check whether the current system is capable of running silverlight application or not. If Silverlight support is not there for current system then appropriate error will be hand

led by this file.

You can see the initialization of Silverlight in javascript function named as “function createSilverlight()” which resides in your Test.html.js file, This function get called from

tag in Test.html Page.

<div id="SilverlightControlHost" class="silverlightHost" >

<script type="text/javascript">

createSilverlight();

script> div>

And reference is get crated in Header tag as

<script type="text/javascript" src="Silverlight.js">script>

<script type="text/javascript" src="TestPage.html.js">script>

This is how the first initialization of Silverlight is done.

screen2.jpg











Ink Presenter :

This demo will show how to implement the simple Ink Presenter feature using Silverlight.For this one need to add Ink Presenter in Page.xaml as follows.

<InkPresenter Height ="600" Width ="600" Background ="Pink"

x:Name="inkP" />

Once you placed Ink Presneter inside xaml file. Then rest of the coding is need to be done from code behind, because one need to handle mouse events which will be better handled from code behind, either C# or VB.NET

For that once should first need to go at Page.xaml.cs file.Then first we need to create object of Stroke class which will help us to draw free hand on web page.Create object “s” for Stroke class as shown below.

namespace MySilverlightInkDemo

{

public partial class Page : Canvas

{

Stroke s;

public void Page_Loaded(object o, EventArgs e)

{

// Required to initialize variables

Point to be noted here that you can see the namespace get consumed here is

System.Windows.Ink. Syntax is

using System.Windows.Ink;

After this,First job is to generate event to draw free han

d lines using Ink.So we need to handle mouse events. For this we will add event handler inside Page_Loaded event as shown below.

public partial class Page : Canvas

{

Stroke s;

public void Page_Loaded(object o, EventArgs e)

{

// Required to initialize variables

InitializeComponent();

inkP.MouseLeftButtonDown += new MouseEventHandler(inkP_MouseLeftButtonDown);

inkP.MouseMove += new MouseEventHandler(inkP_MouseMove);

inkP.MouseLeftButtonUp += new MouseEventHandler(inkP_MouseLeftButtonUp);

}

Note : You just need to type inkP.MouseLeftButtonDown += ..and then Press “Tab” key twice,The event will get generated.

inkP is nothing but name given to InkPresneter in xaml file as shown below.

<InkPresenter Height ="600" Width ="600" Background ="Pink" x:Name="inkP" />

You can see the last attribute x:Name=”inkP”

Here now we will first handle the MouseLeftButtonDown as this is supposed to be get fire first.

void inkP_MouseLeftButtonDown(object sender, MouseEventArgs e)

{

inkP.CaptureMouse();

s = new Stroke();

s.StylusPoints.AddStylusPoints(e.GetStylusPoints(inkP));

s.DrawingAttributes.Color = Colors.Black;

inkP.Strokes.Add(s);

}

You can see the CaptureMouse(); method of inkP.Though is does not take any parameter,Signature of this method is as bool Visual.CaptureMouse();,Object “s” is get instanciated in statment s=new Stroke();

Then we need to add StylusPoints to s, This is nothing but the initialization process of Ink Presenter.Then we need to add this Stroke to inkP by statement inkP.Strokes.Add(s);

Next event to be handle is MouseMove.

void inkP_MouseMove(object sender, MouseEventArgs e)

{

if (s != null)

{

s.StylusPoints.AddStylusPoints(e.GetStylusPoints(inkP));

}

}

This will simply check whether s is null or not,if s is not null then it will keep on adding StylusPoint,Hence we get continious drawing untill we release the mouse button.Finally in MouseLeftButtonUp event, we will make s as null.

void inkP_MouseLeftButtonUp(object sender, MouseEventArgs e)

{

s = null;

}

Hence this is sort of continous process,The same cycle will be again roll on when MouseLeftButtonDown event will get fire.

Build the application and Press “F5”,you should get following output,a sort of board in your browser where you can do free hand drawing.

screen4.jpg
















Above is the default blank canvas ready for drawing, and once you apply strokes using mouse button click like we do in PaintBrush we can gets similar output shown below.

screen3.jpg

screen3.jpg

Thats it !!..done with it..Hope you all will like it..This demo was inspired from one of the silverlight videos which I came across few months back.

Vikram Pendse.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ►  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ▼  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ▼  May (3)
      • Calling WebService Asynchronously using ScriptMana...
      • Test post from Microsoft Windows Live Writer
      • Ink Presenter using Microsoft Silverlight
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile