zoukankan      html  css  js  c++  java
  • 使用 C# 开发智能手机软件:推箱子(二)

    在上篇文章“使用 C# 开发智能手机软件:推箱子(一)”中。我对推箱子程序作了整体介绍。这次,我先介绍 Common/Fcl.cs 源程序文件。
     1 using System;
     2 using System.IO;
     3 using System.Drawing;
     4 
     5 namespace Skyiv.Ben.PushBox.Common
     6 {
     7   /// <summary>
     8   /// 这里是 .NET Framework 支持。而 .NET Compact Framework 不支持的东东
     9   /// </summary>
    10   static class Fcl
    11   {
    12     /// <summary>
    13     /// 获取为此环境定义的换行字符串。-- Environment
    14     /// </summary>
    15     public static string NewLine { get { return " ";  } }
    16 
    17     /// <summary>
    18     /// 打开一个文本文件,将文件的全部行读入一个字符串,然后关闭该文件。-- File
    19     /// </summary>
    20     /// <param name="path">要打开以进行读取的文件</param>
    21     /// <returns>包括文件全部行的字符串</returns>
    22     public static string ReadAllText(string path)
    23     {
    24       string text = "";
    25       if (File.Exists(path))
    26       {
    27         using (StreamReader sr = new StreamReader(path, Pub.Encode))
    28         {
    29           text = sr.ReadToEnd();
    30         }
    31       }
    32       return text;
    33     }
    34 
    35     /// <summary>
    36     /// 创建一个新文件。在当中写入指定的字符串,然后关闭该文件。-- File
    37     /// </summary>
    38     /// <param name="path">要写入的文件</param>
    39     /// <param name="contents">要写入文件的字符串</param>
    40     public static void WriteAllText(string path, string contents)
    41     {
    42       using (StreamWriter sw = new StreamWriter(path, false, Pub.Encode))
    43       {
    44         sw.Write(contents);
    45       }
    46     }
    47 
    48     /// <summary>
    49     /// 将指定的 Size 加入到指定的 Point。-- Point
    50     /// </summary>
    51     /// <param name="point">要加入的 Point</param>
    52     /// <param name="size">要加入的 Size</param>
    53     /// <returns>加法运算的结果</returns>
    54     public static Point Add(Point point, Size size)
    55     {
    56       return new Point(point.X + size.Width, point.Y + size.Height);
    57     }
    58 
    59     /// <summary>
    60     /// 将一维数组的大小更改为指定的新大小。-- Array
    61     /// </summary>
    62     /// <typeparam name="T">数组元素的类型</typeparam>
    63     /// <param name="array">要调整大小的一维数组</param>
    64     /// <param name="newSize">新数组的大小</param>
    65     public static void Resize<T>(ref T[] array, int newSize)
    66     {
    67       if (array != null && array.Length == newSize) return;
    68       if (array == null) array = new T[0];
    69       T[] newArray = new T[newSize];
    70       Array.Copy(array, newArray, Math.Min(array.Length, newArray.Length));
    71       array = newArray;
    72     }
    73   }
    74 }
    75 
        俗话说。工欲善其事,必先利其器。我们知道,Microsoft .NET Compact Framework 仅仅是 Microsoft .NET Framework 的一个子集。她省略了一些不经常使用的功能。

    可是,假设我们恰好须要这些功能,仅仅好自己又一次实现一下了。这个 Fcl 静态类就是起这个作用的。

    源程序代码的凝视已经写得非常清楚了。


        Fcl.NewLine 我原本是想写成这种:

      static class Fcl
      {
        
    static static string newLine;
        
        
    /// <summary>
        
    /// 获取为此环境定义的换行字符串。

    -- Environment
        /// </summary>
        public static string NewLine
        {
          
    get
          {
            
    if (newLine == null)
            {
              newLine 
    = (Environment.OSVersion.Platform != PlatformID.Unix) ?

     " " : " ";
            }
            
    return newLine;
          }
        }
      }

        可惜的是。这段代码无法在 .NET Compact Framework 下通过编译(假设是 .NET Framework 则没有问题)。原因是 PlatformID 枚举的成员:
    Unix         操作系统为 Unix。
    Win32NT      操作系统为 Windows NT 或较新的版本号。
    Win32S       操作系统为 Win32s(Win32 子集)类型。
    Win32Windows 操作系统为 Windows 
    95 或较新的版本号。
    WinCE        操作系统为 Windows CE。
        PlatformID.Unix 并不被 .NET CF 所支持。这实在是一件非常奇怪的事,既然 .NET CF 都支持 PlatformID 的 Win32NT、Win32S、Win32Windows、WinCE 成员,为什么就不能支持 Unix 成员呢?这样,这个程序将来要移植到 Linux 操作系统时就有些小麻烦了。


        要知道,这在主窗口的代码中用下面一段代码来实如今智能手机上禁用“前端显示”功能。

      public partial class MainForm : Form
      {
        
    protected override void OnLoad(EventArgs e)
        {
          
    base.OnLoad(e);
          miTopMost.Enabled 
    = (Environment.OSVersion.Platform != PlatformID.WinCE);
          env.LoadConfig();
          env.LoadGroup();
          LoadLevel(
    true);
          
    if (env.IsSave) Restore(env.Steps);
        }
  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6710002.html
Copyright © 2011-2022 走看看