日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C# Windows CE使用小技巧实例

發布時間:2023/11/27 生活经验 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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. ?
  23. }

最后就可以編寫需要打開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. ?
  13. }

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. ?
  20. }

但此方法在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. ?
  31. }

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使用有所幫助。

轉載于:https://www.cnblogs.com/cwfsoft/archive/2010/06/23/1763217.html

總結

以上是生活随笔為你收集整理的C# Windows CE使用小技巧实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。