诸如论坛回贴中的手写功用,3.种类性质设置新匍京视频

 关于Sliverlight导出Excel(浏览器外运行)相比较复杂

新匍京视频 1
这种应用现在早就相比较常见了,比如论坛回贴中的手写作用,IM聊天中的个性化手写文字,个性签名等,在Silverlight中要达成该功用实在分外不难,只要一个InkPresenter控件即可

  上边是法力

动用要点:

新匍京视频 2

1.要创立设置裁剪区,否则手写时或许笔划会写到你不期待出现的地点.
2.拍卖好MouseLeftButtonDown,MouseMove,LostMouseCapture那两个事件.

 

演示代码:

一、导出Excel步骤

前端Xaml部分:

1.新建Silverlight应用程序ExcelDemo

新匍京视频 3新匍京视频 4by 菩提树下的杨过
http://yjmyzz.cnblogs.com/

2.添加引用:添加对Microsoft.CSharp程序集的 引用

<UserControl x:Class=”InkPresenterTest.Page”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:d=”http://schemas.microsoft.com/expression/blend/2008
    xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006
    mc:Ignorable=”d” Background=”Black”
    >

3.项目性质设置

    <Grid Background=”#FF0BB5A3″ Width=”650″ Height=”615″>

新匍京视频 5

        <Grid.RowDefinitions>
            <RowDefinition Height=”25″></RowDefinition>
            <RowDefinition Height=”30″></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height=”30″></RowDefinition>
            <RowDefinition Height=”150″></RowDefinition>
        </Grid.RowDefinitions>

在“属性”面板中,勾选“允许在浏览器外运行应用程序”

        <TextBlock x:Name=”txtTitle” Text=”InkPresenter 演 示” FontWeight=”Bold” Grid.Row=”0″ HorizontalAlignment=”Center” Height=”20″ VerticalAlignment=”Center” Foreground=”White” />

点击“浏览器外安装”

        <StackPanel x:Name=”spToolBar” Grid.Row=”1″ Orientation=”Horizontal” HorizontalAlignment=”Center” Height=”25″ VerticalAlignment=”Center”>
            <TextBlock Text=”笔划颜色:” VerticalAlignment=”Center”></TextBlock>
            <ComboBox x:Name=”cboColor” Width=”65″ VerticalAlignment=”Center”>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation=”Horizontal”>
                            <Rectangle Fill=”{Binding Color}” ToolTipService.ToolTip=”{Binding Name}” Width=”10″ Height=”10″/>
                            <TextBlock Text=”{Binding Name}” Margin=”2,0,0,0″ Foreground=”{Binding Color}”></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBlock Text=”笔划外框颜色:” VerticalAlignment=”Center” Margin=”10,0,0,0″></TextBlock>
            <ComboBox x:Name=”cboOutlineColor” Width=”65″ VerticalAlignment=”Center”>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation=”Horizontal”>
                            <Rectangle Fill=”{Binding Color}” ToolTipService.ToolTip=”{Binding Name}” Width=”10″ Height=”10″/>
                            <TextBlock Text=”{Binding Name}” Margin=”2,0,0,0″ Foreground=”{Binding Color}”></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBlock Text=”笔划宽度:” Margin=”10,0,0,0″ VerticalAlignment=”Center”></TextBlock>
            <ComboBox x:Name=”cboWidth” Width=”60″ VerticalAlignment=”Center” Height=”20″>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Fill=”Black” Width=”40″ Height=”{Binding Size}”/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBlock Text=”笔划高度:” Margin=”10,0,0,0″ VerticalAlignment=”Center”></TextBlock>
            <ComboBox x:Name=”cboHeight” Width=”60″ VerticalAlignment=”Center” Height=”20″>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Fill=”Black” Width=”40″ Height=”{Binding Size}”/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBlock Text=”透明度:” Margin=”10,0,0,0″ VerticalAlignment=”Center”></TextBlock>
            <ComboBox x:Name=”cboOpactiy” Width=”60″ VerticalAlignment=”Center” SelectionChanged=”cboOpactiy_SelectionChanged”>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation=”Horizontal”>
                            <TextBlock Text=”{Binding Value}” Opacity=”{Binding Value}” ></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>

新匍京视频 6

        <Canvas x:Name=”inkC” Grid.Row=”2″ Height=”370″>
            <Rectangle x:Name=”rectBg” Grid.Row=”2″ Width=”650″ Height=”370″ Fill=”#FFECD85C” Margin=”0″ StrokeThickness=”0″></Rectangle>
            <Image Source=”image/background.png” Stretch=”UniformToFill” x:Name=”imgBg”></Image>
            <InkPresenter x:Name=”ink” Height=”370″ Width=”650″ Grid.Row=”2″            
              Background=”Transparent” Opacity=”1″ 
              MouseLeftButtonDown=”OnMouseLeftButtonDown”
              MouseMove=”OnMouseMove”
              LostMouseCapture=”OnLostMouseCapture” Margin=”0″
              >
                <InkPresenter.Clip>
                    <RectangleGeometry Rect=”0,0,650,370″></RectangleGeometry>
                </InkPresenter.Clip>

设置应用程序显示名称,宽度、中度。勾选“展现安装菜单”和“在浏览器外运行时索要提示的深信”.

            </InkPresenter>
            
            <TextBlock Text=”by 菩提树下的杨过” MouseLeftButtonDown=”TextBlock_MouseLeftButtonDown” Cursor=”Hand” Canvas.Left=”5″ Canvas.Top=”5″ Foreground=”LightGray”></TextBlock>
        </Canvas>

这么Silverlight就足以在浏览器外运行了

        <StackPanel x:Name=”spBtn” Grid.Row=”3″ Height=”30″ Margin=”0″ Orientation=”Horizontal” VerticalAlignment=”Center” HorizontalAlignment=”Center”>
            <Button x:Name=”btnToogleBg”  HorizontalAlignment=”Center”  Width=”80″ Content=”隐藏背景图” VerticalAlignment=”Center” Click=”btnToogleBg_Click”/>
            <Button x:Name=”btnToogleBgRect”  HorizontalAlignment=”Center”  Width=”80″ Content=”隐藏背景象” VerticalAlignment=”Center” Click=”btnToogleBgRect_Click” Margin=”10,0,0,0″/>
            <Button x:Name=”btnSave”  HorizontalAlignment=”Center”  Width=”80″ Content=”保存图片” VerticalAlignment=”Center” Margin=”10,0,0,0″ Click=”btnSave_Click”/>
            <Button x:Name=”btnSaveLocal”  HorizontalAlignment=”Center”  Width=”90″ Content=”保存图片到当地” VerticalAlignment=”Center” Margin=”10,0,0,0″ Click=”btnSaveLocal_Click” />
            <Button x:Name=”btnClear”  HorizontalAlignment=”Center”  Width=”80″ Content=”清空画版” VerticalAlignment=”Center” Margin=”10,0,0,0″ Click=”btnClear_Click”/>
            <Button x:Name=”btnClearSave”  HorizontalAlignment=”Center”  Width=”80″ Content=”清空保存区” VerticalAlignment=”Center” Margin=”10,0,0,0″ Click=”btnClearSave_Click”/>
        </StackPanel>

 

        <ScrollViewer x:Name=”ScreenshotViewer” Margin=”0″ Grid.Row=”4″ Background=”#FFFFFF99″ HorizontalScrollBarVisibility=”Auto” VerticalScrollBarVisibility=”Hidden”>
            <StackPanel x:Name=”thumbs”  Orientation=”Horizontal”/>
        </ScrollViewer>

4.实体Customer

    </Grid>

 public class Customer :
INotifyPropertyChanged
    {
        private string m_name;
        public string Name
        {
            get { return m_name;
}
            set
            {
                m_name = value;
               
OnPropertyChanged(“Name”);
            }
        }

</UserControl>

        public int ID { get; set;
}
       
        public short Age { get; set;
}

 

        public event
PropertyChangedEventHandler PropertyChanged;

 

        protected void
OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged !=
null)
            {
                PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
            }
        }
    }

后端代码:

 

新匍京视频 7新匍京视频 8by 菩提树下的杨过
http://yjmyzz.cnblogs.com/

5.数据源DataSource

using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using System.IO;
using FluxJpeg.Core;

public class DataSource
    {
        public static
IList<Customer> LoadData()
        {

namespace InkPresenterTest
{
    public partial class Page : UserControl
    {
        Stroke _drawStroke;

            IList<Customer> list =
new List<Customer>();

        List<FillColor> lstFillColor;
        List<SizeData> lstSizeData;
        List<OpacityData> lstOpacityData;

            for (int i = 0; i < 10000;
i++)
            {
               list.Add(new
Customer(){Age=23,ID=1, Name=”李秀婷”});
                list.Add(new
Customer(){Age=24,ID=2, Name=”吴建强”});
                list.Add(new
Customer(){Age=24,ID=3, Name=”张美静”});
                list.Add(new
Customer(){Age=25,ID=4, Name=”马超”});
                list.Add(new
Customer(){Age=25,ID=5, Name=”吴美音”});
                list.Add(new
Customer(){Age=33,ID=6, Name=”王菲”});
                list.Add(new
Customer(){Age=48,ID=7, Name=”刘德华”});
                list.Add(new
Customer(){Age=48,ID=8, Name=”李连杰”});
                list.Add(new
Customer(){Age=36,ID=9, Name=”权相宇”});
                list.Add(new
Customer(){Age=52,ID=10, Name=”成龙”});
                list.Add(new
Customer(){Age=22,ID=11, Name=”丁魁”});
                list.Add(new
Customer(){Age=35,ID=12, Name=”金喜善”});
                list.Add(new
Customer(){Age=38,ID=13, Name=”宋承宪”});
                list.Add(new
Customer(){Age=24,ID=14, Name=”赵建”});
                list.Add(new
Customer(){Age=33,ID=15, Name=”徐工”});
                list.Add(new
Customer(){Age=21,ID=16, Name=”尹恩惠”});
                list.Add(new
Customer(){Age=23,ID=17, Name=”金泰熙”});
                list.Add(new
Customer(){Age=12,ID=18, Name=”马缙波”});
            }
            return list;

        bool _isLoaded = false;

        }
    }

        public Page()
        {
            InitializeComponent();

 

            //初阶化数据
            lstFillColor = new List<FillColor>() { 
                new FillColor(){ Color = new SolidColorBrush(Colors.Black), Name=”黑色”},
                new FillColor(){ Color = new SolidColorBrush(Colors.Red), Name=”红色”},
                new FillColor(){ Color = new SolidColorBrush(Colors.Blue), Name=”蓝色”},
                new FillColor(){ Color = new SolidColorBrush(Colors.Green),Name=”绿色”},
                new FillColor(){ Color = new SolidColorBrush(Colors.Magenta), Name=”洋红”},               
                new FillColor(){ Color = new SolidColorBrush(Colors.Orange), Name=”橙色”},
            };

6.界面MainPage

            lstSizeData = new List<SizeData>()
            {
                new SizeData(){ Size=1.0},
                new SizeData(){ Size=3.0},
                new SizeData(){ Size=5.0},
                new SizeData(){ Size=7.0},
                new SizeData(){ Size=9.0},
                new SizeData(){ Size=11.0},
                new SizeData(){ Size=13.0},
                new SizeData(){ Size=15.0}
               
            };

<UserControl x:Name=”userControl”
xmlns:dataGrid=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data” 
x:Class=”ExcelDemo.MainPage”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006
xmlns:sdk=”http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk

            lstOpacityData = new List<OpacityData>(){
                new OpacityData(){ Value=0.1},
                new OpacityData(){ Value=0.2},
                new OpacityData(){ Value=0.3},
                new OpacityData(){ Value=0.4},
                new OpacityData(){ Value=0.5},
                new OpacityData(){ Value=0.6},
                new OpacityData(){ Value=0.7},
                new OpacityData(){ Value=0.8},
                new OpacityData(){ Value=0.9},
                new OpacityData(){ Value=1.0}
            };

mc:Ignorable=”d”
d:DesignHeight=”300″
d:DesignWidth=”400″>

            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

<Grid x:Name=”LayoutRoot”
Background=”Transparent”>
<TextBlock  x:Name=”text”
Text=”加载中,请等待…”   TextAlignment=”Center” FontSize=”18.667″
FontWeight=”Bold” Visibility=”Collapsed”/>
<Rectangle Height=”51″
HorizontalAlignment=”Left”   Margin=”1,325,0,0″
x:Name=”rectangle2″
Stroke=”Black”
StrokeThickness=”1″
VerticalAlignment=”Top”
Width=”576″>
<Rectangle.Fill>
<LinearGradientBrush EndPoint=”0.5,1″ 
StartPoint=”0.5,0″>
<GradientStop Color=”Black”
Offset=”1″></GradientStop>
<GradientStop Color=”#6FFFFFFF”
Offset=”0″></GradientStop>
</LinearGradientBrush>

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.cboColor.ItemsSource = lstFillColor;
            this.cboColor.SelectedIndex = 0;

</Rectangle.Fill>
</Rectangle>
<dataGrid:DataGrid
AutoGenerateColumns=”True”
    Height=”295″
    IsReadOnly=”True”
    HorizontalAlignment=”Left”
   
VerticalScrollBarVisibility=”Auto”
    Margin=”0,30,0,0″
x:Name=”customerDataGrid” 
VerticalAlignment=”Top”
Width=”576″ ItemsSource=”{Binding
CustomerCollection, ElementName=userControl, Mode=TwoWay}” />

            this.cboOutlineColor.ItemsSource = lstFillColor;
            this.cboOutlineColor.SelectedIndex = 0;

        <Border
BorderBrush=”Black”
BorderThickness=”1″
Height=”31″
HorizontalAlignment=”Left”
Margin=”0,0,0,0″
x:Name=”border1″
VerticalAlignment=”Top”
Width=”576″
CornerRadius=”5,5,0,0″
MouseLeftButtonDown=”border1_MouseLeftButtonDown”>
           
<Border.Background>

            this.cboWidth.ItemsSource = lstSizeData;
            this.cboWidth.SelectedIndex = 0;

<LinearGradientBrush
EndPoint=”0.5,1″
               
StartPoint=”0.5,0″>

            this.cboHeight.ItemsSource = lstSizeData;
            this.cboHeight.SelectedIndex = 0;

<GradientStop Color=”Black”
            Offset=”0″ />

            this.cboOpactiy.ItemsSource = lstOpacityData;
            this.cboOpactiy.SelectedIndex = 5;

<GradientStop Color=”Black”
            Offset=”0″ />

            _isLoaded = true;
        }

</LinearGradientBrush>

        private void OnMouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            ink.CaptureMouse();
            StylusPointCollection MyStylusPointCollection = new StylusPointCollection();
            MyStylusPointCollection.Add(e.StylusDevice.GetStylusPoints(ink));
            _drawStroke = new Stroke(MyStylusPointCollection);
            _drawStroke.DrawingAttributes.Color = (cboColor.SelectedItem as FillColor).Color.Color;
            _drawStroke.DrawingAttributes.OutlineColor = (this.cboOutlineColor.SelectedItem as FillColor).Color.Color;
            _drawStroke.DrawingAttributes.Width = (this.cboWidth.SelectedItem as SizeData).Size;
            _drawStroke.DrawingAttributes.Height = (this.cboHeight.SelectedItem as SizeData).Size;
            //_drawStroke.SetValue(OpacityProperty, (this.cboOpactiy.SelectedItem as OpacityData).Value);
            ink.Strokes.Add(_drawStroke);
            ink.Opacity = (cboOpactiy.SelectedItem as OpacityData).Value;
        }

</Border.Background>

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (_drawStroke != null)
            {
                _drawStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(ink));
            }
        }

<Border BorderBrush=”{x:Null}”
BorderThickness=”0″
CornerRadius=”5,5,0,0″
Height=”28″
x:Name=”border2″
Width=”570″
Margin=”0,2,0,0″
HorizontalAlignment=”Center”
VerticalAlignment=”Top”>

        private void OnLostMouseCapture(object sender, MouseEventArgs e)
        {
            _drawStroke = null;
        }

<Border.Background>

        private void btnClear_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ink.Strokes.Clear();
        }

<LinearGradientBrush
EndPoint=”0.5,1″
                   
StartPoint=”0.5,0″>

        private void cboOpactiy_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (_isLoaded)
            {
                ink.Opacity = (cboOpactiy.SelectedItem as OpacityData).Value;
            }
        }

<GradientStop Color=”White”
                Offset=”0″ />

        private void btnToogleBg_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (sender as Button);
            if (btn.Content.ToString() == “隐藏背景图”)
            {
                imgBg.Visibility = Visibility.Collapsed;
                btn.Content = “突显背景图”;
            }
            else
            {
                imgBg.Visibility = Visibility.Visible;
                btn.Content = “隐藏背景图”;
            }
        }

<GradientStop
Color=”#00000000″
                Offset=”0.616″
/>

        private void btnToogleBgRect_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (sender as Button);
            if (btn.Content.ToString() == “隐藏背景象”)
            {
                this.rectBg.Visibility = Visibility.Collapsed;
                btn.Content = “展现背景象”;
            }
            else
            {
                rectBg.Visibility = Visibility.Visible;
                btn.Content = “隐藏背景观”;
            }
        }

</LinearGradientBrush>

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HtmlPage.Window.Navigate(new System.Uri(“http://yjmyzz.cnblogs.com/“), “_blank”);
        }

</Border.Background>

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            // 创立一个WriteableBitmap并且把需要表现位图的因素赋值给WriteableBitmap
            WriteableBitmap wb = new WriteableBitmap(inkC, null);

<TextBlock Height=”19″
IsHitTestVisible=”False”
    x:Name=”textBlock1″
    Text=”Silverlight 导出 Excel

Foreground=”White”
    FontSize=”14″
    Width=”558″ />

            // 成立一个Image元平素承载位图
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            image.Height = 120;
            image.Margin = new Thickness(5);
            image.Source = wb;

</Border>

            // 将Image元素放入容器控件中
            thumbs.Children.Add(image);

</Border>

            ScreenshotViewer.ScrollToHorizontalOffset(ScreenshotViewer.ExtentWidth);

<Button Height=”28″
HorizontalAlignment=”Left”
Click=”exportToExcelButton_Click”
Margin=”244,333,0,0″
x:Name=”exportToExcelButton”
Content=”Export To Excel”
Width=”109″ />

        }

<Button Height=”28″
HorizontalAlignment=”Left”
Click=”installButton_Click”
Margin=”244,333,0,0″
x:Name=”installButton”
Content=”Install”
VerticalAlignment=”Top”
Width=”109″ />

        private void btnClearSave_Click(object sender, RoutedEventArgs e)
        {
            thumbs.Children.Clear();
        }

<Rectangle Height=”18″
Cursor=”Hand”
HorizontalAlignment=”Left”
Margin=”549,6,0,0″
x:Name=”rectangle1″
Stroke=”Black”
StrokeThickness=”1″
VerticalAlignment=”Top”
Width=”19″
Fill=”#FF990000″
MouseLeftButtonDown=”rectangle1_MouseLeftButtonDown”/>

        private void btnSaveLocal_Click(object sender, RoutedEventArgs e)
        {
            WriteableBitmap wb = new WriteableBitmap(inkC, null);

<TextBlock Height=”12″
HorizontalAlignment=”Left”                    

            if (wb != null)
            {
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Filter = “JPEG Files (*.jpeg)|*.jpeg”;
                saveDlg.DefaultExt = “.jpeg”;

Margin=”555,7,0,0″
x:Name=”textBlock2″
Text=”X”
Foreground=”White”
VerticalAlignment=”Top” />
</Grid>
</UserControl>

                if (saveDlg.ShowDialog().Value)
                {
                    using (Stream fs = saveDlg.OpenFile())
                    {
                        SaveToFile(wb, fs);
                        MessageBox.Show(string.Format(“文件已经保存至“{0}””,saveDlg.SafeFileName));
                    }
                }
            }

 

        }

7.后台代码

        private void SaveToFile(WriteableBitmap bitmap, Stream fs)
        {
            int width = bitmap.PixelWidth;
            int height = bitmap.PixelHeight;
            int bands = 3;
            byte[][,] raster = new byte[bands][,];

using System.Windows;
using System.Windows.Controls;
using
System.Collections.ObjectModel;
using
System.Runtime.InteropServices.Automation;
using System.Windows.Input;
using System;
using System.Threading;
using System.Collections.Generic;

            for (int i = 0; i < bands; i++)
            {
                raster[i] = new byte[width, height];
            }

namespace ExcelDemo
{
    public partial class MainPage :
UserControl
    {
        //建立dynamic的excel对象
        dynamic excel = null;
        bool newInstance = true;

            for (int row = 0; row < height; row++)
            {
                for (int column = 0; column < width; column++)
                {
                    int pixel = bitmap.Pixels[width * row + column];
                    raster[0][column, row] = (byte)(pixel >> 16);
                    raster[1][column, row] = (byte)(pixel >> 8);
                    raster[2][column, row] = (byte)pixel;
                }

        delegate void
SheetChangedDelegate(dynamic excelSheet, dynamic rangeArgs);

            }

        /// <summary>
        /// 获取或安装集合
        /// </summary>
        public IList<Customer>
CustomerCollection
        {
            get { return
(IList<Customer>)GetValue(CustomerCollectionProperty); }
            set {
SetValue(CustomerCollectionProperty, value); }
        }

            FluxJpeg.Core.ColorModel model = new FluxJpeg.Core.ColorModel { colorspace = FluxJpeg.Core.ColorSpace.RGB };
            FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

        public static readonly
DependencyProperty CustomerCollectionProperty =
   
DependencyProperty.Register(“CustomerCollection”,
typeof(IList<Customer>), typeof(MainPage), new
PropertyMetadata(null));

            //Encode the Image as a JPEG
            MemoryStream stream = new MemoryStream();
            FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
            encoder.Encode();

        public MainPage()
        {
            InitializeComponent();

            //Back to the start
            stream.Seek(0, SeekOrigin.Begin);

           
//判断应用程序是或不是已设置(浏览器外)
            if (App.Current.InstallState
== InstallState.Installed)
            {
                if
(App.Current.IsRunningOutOfBrowser)
                {
                   
installButton.Visibility = Visibility.Collapsed;
                    rectangle1.Visibility
= Visibility.Visible;
                    textBlock2.Visibility
= Visibility.Visible;
                   
App.Current.MainWindow.TopMost = true;
                }

            //Get teh Bytes and write them to the stream
            byte[] binaryData = new byte[stream.Length];
            long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
            fs.Write(binaryData, 0, binaryData.Length);
        }

                else
                {
                   
installButton.IsEnabled = false;
                    rectangle1.Visibility
= Visibility.Collapsed;
                    textBlock2.Visibility
= Visibility.Collapsed;
                }
            }

    }

            else
            {
                installButton.Visibility =
Visibility.Visible;
                rectangle1.Visibility =
Visibility.Collapsed;
                textBlock2.Visibility =
Visibility.Collapsed;
            }

    public class FillColor
    {
        public SolidColorBrush Color { set; get; }
        public string Name { set; get; }
    }

            CustomerCollection =
DataSource.LoadData();

    public class SizeData
    {
        public double Size { set; get; }
    }

            //异步执行
            ThreadPool.QueueUserWorkItem(t
=>
                {
                    UIInvoke(()
=>
                    {
                        text.Visibility =
Visibility.Visible;
                    });

    public class OpacityData
    {
        public double Value { set; get; }
    }
}

                    UIInvoke(()
=>
                    {
                        CustomerCollection
= DataSource.LoadData();
                    });

 

                    UIInvoke(()
=>
                    {
                        text.Visibility =
Visibility.Collapsed;
                    });
                });
        }

 

        /// <summary>
        /// 异步方法
        /// </summary>
        /// <param
name=”action”></param>
        private void UIInvoke(Action
action)
        {
           
System.Windows.Threading.Dispatcher dispatcher =
Deployment.Current.Dispatcher;

源代码下载地址: http://files.cnblogs.com/yjmyzz/InkPresenterTest.rar

            if
(dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                ManualResetEvent
resetEvent = new ManualResetEvent(false);
               
resetEvent.Reset();
                dispatcher.BeginInvoke(()
=>
                {
                    action();
                   
resetEvent.Set();
                });
               
resetEvent.WaitOne();
            }
        }

对Flash感兴趣的意中人,做为相比,也得以看下Flash/Flex学习笔记(14):制作涂鸦板 

        private void
rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs
e)
        {
           
App.Current.MainWindow.Close();
        }

转发请评释来源菩提树下的杨过 http://www.cnblogs.com/yjmyzz/archive/2010/01/14/1647636.html

        private void
installButton_Click(object sender, RoutedEventArgs e)
        {
           
App.Current.InstallStateChanged +=
(Current_InstallStateChanged);

 

            //开头设置
           
App.Current.Install();
        }

注:里面用到了一个开源的零部件FJCore

        void
Current_InstallStateChanged(object sender, EventArgs e)
        {
            if (App.Current.InstallState
== InstallState.Installed)
            {
                installButton.IsEnabled =
false;
            }
        }

        private void
exportToExcelButton_Click(object sender, RoutedEventArgs e)
        {
            //执行导出
            LaunchExcel(sender,
e);
        }

        /// <summary>
        /// 导出多少到Excel
        /// </summary>
        /// <param
name=”sender”></param>
        /// <param
name=”e”></param>
        private void LaunchExcel(object
sender, RoutedEventArgs e)
        {
            excel =
AutomationFactory.CreateObject(“Excel.Application”);
            excel.Visible = true;

            dynamic workbook =
excel.workbooks;
            workbook.Add();
            dynamic sheet =
excel.ActiveSheet;
            dynamic cell = null;
            int i = 1;
            foreach (Customer item in
customerDataGrid.ItemsSource)
            {
                cell = sheet.Cells[i,
1];
                cell.Value =
item.Name;
                cell.ColumnWidth =
50;

                cell = sheet.Cells[i,
2];
                cell.Value =
item.ID;

                cell = sheet.Cells[i,
3];
                cell.Value =
item.Age;

                i++;
            }

            if (newInstance)
            {
               
App.Current.MainWindow.Closing += (MainWindow_Closing);
                excel.SheetChange += new
SheetChangedDelegate(SheetChangedEventHandler);
                newInstance =
false;
            }
        }

        void MainWindow_新匍京视频,Closing(object
sender, System.ComponentModel.ClosingEventArgs e)
        {
            if (excel != null)
            {
                excel.SheetChange -= new
SheetChangedDelegate(SheetChangedEventHandler);
            }
        }

        private void
border1_MouseLeftButtonDown(object sender, MouseButtonEventArgs
e)
        {
           
App.Current.MainWindow.DragMove();
        }

        private void
SheetChangedEventHandler(dynamic excelSheet, dynamic rangeArgs)
        {
            dynamic sheet =
excelSheet;
            dynamic col2range =
sheet.Range(“A1:A” + CustomerCollection.Count);
            for (int i = 0; i <
CustomerCollection.Count; i++)
            {
               
CustomerCollection[i].Name = col2range.Item(i +
1).Value.ToString();
            }
        }
    }
}

注:1.源代码下载地址:http://files.cnblogs.com/salam/ExcelDemo.rar

    2.关于项目dynamic:

  在Visual C#
2010中引入了一种新的dynamic类型,该品种是一个静态的(static)类型,但是一个dynamic类型的对象会绕过静态类型检查。在一大半气象下dynamic和object类型有些相似,不过在编译时,dynamic类型被假定为支持其他操作,也就是说dynamic类型的对象足以是一个Office对象,可以是一个COM对象或者是DOM对象,而只要在运作时意识该目标不是期待的对象则会抛出一个运转时充裕。

由于把一个对象定义为dynamic类型,因而在代码中编辑任意调用在编译时都是法定的。

  使用了经过动态类型参数的方法会在运作时分析而不是在编译时解析,在.NET
Framework 4 Beta1中引入了新的dynamic language
runtime(DLR),它为C#中的dynamic类型提供了支撑,而且提供了对诸如IronPython和IronRuby等动态语言的落成。

  Visual C#
2010采纳dynamic类型和命名和可选参数为与COM
API交互提供了便于。许多COM方法接收各个别型参数并且普通会回到object类型值,开发人员需求开展类型转换以开展越发操作。在.NET
Framework
4中,假若采纳/link开关编译程序,dynamic类型允许在COM调用时将object类型作为dynamic类型对待,那样可以幸免类型转换

相关文章