Out of Browser与COM互操作实例

最后更新于:2022-04-01 14:20:47

![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c868925.jpg) 在前面已经介绍了Silverlight的[Out of Browser模式与COM的基本操作](http://www.cnblogs.com/jv9/archive/2010/07/23/1783379.html)以及[与Office COM的交互](http://www.cnblogs.com/jv9/archive/2010/07/24/1784102.html)。这篇我们将介绍更多Silverlight Out of Brwoser的COM实例。 我们将继续使用过去的SilverlightOOBDemo项目进行简单扩展。 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c95a9ed.jpg) 实例1:演示Silverlight与DOS的交互,对于Windows API熟悉的朋友应该了解,使用[WShell](http://msdn.microsoft.com/en-us/library/ahcz2kh6(VS.85).aspx)可以运行任何Dos命令。 ~~~  private void dosBtn_Click(object sender, RoutedEventArgs e)  {              using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))              {                  //shell.Run(@"cmd /k dir /w /p");                  shell.Run(@"cmd /k ping www.cnblogs.com -t");              } } ~~~ ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c96b23d.jpg) 实例2:使用[WShell API](http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx)模拟用户输入实例。使用WShell的SendKeys可以模拟用户输入效果到应用程序中,并且可以模拟一些特殊键功能,例如,回车,Tab,Ctrl等按键。 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c985548.jpg) 其中要实现模拟输入代码如下: ~~~  private void inputBtn_Click(object sender, RoutedEventArgs e)  {      using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))      {          shell.Run(@"c:/windows/notepad.exe");          shell.SendKeys("my blog:{Enter}jv9.cnblogs.com");      }  } ~~~ ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c992e37.jpg) 实例3:Silverlight OOB应用读取注册表信息实例 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c9a50cd.jpg) 使用Shell.Application的[RegRead方法](http://msdn.microsoft.com/en-us/library/x05fawxd(VS.85).aspx)可以读取本地注册表键值,例如,读取“HKLM/Software/Microsoft/ASP.NET/RootVer”,.Net Framework的版本。 ~~~  private void regBtn_Click(object sender, RoutedEventArgs e)  {              using (dynamic WShell = AutomationFactory.CreateObject("WScript.Shell"))              {                  string reg = WShell.RegRead(@"HKLM/Software/Microsoft/ASP.NET/RootVer");                  MessageBox.Show(".Net Framework Root Version: " + reg);              } } ~~~ 读取结果: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67c9b5a6b.jpg) 实例4:使用Shell.Application的[RegWrite方法](http://msdn.microsoft.com/en-us/library/yfdfhz1b(VS.85).aspx)可以对注册表进行写操作。这个实例将实现添加Silverlight Out of Browser应用到Windows启动项。 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67ca1a6c4.jpg) ~~~   private void regWriteBtn_Click(object sender, RoutedEventArgs e)   {               using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))               {                   dynamic commonPrograms = ShellApplication.NameSpace(11);                  string allUsersPath = commonPrograms.Self.Path;                  dynamic directory = ShellApplication.NameSpace(allUsersPath + @"/Programs");                   dynamic link = directory.ParseName(Deployment.Current.OutOfBrowserSettings.ShortName + ".lnk");                  string OOBLink = link.Path;                  using (dynamic WShell = AutomationFactory.CreateObject("WScript.Shell"))                  {                      WShell.RegWrite(@"HKLM/Software/Microsoft/Windows/CurrentVersion/Run/"                                                   + Deployment.Current.OutOfBrowserSettings.ShortName,                                                   OOBLink);                      MessageBox.Show("请重启你的机器,你的应用将被自动载入启动列表.");                  }             }  } ~~~ 当运行以上代码后,应用会将OOB应用快捷方式写入注册表HKLM/Software/Microsoft/Windows/CurrentVersion/Run/ 应用程序将在下次重启后,自动启动。 实例5:使用Windows 7 API实现锁定应用到Windows 7任务栏 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67ca29112.jpg) 在Windows 7中使用Shell.Application类库允许遍历应用,检查Verbs进行应用锁定。 ~~~   private void pinBtn_Click(object sender, RoutedEventArgs e)   {               using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))               {                   dynamic commonPrograms = ShellApplication.NameSpace(23);                   string allUsersPath = commonPrograms.Self.Path;                  dynamic directory = ShellApplication.NameSpace(allUsersPath + @"/Accessories");                   dynamic link = directory.ParseName("Calculator.lnk");                  dynamic verbs = link.Verbs();                  for (int i = 0; i < verbs.Count(); i++)                  {                      dynamic verb = verbs.Item(i);                      if (verb.Name.Replace(@"&", string.Empty).ToLower() == "pin to taskbar")                      {                          verb.DoIt();                      }                  }              }  } ~~~ 当执行以上代码后,获取计算器应用快捷方式,然后执行“Pin to Taskbar”后,将应用锁定在Windows 7任务栏。 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67ca38de0.jpg) 实例6:Silverlight Out of Browser语音阅读实例 使用Windows自带的Speech API中的SAPI引擎SpVoice类可以实现语音阅读功能。 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-18_56eb67ca498c5.jpg) ~~~  private void speechBtn_Click(object sender, RoutedEventArgs e)  {              using (dynamic ISpeechVoice = AutomationFactory.CreateObject("SAPI.SpVoice"))              {                  ISpeechVoice.Volume = 100;                  ISpeechVoice.Speak("<rate speed=/"0/"><pitch middle=/"0/">Hello everyone! Welcome to my blog,http://jv9.cnblogs.com");              }  } ~~~ 当运行以上代码后,会听到以上阅读内容。 对于Silverlight Out of Browser的COM应用有一款开源项目COM Toolkit,该控件在OOB模式下可以对本地数据库进行操作,[推荐大家参考学习](http://silverlightchina.net/html/works/2010/0808/1702.html)。 今天就写到这里了,希望能对大家有所帮助。 [源代码下载](http://www.silverlightchina.net/resource/code/SilverlightOOBDemo0808.rar) 欢迎大家加入"专注Silverlight" 技术讨论群: 32679955(六群) 23413513(五群) 32679922(四群) 100844510(三群) 37891947(二群) 22308706(一群)  
';