• 一起谈.NET技术,Silverlight实例教程 Out of Browser与COM互操作实例 狼人:


      Silverlight 实例教程索引

      在前面已经介绍了Silverlight的Out of Browser模式与COM的基本操作以及与Office COM的交互。这篇我们将介绍更多Silverlight Out of Brwoser的COM实例。我们将继续使用过去的SilverlightOOBDemo项目进行简单扩展。

      实例1:演示Silverlight与DOS的交互,对于Windows API熟悉的朋友应该了解,使用WShell可以运行任何Dos命令。

    1 private void dosBtn_Click(object sender, RoutedEventArgs e)
    2 {
    3             using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
    4             {
    5                 //shell.Run(@"cmd /k dir /w /p");
    6                  shell.Run(@"cmd /k ping www.cnblogs.com -t");
    7             }
    8 
    9 }

      实例2:使用WShell API模拟用户输入实例。使用WShell的SendKeys可以模拟用户输入效果到应用程序中,并且可以模拟一些特殊键功能,例如,回车,Tab,Ctrl等按键。

      其中要实现模拟输入代码如下:

    1 private void inputBtn_Click(object sender, RoutedEventArgs e)
    2 {
    3     using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
    4     {
    5         shell.Run(@"c:\windows\notepad.exe");
    6         shell.SendKeys("my blog:{Enter}jv9.cnblogs.com");
    7     }
    8 }

      实例3:Silverlight OOB应用读取注册表信息实例

      使用Shell.Application的RegRead方法可以读取本地注册表键值,例如,读取“HKLM\Software\Microsoft\ASP.NET\RootVer”,.Net Framework的版本。

    1 private void regBtn_Click(object sender, RoutedEventArgs e)
    2 {
    3             using (dynamic WShell = AutomationFactory.CreateObject("WScript.Shell"))
    4             {
    5                 string reg = WShell.RegRead(@"HKLM\Software\Microsoft\ASP.NET\RootVer");
    6                 MessageBox.Show(".Net Framework Root Version: " + reg);
    7             }
    8 }

      读取结果:

      实例4:使用Shell.Application的RegWrite方法可以对注册表进行写操作。这个实例将实现添加Silverlight Out of Browser应用到Windows启动项。

     1 private void regWriteBtn_Click(object sender, RoutedEventArgs e)
     2 {
     3             using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))
     4             {
     5                 dynamic commonPrograms = ShellApplication.NameSpace(11);
     6                 string allUsersPath = commonPrograms.Self.Path;
     7 
     8                 dynamic directory = ShellApplication.NameSpace(allUsersPath + @"\Programs");
     9                 dynamic link = directory.ParseName(Deployment.Current.OutOfBrowserSettings.ShortName + ".lnk");
    10                 string OOBLink = link.Path;
    11 
    12                 using (dynamic WShell = AutomationFactory.CreateObject("WScript.Shell"))
    13                 {
    14                     WShell.RegWrite(@"HKLM\Software\Microsoft\Windows\CurrentVersion\Run\"
    15                                                  + Deployment.Current.OutOfBrowserSettings.ShortName,
    16                                                  OOBLink);
    17                     MessageBox.Show("请重启你的机器,你的应用将被自动载入启动列表.");
    18                 }
    19             }
    20 }

      当运行以上代码后,应用会将OOB应用快捷方式写入注册表HKLM\Software\Microsoft\Windows\CurrentVersion\Run\ ,应用程序将在下次重启后,自动启动。

      实例5:使用Windows 7 API实现锁定应用到Windows 7任务栏

      在Windows 7中使用Shell.Application类库允许遍历应用,检查Verbs进行应用锁定。

     1 private void pinBtn_Click(object sender, RoutedEventArgs e)
     2 {
     3             using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))
     4             {
     5                 dynamic commonPrograms = ShellApplication.NameSpace(23);
     6                 string allUsersPath = commonPrograms.Self.Path;
     7 
     8                 dynamic directory = ShellApplication.NameSpace(allUsersPath + @"\Accessories");
     9                 dynamic link = directory.ParseName("Calculator.lnk");
    10 
    11                 dynamic verbs = link.Verbs();
    12                 for (int i = 0; i < verbs.Count(); i++)
    13                 {
    14                     dynamic verb = verbs.Item(i);
    15                     if (verb.Name.Replace(@"&"string.Empty).ToLower() == "pin to taskbar")
    16                     {
    17                         verb.DoIt();
    18                     }
    19                 }
    20             }
    21 }

      当执行以上代码后,获取计算器应用快捷方式,然后执行“Pin to Taskbar”后,将应用锁定在Windows 7任务栏。

      实例6:Silverlight Out of Browser语音阅读实例,使用Windows自带的Speech API中的SAPI引擎SpVoice类可以实现语音阅读功能。

    1 private void speechBtn_Click(object sender, RoutedEventArgs e)
    2 {
    3             using (dynamic ISpeechVoice = AutomationFactory.CreateObject("SAPI.SpVoice"))
    4             {
    5                 ISpeechVoice.Volume = 100;
    6                 ISpeechVoice.Speak("<rate speed=\"0\"><pitch middle=\"0\">Hello everyone! Welcome to my blog,http://jv9.cnblogs.com");
    7             }
    8 }

      当运行以上代码后,会听到以上阅读内容。对于Silverlight Out of Browser的COM应用有一款开源项目COM Toolkit,该控件在OOB模式下可以对本地数据库进行操作,推荐大家参考学习

    今天就写到这里了,希望能对大家有所帮助。

      源代码下载

  • 相关阅读:
    Partial Class —— 转发
    HTTP Message Handlers in ASP.NET Web API & HttpClient Message Handlers in ASP.NET Web API (转发)
    egg.js sequelize 事务处理
    (sequelize)bulkCreate函数中的updateOnDuplicate参数使用
    Sequelize 关联查询数据合并字段
    Vue获取钉钉免登陆授权码
    Microsoft太小气了,Wallop的帐号竟然被取消了。
    plot_importance多分类、排序不匹配、图片数值不显示
    Markdown常用语法说明
    UIS8910的引脚复用
  • 原文地址:https://www.cnblogs.com/waw/p/2158616.html
Copyright © 2020-2023  润新知