C# Windows CE使用小技巧实例

开发 后端
C# Windows CE使用小技巧实例主要向你介绍了WinCE下使用C#来打开一个外部文件、单步执行程序等等,希望对你学习了解C# Windows CE使用有所帮助。

C# Windows CE使用的一些感受:使用Windows的开发机上用C#启动一个外部程序的方法有很多,但这些方法用在使用WinCE的目标工控机上都无能为力。

C# Windows CE使用1、

现在以打开一个IE为例,介绍如何在WinCE下使用C#来打开一个外部文件:

首先添加命名空间

  1. usingSystem.Runtime.InteropServices;, 

然后调用API函数:

  1. [DllImport("coredll.Dll",  
  2. EntryPoint="CreateProcess",SetLastError=true)]  
  3.  
  4. externstaticintCreateProcess(  
  5. stringstrImageName,stringstrCmdLine,  
  6. IntPtrpProcessAttributes,IntPtrpThreadAttributes,  
  7. intbInheritsHandle,intdwCreationFlags,  
  8. IntPtrpEnvironment, IntPtrpCurrentDir,  
  9. IntPtrbArray,ProcessInfooProc);  
  10.  
  11. publicclassProcessInfo  
  12.  
  13. {  
  14.  
  15. publicInt32hProcess;  
  16.  
  17. publicInt32hThread;  
  18.  
  19. publicInt32ProcessID;  
  20.  
  21. publicInt32ThreadID;  
  22.  

最后就可以编写需要打开IE的代码了(点击一个按钮打开IE浏览器中相应内容,此例程要求打开目标工控机硬盘上的Readme文件):

  1. privatevoidbutton_Click(  
  2. objectsender,System.EventArgse)  
  3.  
  4. {  
  5.  
  6. ProcessInfopi=newProcessInfo();  
  7.  
  8. CreateProcess(" \\windows\\iesample.exe",  
  9. "\\HardDisk\\Readme.htm",IntPtr.Zero,  
  10. IntPtr.Zero,0,0,IntPtr.Zero,  
  11. IntPtr.Zero,IntPtr.Zero,pi);  
  12.  

C# Windows CE使用2、

有时候我们会希望我们的程式只被执行一次,VB的时代我们会用App.PrevInstance,而.net的时代我们可以用下列方式实现

  1. [STAThread]  
  2.  
  3. staticvoidMain()  
  4.  
  5. {  
  6.  
  7. //如果跟本程式命名的行程只有一个才执行程式  
  8.  
  9. if(System.Diagnostics.Process.  
  10. GetProcessesByName(  
  11.  
  12. Application.ProductName).Length==1)  
  13.  
  14. {  
  15.  
  16. Application.Run(newForm1());  
  17.  
  18. }  
  19.  

但此方法在WinCE下无法实现,所以我们还是要先调用动态链接库,

  1. [DllImport("coredll.Dll")]  
  2.  
  3. privatestaticexternintGetLastError();  
  4.  
  5. [DllImport("coredll.Dll")]  
  6.  
  7. privatestaticexternintReleaseMutex(IntPtrhMutex);  
  8.  
  9. [DllImport("coredll.Dll")]  
  10.  
  11. privatestaticexternIntPtrCreateMutex(  
  12. SECURITY_ATTRIBUTESlpMutexAttributes,  
  13. boolbInitialOwner,stringlpName);  
  14.  
  15. [StructLayout(youtKind.Sequential)]  
  16.  
  17. publicclassSECURITY_ATTRIBUTES  
  18.  
  19. {  
  20.  
  21. publicintnLength;  
  22.  
  23. publicintlpSecurityDescriptor;  
  24.  
  25. publicintbInheritHandle;  
  26.  
  27. }  
  28.  
  29. constintERROR_ALREADY_EXISTS=0183;  

然后编写代码

  1. staticvoidMain()  
  2.  
  3. {  
  4.  
  5. #regionApi_CallCreateMutex;  
  6.  
  7. IntPtrhMutex;  
  8.  
  9. hMutex=CreateMutex(null,false,"程序名");  
  10.  
  11. if(GetLastError()!=ERROR_ALREADY_EXISTS)  
  12.  
  13. {  
  14.  
  15. Application.Run(newFrmmenu());  
  16.  
  17. }  
  18.  
  19. else 
  20.  
  21. {  
  22.  
  23. MessageBox.Show("本程序只允许同时运行一个");  
  24.  
  25. ReleaseMutex(hMutex);  
  26.  
  27. }  
  28.  
  29. #endregion  
  30.  

C# Windows CE使用3、

在.NETFramework中没有函数可以激活属于另外一个进程或程序的窗体,所以我们要通过调用API函数来实现:

  1. usingSystem.Runtime.InteropServices;  
  2.  
  3. [DllImport("coredll.Dll")]  
  4.  
  5. publicstaticexternIntPtrFindWindow(  
  6. Stringclassname,Stringtitle);  
  7.  
  8. [DllImport("coredll.Dll")]  
  9.  
  10. publicstaticexternvoidSetForegroundWindow(IntPtrhwnd); 

然后使用下列代码即可

  1. IntPtrhDlg;  
  2.  
  3. hDlg=FindWindow(null,"窗口标题");  
  4.  
  5. SetForegroundWindow(hDlg); 

最后,WinCE下的C#里不支持GroupBox控件,建议使用Panel控件代替;不支持Frame控件,如果非要达到那样的效果,可以用Label和TextBox组和起来应付一下。

其实,任何时候,只要.NETFramework无法满足编程者需要的时候,通常都可以使用托管(interop)机制直接与Windows交互。大家也许看出调用原有的[DllImport("user32.Dll")]动态链接库时无法满足WinCE下程序要求,所以我们调用了[DllImport("coredll.Dll")]。希望这篇文章能给初学者提供一些捷径。

C# Windows CE使用的一些感受和实例的介绍就向你介绍到这里,希望对你了解C# Windows CE使用有所帮助。

【编辑推荐】

  1. C#Windows应用程序开发之窗体控件
  2. C#Windows应用程序开发之添加菜单
  3. C#Windows应用程序开发之添加状态条
  4. C#Windows应用程序开发之事件处理器
  5. c# Windows CE读取电池电量的实现
责任编辑:仲衡 来源: wmisv.com.cn
相关推荐

2009-08-17 09:57:00

C# Windows

2009-08-17 10:29:58

C# Windows

2009-08-17 10:02:58

C# Windows

2009-08-17 10:22:19

C# Windows

2009-08-17 10:26:34

C# Windows

2009-08-17 10:11:12

C# Windows

2009-08-17 10:17:01

C# Windows

2009-08-17 09:27:12

c# Windows

2009-08-27 15:17:18

C# interfacinterface使用

2009-09-09 22:31:21

c# textbox失

2009-09-14 14:25:53

C# Lambda EC# Lambda

2009-08-14 16:32:50

C#启动Windows

2009-09-02 18:44:19

C#递归

2009-09-04 15:53:42

C#内存流

2009-08-26 13:36:33

C#打印控件

2009-08-13 14:56:46

C#的结构体使用

2009-12-31 10:49:36

VPN配置实例

2009-08-14 17:04:19

Windows后台服务

2009-08-28 16:37:32

C# for循环

2013-09-30 10:11:40

Windows 8技巧
点赞
收藏

51CTO技术栈公众号