zoukankan      html  css  js  c++  java
  • List<T>的Add方法等

    1.以前常用List<T>的Add方法,没怎么在意,有个问题常常困扰。

    static List<object> a = new List...

    Addb 
    void(){
       
    object b = new object;
       a.Add(b);
    }

    b作为临时变量,程序Addb执行完后 object b会进行释放。但是依然可以访问到a[0]这个值。

    看了MSDN对Add的解释。

    "在添加新元素之前,会通过自动重新分配内部数组来增加 List<(Of <(T>)>) 的容量并会将现有元素复制到新数组中。"

    也就是说a执行Add时,先申请一段内存空间,将b的内容复制过来(内存拷贝)。而不是只在a中存放了b的地址。List不是链表。所以上面a中的元素得以保存。

    2.List<T>如何及时释放。

    在WINCE编程中,内存有限,因此希望资源能够尽快的被垃圾回收器GC回收。

    可以手动调用

    StaticClass.StaticList = null;
    GC.Collect();

    进行及时的资源释放。

    3.WINCE中动态添加和使用字体。

    需要用到WINCE API函数

    代码
    [DllImport("coredll", EntryPoint = "AddFontResource")]
    private static extern int AddFontResource([In, MarshalAs(UnmanagedType.LPWStr)]string fontSource);

    [DllImport(
    "coredll", EntryPoint = "SendMessage")]
    private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    [DllImport(
    "coredll", EntryPoint = "RemoveFontResource")]
    private static extern int RemoveFontResource([In, MarshalAs(UnmanagedType.LPWStr)]string fontSource);

    程序运行的时候执行

    int installFont = AddFontResource(@"\Temp\simsun.ttc");
    if (installFont != 0)
    {
       SendMessage((IntPtr)
    0xffff0x001d, IntPtr.Zero, IntPtr.Zero);
    }

    结束时需要释放,否则会有内存泄露

    int removeFont = RemoveFontResource(@"\Temp\simsun.ttc");
    if (removeFont != 0){
      SendMessage((IntPtr)
    0xffff0x001d, IntPtr.Zero, IntPtr.Zero);
    }

    SendMessage的作用是通知所有程序,字体已经改变,VC #define WM_FONTCHANGE 0x001d;

    通过以下程序查看目前能够使用的字体

    InstalledFontCollection enumFonts = new InstalledFontCollection();
    FontFamily[] fonts 
    = enumFonts.Families;
    foreach (FontFamily font in fonts){
        MessageBox.Show(font.Name);
    }
  • 相关阅读:
    《算法》第六章部分程序 part 5
    《算法》第六章部分程序 part 4
    《算法》第六章部分程序 part 3
    《算法》第六章部分程序 part 2
    《算法》第六章部分程序 part 1
    OpenGL Hello World
    《算法》第五章部分程序 part 3
    《算法》第五章部分程序 part 8
    《算法》第五章部分程序 part 7
    《算法》第五章部分程序 part 6
  • 原文地址:https://www.cnblogs.com/poplau/p/1852332.html
Copyright © 2011-2022 走看看