Windows 8实用窍门系列:14.windows 8中粘贴板(剪切板)的使用
生活随笔
收集整理的這篇文章主要介紹了
Windows 8实用窍门系列:14.windows 8中粘贴板(剪切板)的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Windows 8中我們的粘貼板分別保存4種信息:文本、圖片、網(wǎng)頁、文件。在本文中我們將分別復(fù)制和粘貼這4種元素,當(dāng)然你也可以在外部復(fù)制這4種元素,然后在程序中粘貼出來。
DataPackage:包含用戶希望與另一個(gè)應(yīng)用程序交換的數(shù)據(jù)
//設(shè)置一個(gè)中轉(zhuǎn)變量保存用戶的值
DataPackage dp = new DataPackage();
第一:我們來看看復(fù)制和粘貼文本的后臺(tái)處理代碼。
//<!--復(fù)制文本-->
private void CopyText_Click(object sender, RoutedEventArgs e)
{
dp.SetText(this.SourceText.Text);
Clipboard.SetContent(dp);
}
//<!--粘貼文本-->
private async void PasteText_Click(object sender, RoutedEventArgs e)
{
var ClipBoardData = Clipboard.GetContent();
if (ClipBoardData.Contains(StandardDataFormats.Text))
{
this.TargetText.Text = await ClipBoardData.GetTextAsync();
}
}
第二:復(fù)制和粘貼圖片后臺(tái)處理代碼
//<!--復(fù)制圖片-->
private async void CopyImage_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("ms-appx:///Assets/iphone0426_006.jpg");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
dp.SetBitmap(RandomAccessStreamReference.CreateFromFile(file));
Clipboard.SetContent(dp);
}
//<!--粘貼圖片-->
private async void PasteImage_Click(object sender, RoutedEventArgs e)
{
var ClipBoardData = Clipboard.GetContent();
if (ClipBoardData.Contains(StandardDataFormats.Bitmap))
{
RandomAccessStreamReference img = await ClipBoardData.GetBitmapAsync();
var imgstream = await img.OpenReadAsync();
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imgstream);
this.TargetImage.Source = bitmap;
}
}
第三:復(fù)制和粘貼HTML的后臺(tái)處理代碼
//<!--復(fù)制HTML-->
private void CopyHtml_Click(object sender, RoutedEventArgs e)
{
string html = HtmlFormatHelper.CreateHtmlFormat(this.SourceHtml.InvokeScript("eval",
new string[] { "document.documentElement.outerHTML;" }));
dp.SetHtmlFormat(html);
Clipboard.SetContent(dp);
}
//<!--粘貼HTML-->
private async void PasteHtml_Click(object sender, RoutedEventArgs e)
{
var ClipBoardData = Clipboard.GetContent();
if (ClipBoardData.Contains(StandardDataFormats.Html))
{
string html = await ClipBoardData.GetHtmlFormatAsync();
this.TargetHtml.NavigateToString(html);
}
}
第四:復(fù)制和粘貼文件的后臺(tái)處理代碼
//<!--復(fù)制文件-->
private async void CopyFile_Click(object sender, RoutedEventArgs e)
{
Uri uri1 = new Uri("ms-appx:///Assets/iphone0426_006.jpg");
StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(uri1);
Uri uri2 = new Uri("ms-appx:///Assets/iphone0426_010.jpg");
StorageFile file2 = await StorageFile.GetFileFromApplicationUriAsync(uri2);
List<StorageFile> filelist = new List<StorageFile>();
filelist.Add(file1);
filelist.Add(file2);
dp.SetStorageItems(filelist);
Clipboard.SetContent(dp);
}
//<!--粘貼文件-->
private async void PasteFile_Click(object sender, RoutedEventArgs e)
{
var ClipBoardData = Clipboard.GetContent();
this.TargetFile.Text = "";
if (ClipBoardData.Contains(StandardDataFormats.StorageItems))
{
var filelist = await ClipBoardData.GetStorageItemsAsync();
foreach (StorageFile sfile in filelist)
{
StorageFile storageFileCopy = await sfile.CopyAsync(KnownFolders.DocumentsLibrary,
sfile.Name,NameCollisionOption.ReplaceExisting);
this.TargetFile.Text += sfile.Name + "文件粘貼一份到“庫\\文檔\\" + sfile.Name+"\r";
}
}
}
第五:我們貼出前臺(tái)代碼如下
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<!--復(fù)制文本-->
<TextBox x:Name="SourceText" HorizontalAlignment="Left" Margin="96,50,0,0"
TextWrapping="Wrap" Text="復(fù)制源" VerticalAlignment="Top" Width="178"/>
<Button x:Name="CopyText" Content="復(fù)制文本" HorizontalAlignment="Left"
Margin="367,50,0,0" VerticalAlignment="Top" Click="CopyText_Click"/>
<Button x:Name="PasteText" Content="粘貼文本" HorizontalAlignment="Left"
Margin="545,50,0,0" VerticalAlignment="Top" Click="PasteText_Click"/>
<TextBlock x:Name="TargetText" HorizontalAlignment="Left" Margin="690,50,0,0"
TextWrapping="Wrap" Text="顯示文本" VerticalAlignment="Top" Height="32" Width="170"/>
<!--復(fù)制圖片-->
<Image x:Name="SourceImage" Source="Assets/iphone0426_006.jpg" Margin="96,150,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="178" Height="178"/>
<Button x:Name="CopyImage" Content="復(fù)制圖片" HorizontalAlignment="Left"
Margin="367,150,0,0" VerticalAlignment="Top" Click="CopyImage_Click"/>
<Button x:Name="PasteImage" Content="粘貼圖片" HorizontalAlignment="Left"
Margin="545,150,0,0" VerticalAlignment="Top" Click="PasteImage_Click"/>
<Image x:Name="TargetImage" HorizontalAlignment="Left" Margin="690,150,0,0"
VerticalAlignment="Top" Width="170" Height="170"/>
<!--復(fù)制HTML-->
<WebView x:Name="SourceHtml" HorizontalAlignment="Left" Source="http://wap.easou.com/"
Margin="96,350,0,0" VerticalAlignment="Top" Width="266" Height="278"/>
<Button x:Name="CopyHtml" Content="復(fù)制HTML" HorizontalAlignment="Left"
Margin="367,350,0,0" VerticalAlignment="Top" Click="CopyHtml_Click"/>
<Button x:Name="PasteHtml" Content="粘貼HTML" HorizontalAlignment="Left"
Margin="545,350,0,0" VerticalAlignment="Top" Click="PasteHtml_Click"/>
<WebView x:Name="TargetHtml" HorizontalAlignment="Left" Margin="690,350,0,0"
VerticalAlignment="Top" Width="270" Height="278"/>
<!--復(fù)制文件-->
<Button x:Name="CopyFile" Content="復(fù)制文件" HorizontalAlignment="Left"
Margin="367,650,0,0" VerticalAlignment="Top" Click="CopyFile_Click"/>
<Button x:Name="PasteFile" Content="粘貼文件" HorizontalAlignment="Left"
Margin="545,650,0,0" VerticalAlignment="Top" Click="PasteFile_Click"/>
<TextBlock x:Name="TargetFile" HorizontalAlignment="Left" Margin="690,656,0,0"
TextWrapping="Wrap" Text="顯示文件拷貝信息" VerticalAlignment="Top" Height="55" Width="450"/>
</Grid>
第六:因?yàn)橐獜?fù)制文件到“庫/文檔"文件夾下面,所以需要設(shè)置如下兩處,具體參考http://www.cnblogs.com/chengxingliang/archive/2012/12/17/2819568.html
設(shè)置文件后綴為jpg的可訪問。
最后如需源碼請(qǐng)點(diǎn)擊Win8ClipBoard2.rar ,效果圖如下:
總結(jié)
以上是生活随笔為你收集整理的Windows 8实用窍门系列:14.windows 8中粘贴板(剪切板)的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP Spartacus b2b 页面
- 下一篇: Epic火炬之光2控制台代码有哪些