JScript.NET  新JavaScript入門  JavaScript,Neo-Generation  DOM  WSH  掲示板  表紙
Written 1/28/04
JScript.NET
Font
Fontの使い方
Fontは、 例えば次のようにラベルのフォントを指定するのに使います。
    label2.js
    -----------------------------------------------------------------
        ...
    class MyForm extends Form {
        var lab1 : Label;
        function MyForm() {
            super();
                ...
            lab1 = new Label();
            lab1.Text = "Berryz";
            lab1.Font = new System.Drawing.Font("MS UI Gothic", 12);
            lab1.Location = new Point(15, 20);
            Controls.Add(lab1);
        }
    }
 
このフォームは「Berryz」という文字を「MS UI Gothic」の12ポイントで表示します。
Fontオブジェクトの作成
上の例で、フォントサイズを10ポイントにしたいとき、
    lab1.Font.SizeInPoints = 10;
 
というようにすればよさそうなものですが、 Fontオブジェクトは変更ができないので、 いちいち上のように生成しなければなりません。
Fontクラスのコンストラクタは色々あるのですが、 代表的と思われるものを紹介します。
    Font("MS UI Gothic", 12)
 
これは、フォント名とポイントを指定するものです。
    Font(lab1.Font, FontStyle.Bold)
 
これは、フォントオブジェクトとフォントスタイルを指定するものです。
フォントスタイルには System.Drawing.FontStyle 列挙体を用います。これには、 BoldItalicRegularStrikeoutUnderline というメンバが存在します。
複数のフォントスタイルを取るには次のようにします。
    //FontStyle列挙体を明示しないと、コンパイラに怒られる
    Font(lab1.Font, FontStyle(FontStyle.Italic | FontStyle.Underline))
 
このようにビットごとのOrを取ります。
    Font("Times New Roman", 12, FontStyle.Bold);
 
これは、フォント名とポイントとフォントスタイルを指定しています。
Font名を列挙する
インストールされているフォント名を列挙するには System.Drawing.Text.InstalledFontCollection オブジェクトを用います。
    enumfont.js
    ------------------------------------
    import System.Drawing;
    import System.Drawing.Text;
    
    var ifc : InstalledFontCollection;
    ifc = new InstalledFontCollection();
    var ff : FontFamily[];
    ff = ifc.Families;
    for(var i = 0; i < ff.Length; i++)
        print(ff[i].Name);
 
InstalledFontCollectionオブジェクトから FontFamilyオブジェクトの配列を得て、 それらの名前を表示します。
プロパティ
Fontオブジェクトは変更できないので、 値の取得のみ可能なプロパティが色々用意されています。
    font2.js
    ---------------------------------------------------------
    import System.Drawing;
    
    var f : Font = new Font("Times New Roman", 12,
            FontStyle(FontStyle.Bold | FontStyle.Underline));
    
    print(f.FontFamily);  //[FontFamily: Name=Times New Roman]
    print(f.Name);        //Times New Roman
    
    //FontStyle関係
    print(f.Bold);        //true
    print(f.Italic);      //false
    print(f.Strikeout);   //false
    print(f.Underline);   //true
    print(f.Style);       //Bold, Underline
    
    //大きさ関係
    print(f.Size);            //12
    print(f.SizeInPoints);    //12
    print(f.Height);          //19
    print(f.Unit);            //Point
 
JScript.NET exit