1、我们知道,在使用Windows的开发机上用C#启动一个外部程序的方法有很多,但这些方法用在使用WinCE的目标工控机上都无能为力,现在小嫚儿以打开一个IE为例,介绍如何在WinCE下使用C#来打开一个外部文件:首先添加命名空间

using System.Runtime.InteropServices;,然后调用API函数:

[DllImport("coredll.Dll",  EntryPoint="CreateProcess",  SetLastError=true)] 
extern  static  int  CreateProcess(string  strImageName,  string  strCmdLine,  IntPtr pProcessAttributes,  IntPtr  pThreadAttributes  ,  int  bInheritsHandle,  int dwCreationFlags,  IntPtr  pEnvironment,  IntPtr  pCurrentDir,  IntPtr  bArray, ProcessInfo  oProc); 
  public  class  ProcessInfo 
  { 
   public  Int32  hProcess; 
   public  Int32  hThread; 
   public  Int32  ProcessID; 
   public  Int32  ThreadID; 
  } 
最后就可以编写你需要打开IE的代码了(点击一个按钮打开IE浏览器中相应内容,此例程要求打开目标工控机硬盘上的Readme文件):
 private void button_Click(object sender, System.EventArgs e)
  {
   ProcessInfo  pi  =  new  ProcessInfo(); 
   CreateProcess("\\windows\\iesample.exe"  ,  "\\HardDisk\\Readme.htm", IntPtr.Zero,  IntPtr.Zero,  0,  0,  IntPtr.Zero,  IntPtr.Zero,  IntPtr.Zero,  pi);   
  }
2、有时候我们会希望我们的程式只被执行一次,VB的时代我们会用App.PrevInstance,而.net的时代我们可以用下列方式实现
  [STAThread]
 static void Main()
{
   //如果跟本程式命名的行程只有一个才执行程式
   if (System.Diagnostics.Process.GetProcessesByName(
     Application.ProductName).Length == 1)
  {
    Application.Run(new Form1());
  }
}
但此方法在WinCE下无法实现,所以我们还是要先调用动态链接库,
[DllImport("coredll.Dll")]
private static extern int GetLastError();

[DllImport("coredll.Dll")]
private static extern int ReleaseMutex(IntPtr hMutex);

[DllImport("coredll.Dll")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);

[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
 public int nLength;
 public int lpSecurityDescriptor;
 public int bInheritHandle;
}
const int ERROR_ALREADY_EXISTS = 0183;
然后编写代码
static void Main()
{  
  #region Api_Call CreateMutex;
IntPtr hMutex;
  hMutex=CreateMutex(null,false,"程序名");
  if (GetLastError()!=ERROR_ALREADY_EXISTS)
  {
   Application.Run(new Frmmenu()); 
  }
  else
  {
   MessageBox.Show("本程序只允许同时运行一个");
   ReleaseMutex(hMutex);
 }
 #endregion
}
3、有时我们希望自己的程序能做到激活一个窗体,但在 .NET  Framework 中没有函数可以激活属于另外一个进程或程序的窗体。所以,我们还是要调用API函数来实现:
using System.Runtime.InteropServices;
[DllImport("coredll.Dll")]
 public static extern IntPtr FindWindow(String classname, String title);

[DllImport("coredll.Dll")]
 public static extern void  SetForegroundWindow(IntPtr hwnd);
然后使用下列代码即可
IntPtr hDlg;
hDlg=FindWindow(null, "窗口标题");
SetForegroundWindow( hDlg );
最后,WinCE下的C#里不支持GroupBox控件,建议使用Panel控件代替;不支持Frame控件,如果非要达到那样的效果,可以用Label和TextBox组和起来应付一下。
其实,任何时候,只要.NET Framework无法满足编程者的需要,通常都可以使用托管(interop)机制直接与Windows 交互。大家也许看出调用原有的[DllImport("user32 .Dll")]动态链接库时无法满足WinCE下程序要求,所以我们调用了[DllImport("coredll.Dll")]

最新文章

  1. viewPager动态加载listview数据
  2. vim命令/压缩和解压命令
  3. PostgreSQL的 initdb 源代码分析之二十四
  4. 如何添加网站for Linux(绑定域名)
  5. STM32 IAP 在线更新程序 为什么有时行 有时又不行 感觉不可靠 问题解决
  6. Canvas模糊化处理图片、毛玻璃处理图片之stackblur.js
  7. keil c编译器错误与解决方法
  8. STL模板_智能指针概念
  9. 最近项目用到Dubbo框架,分享一下~
  10. 24个 CSS 高级技巧合集
  11. Angular刷新浏览器 404 问题
  12. .net core 认证
  13. Win10新增功能快捷键大全
  14. Memcached概念、作用、运行原理、特性、不足简单梳理(1)
  15. 正则表达式学习之grep,sed和awk
  16. centos7 firewalled
  17. scala (8) 模糊匹配
  18. maven项目的多级目录
  19. shell脚本 入门 —— 符号篇
  20. 跨平台移动开发 Xuijs超轻量级的框架Style CSS属性用法

热门文章

  1. 同余方程(codevs 1200)
  2. class文件检查器
  3. linux-3.2.36内核启动2-setup_arch中的内存初始化1(arm平台 分析高端内存和初始化memblock)【转】
  4. 微信小程序 使用HMACSHA1和md5为登陆注册报文添加指纹验证签名
  5. SpringBoot 整合 RabbitMQ(包含三种消息确认机制以及消费端限流)
  6. Flink起步安装和使用
  7. SQL SERVER 工具
  8. 一个能让你了解所有函数调用顺序的Android库
  9. Canvas的效果操作及save()和restore()方法应用
  10. win7 32位安装pyqt