C# Windows CE使用小技巧實(shí)例
C# Windows CE使用的一些感受:使用Windows的開(kāi)發(fā)機(jī)上用C#啟動(dòng)一個(gè)外部程序的方法有很多,但這些方法用在使用WinCE的目標(biāo)工控機(jī)上都無(wú)能為力。
C# Windows CE使用1、
現(xiàn)在以打開(kāi)一個(gè)IE為例,介紹如何在WinCE下使用C#來(lái)打開(kāi)一個(gè)外部文件:
首先添加命名空間
- usingSystem.Runtime.InteropServices;,
然后調(diào)用API函數(shù):
- [DllImport("coredll.Dll",
- EntryPoint="CreateProcess",SetLastError=true)]
- externstaticintCreateProcess(
- stringstrImageName,stringstrCmdLine,
- IntPtrpProcessAttributes,IntPtrpThreadAttributes,
- intbInheritsHandle,intdwCreationFlags,
- IntPtrpEnvironment, IntPtrpCurrentDir,
- IntPtrbArray,ProcessInfooProc);
- publicclassProcessInfo
- {
- publicInt32hProcess;
- publicInt32hThread;
- publicInt32ProcessID;
- publicInt32ThreadID;
- }
最后就可以編寫(xiě)需要打開(kāi)IE的代碼了(點(diǎn)擊一個(gè)按鈕打開(kāi)IE瀏覽器中相應(yīng)內(nèi)容,此例程要求打開(kāi)目標(biāo)工控機(jī)硬盤上的Readme文件):
- privatevoidbutton_Click(
- objectsender,System.EventArgse)
- {
- ProcessInfopi=newProcessInfo();
- CreateProcess(" \\windows\\iesample.exe",
- "\\HardDisk\\Readme.htm",IntPtr.Zero,
- IntPtr.Zero,0,0,IntPtr.Zero,
- IntPtr.Zero,IntPtr.Zero,pi);
- }
C# Windows CE使用2、
有時(shí)候我們會(huì)希望我們的程式只被執(zhí)行一次,VB的時(shí)代我們會(huì)用App.PrevInstance,而.net的時(shí)代我們可以用下列方式實(shí)現(xiàn)
- [STAThread]
- staticvoidMain()
- {
- //如果跟本程式命名的行程只有一個(gè)才執(zhí)行程式
- if(System.Diagnostics.Process.
- GetProcessesByName(
- Application.ProductName).Length==1)
- {
- Application.Run(newForm1());
- }
- }
但此方法在WinCE下無(wú)法實(shí)現(xiàn),所以我們還是要先調(diào)用動(dòng)態(tài)鏈接庫(kù),
- [DllImport("coredll.Dll")]
- privatestaticexternintGetLastError();
- [DllImport("coredll.Dll")]
- privatestaticexternintReleaseMutex(IntPtrhMutex);
- [DllImport("coredll.Dll")]
- privatestaticexternIntPtrCreateMutex(
- SECURITY_ATTRIBUTESlpMutexAttributes,
- boolbInitialOwner,stringlpName);
- [StructLayout(youtKind.Sequential)]
- publicclassSECURITY_ATTRIBUTES
- {
- publicintnLength;
- publicintlpSecurityDescriptor;
- publicintbInheritHandle;
- }
- constintERROR_ALREADY_EXISTS=0183;
然后編寫(xiě)代碼
- staticvoidMain()
- {
- #regionApi_CallCreateMutex;
- IntPtrhMutex;
- hMutex=CreateMutex(null,false,"程序名");
- if(GetLastError()!=ERROR_ALREADY_EXISTS)
- {
- Application.Run(newFrmmenu());
- }
- else
- {
- MessageBox.Show("本程序只允許同時(shí)運(yùn)行一個(gè)");
- ReleaseMutex(hMutex);
- }
- #endregion
- }
C# Windows CE使用3、
在.NETFramework中沒(méi)有函數(shù)可以激活屬于另外一個(gè)進(jìn)程或程序的窗體,所以我們要通過(guò)調(diào)用API函數(shù)來(lái)實(shí)現(xiàn):
- usingSystem.Runtime.InteropServices;
- [DllImport("coredll.Dll")]
- publicstaticexternIntPtrFindWindow(
- Stringclassname,Stringtitle);
- [DllImport("coredll.Dll")]
- publicstaticexternvoidSetForegroundWindow(IntPtrhwnd);
然后使用下列代碼即可
- IntPtrhDlg;
- hDlg=FindWindow(null,"窗口標(biāo)題");
- SetForegroundWindow(hDlg);
最后,WinCE下的C#里不支持GroupBox控件,建議使用Panel控件代替;不支持Frame控件,如果非要達(dá)到那樣的效果,可以用Label和TextBox組和起來(lái)應(yīng)付一下。
其實(shí),任何時(shí)候,只要.NETFramework無(wú)法滿足編程者需要的時(shí)候,通常都可以使用托管(interop)機(jī)制直接與Windows交互。大家也許看出調(diào)用原有的[DllImport("user32.Dll")]動(dòng)態(tài)鏈接庫(kù)時(shí)無(wú)法滿足WinCE下程序要求,所以我們調(diào)用了[DllImport("coredll.Dll")]。希望這篇文章能給初學(xué)者提供一些捷徑。
C# Windows CE使用的一些感受和實(shí)例的介紹就向你介紹到這里,希望對(duì)你了解C# Windows CE使用有所幫助。
【編輯推薦】