windows mobile 5.0 进程管理、窗体管理、重启和关闭操作系统
1、进程管理:在NET Compact Framework里进程管理的函数相对要比net Framework里要简化,不过仍然可以比较好的控制程序进程。
A.启动进程:在启动进程后返回进程的id
/// <summary> /// 启动进程 /// </summary> /// <param name="fileName">启动文件的目录路径</param> /// <returns>返回启动进程的进程ID</returns> public static int StartProcess(string fileName) { int progressID = 0; try { //这个目录是动态的 progressID = System.Diagnostics.Process.Start(fileName).Id; return progressID; } catch //(Exception ex) { //throw ex; return 0; } }
B.获取当前进程的ID
/// <summary> /// 获取当前进程ID /// </summary> public static int GetCurrentProcessID() { Process currentProcess = Process.GetCurrentProcess(); int CurrentProcessID = currentProcess.Id; return CurrentProcessID; }
C.终止当前进程
/// <summary> /// 终止当前进程 /// </summary> public static void KillCurrentProcess() { System.Diagnostics.Process.GetCurrentProcess().Kill(); }
D.通过进程ID来终止进程
/// <summary> /// 终止进程 /// </summary> /// <param name="progressID">进程ID</param> /// <returns>true 表示终止成功,反之表示失败</returns> public static bool KillProcess(int progressID) { try { System.Diagnostics.Process.GetProcessById(progressID).Kill(); return true; } catch //(Exception ex) { //throw ex; return false; } }
2、窗体管理。在NET Compact Framework(2.0)的窗体类(System.Windows.Forms.Form))虽然提供WindowsState属性,这个属性是 FormWindowState 枚举类型,在枚举中只有Normal 和 Maximized,不过不能通过Maximized来控制窗体最小,同时也无法控制窗体的关闭。要是实现关闭和最小化只能通过调用api来实现。下面是有关代码:
[DllImport("coredll.dll")] private extern static bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("coredll.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); ///最小化窗体 public static void WindowsMin(string frmTextName) { IntPtr hwnd = FindWindow(null, frmTextName); ShowWindow(hwnd, 6); } ///隐藏窗体 public static void WindowHide(string frmTextName) { IntPtr hwnd = FindWindow(null, frmTextName); ShowWindow(hwnd, 0); }
3、重启和关闭操作系统。同样是调用api来完成。
[DllImport("Aygshell.dll")] static extern bool ExitWindowsEx(uint uFlags, int dwReserved); private const uint EWX_REBOOT = 2; private const uint EWX_POWEROFF = 8; ///重启操作系统 public static void RootWindows() { ExitWindowsEx(EWX_REBOOT, 0); } ///关闭操作系统 public static void ShutDownWindows() { ExitWindowsEx(EWX_POWEROFF, 0); }
以上代码的运行环境和开发环境:windows mobile5.0 +ppc sdk +vs2005
本文首发地址:http://www.watch-life.net/windows-mobile/process-window-form-system-manager.html
-------------------------------------------
更多文章见:守望轩[http://www.watch-life.net]