<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含应用程序的名称和页标题-->
<Grid>
<InkPresenter x:Name="MyPresenter"
HorizontalAlignment="Left"
VerticalAlignment="Top"
MouseLeftButtonDown="MyPresenter_MouseLeftButtonDown"
LostMouseCapture="MyPresenter_LostMouseCapture"
MouseMove="MyPresenter_MouseMove"
Background="Transparent"
Opacity="1" Width="480" Height="750" />
</Grid>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
</Grid>
<!--演示 ApplicationBar 用法的示例代码-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="appbar.save.rest.png" Text="保存" Click="ApplicationBarIconButton_Click" />
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="PenBlue" Click="ApplicationBarMenuItem_Click_1" />
<shell:ApplicationBarMenuItem Text="PenBrown" Click="ApplicationBarMenuItem_Click_2" />
<shell:ApplicationBarMenuItem Text="PenCyan" Click="ApplicationBarMenuItem_Click_3" />
<shell:ApplicationBarMenuItem Text="PenDarkGray" Click="ApplicationBarMenuItem_Click_4" />
<shell:ApplicationBarMenuItem Text="PenGray" Click="ApplicationBarMenuItem_Click_5" />
<shell:ApplicationBarMenuItem Text="PenGreen" Click="ApplicationBarMenuItem_Click_6" />
<shell:ApplicationBarMenuItem Text="PenLightGray" Click="ApplicationBarMenuItem_Click_7" />
<shell:ApplicationBarMenuItem Text="PenMagenta" Click="ApplicationBarMenuItem_Click_8" />
<shell:ApplicationBarMenuItem Text="PenOrange" Click="ApplicationBarMenuItem_Click_9" />
<shell:ApplicationBarMenuItem Text="PenPurple" Click="ApplicationBarMenuItem_Click_10" />
<shell:ApplicationBarMenuItem Text="PenRed" Click="ApplicationBarMenuItem_Click_11" />
<shell:ApplicationBarMenuItem Text="PenTransparent" Click="ApplicationBarMenuItem_Click_12" />
<shell:ApplicationBarMenuItem Text="PenWhite" Click="ApplicationBarMenuItem_Click_13" />
<shell:ApplicationBarMenuItem Text="PenYellow" Click="ApplicationBarMenuItem_Click_14" />
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
后台代码:
public partial class MainPage : PhoneApplicationPage
{
Stroke CurrentStroke = null;
string penColor = "";
// 构造函数
public MainPage()
{
InitializeComponent();
// 设置剪辑,以便收集墨迹
RectangleGeometry rg = new RectangleGeometry();
// 为了使范围准确,应使用控件的最终呈现高度。
rg.Rect = new Rect(0, 0, MyPresenter.ActualWidth, MyPresenter.ActualHeight);
MyPresenter.Clip = rg;
}
private void MyPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 当我们点击时获捉鼠标光标
MyPresenter.CaptureMouse();
// 收集当前的光标所在的位置的点
StylusPointCollection sc = new StylusPointCollection();
sc.Add(e.StylusDevice.GetStylusPoints(MyPresenter));
CurrentStroke = new Stroke(sc);
// 设置笔触的颜色,大小
if (penColor == "Black") { CurrentStroke.DrawingAttributes.Color = Colors.Black; }
else if (penColor == "Blue") { CurrentStroke.DrawingAttributes.Color = Colors.Blue; }
else if (penColor == "Brown") { CurrentStroke.DrawingAttributes.Color = Colors.Brown; }
else if (penColor == "Cyan") { CurrentStroke.DrawingAttributes.Color = Colors.Cyan; }
else if (penColor == "DarkGray") { CurrentStroke.DrawingAttributes.Color = Colors.DarkGray; }
else if (penColor == "Gray") { CurrentStroke.DrawingAttributes.Color = Colors.Gray; }
else if (penColor == "Green") { CurrentStroke.DrawingAttributes.Color = Colors.Green; }
else if (penColor == "LightGray") { CurrentStroke.DrawingAttributes.Color = Colors.LightGray; }
else if (penColor == "Magenta") { CurrentStroke.DrawingAttributes.Color = Colors.Magenta; }
else if (penColor == "Orange") { CurrentStroke.DrawingAttributes.Color = Colors.Orange; }
else if (penColor == "Purple") { CurrentStroke.DrawingAttributes.Color = Colors.Purple; }
else if (penColor == "Red") { CurrentStroke.DrawingAttributes.Color = Colors.Red; }
else if (penColor == "Transparent") { CurrentStroke.DrawingAttributes.Color = Colors.Transparent; }
else if (penColor == "White") { CurrentStroke.DrawingAttributes.Color = Colors.White; }
else { CurrentStroke.DrawingAttributes.Color = Colors.Yellow; }
CurrentStroke.DrawingAttributes.Width = 8;
CurrentStroke.DrawingAttributes.Height = 8;
// 把新的笔触添加到集合中
MyPresenter.Strokes.Add(CurrentStroke);
}
private void MyPresenter_LostMouseCapture(object sender, MouseEventArgs e)
{
// 当释放鼠标时,也同时释放笔触变量的引用
CurrentStroke = null;
}
private void MyPresenter_MouseMove(object sender, MouseEventArgs e)
{
if (CurrentStroke != null)
{
// 每移动一次鼠标,都收集对应的点。
CurrentStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(MyPresenter));
}
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
WriteableBitmap bmp = new WriteableBitmap(480, 800);
bmp.Render(App.Current.RootVisual, null);
bmp.Invalidate();
MemoryStream stream = new MemoryStream();
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 80);
stream.Seek(0, SeekOrigin.Begin);
MediaLibrary library = new MediaLibrary();
string filename = "Screen" + DateTime.Now.ToString("yyyy-MM-dd_hh:mm:ss") + ".jpg";
library.SavePicture(filename, stream);
stream.Close();
MessageBox.Show("已保存到媒体库!!");
}
private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
{
penColor = "Blue";
}
private void ApplicationBarMenuItem_Click_2(object sender, EventArgs e)
{
penColor = "Brown";
}
private void ApplicationBarMenuItem_Click_3(object sender, EventArgs e)
{
penColor = "Cyan";
}
private void ApplicationBarMenuItem_Click_4(object sender, EventArgs e)
{
penColor = "DarkGray";
}
private void ApplicationBarMenuItem_Click_5(object sender, EventArgs e)
{
penColor = "Gray";
}
private void ApplicationBarMenuItem_Click_6(object sender, EventArgs e)
{
penColor = "Green";
}
private void ApplicationBarMenuItem_Click_7(object sender, EventArgs e)
{
penColor = "LightGray";
}
private void ApplicationBarMenuItem_Click_8(object sender, EventArgs e)
{
penColor = "Magenta";
}
private void ApplicationBarMenuItem_Click_9(object sender, EventArgs e)
{
penColor = "Orange";
}
private void ApplicationBarMenuItem_Click_10(object sender, EventArgs e)
{
penColor = "Purple";
}
private void ApplicationBarMenuItem_Click_11(object sender, EventArgs e)
{
penColor = "Red";
}
private void ApplicationBarMenuItem_Click_12(object sender, EventArgs e)
{
penColor = "Transparent";
}
private void ApplicationBarMenuItem_Click_13(object sender, EventArgs e)
{
penColor = "White";
}
private void ApplicationBarMenuItem_Click_14(object sender, EventArgs e)
{
penColor = "Yellow";
}
}
这里需要一个类
public class CommonHelper
{
public static void MsgBox(string msg)
{
ToastPrompt prompt = new ToastPrompt();
prompt.Message = msg;
prompt.Show();
}
}