▼.Net Compact Framework 2.0メモ▼
文字列の表示


文字列を表示するプログラムを作成する。



デザインの編集
StringForm
プロパティ
Text StringEx
MinimizeBox false
FormFactor Windows Mobile 6 Professional VGA


ソースコードの編集
StringForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StringEx {
    public partial class StringForm : Form {
        private Font       font;     //フォント
        private SolidBrush fontColor;//フォント色

        //コンストラクタ
        public StringForm() {
            InitializeComponent();

            //デフォルトフォント
            font=new Font("MS ゴシック",6,FontStyle.Regular);
            fontColor=new SolidBrush(Color.Black); 
        }

        //フォントの指定
        private void SetFont(Font font) {
            this.font.Dispose();
            this.font=font;
        }

        //フォント名の指定
        private void SetFontName(String name) {
            SetFont(new Font(name,font.Size,font.Style));
        }

        //フォントサイズの指定
        private void SetFontSize(float size) {
            SetFont(new Font(font.Name,size,font.Style));
        }

        //フォントスタイルの指定
        private void SetFontStyle(FontStyle style) {
            SetFont(new Font(font.Name,font.Size,style));
        }

        //フォント色の指定
        private void SetFontColor(int r,int g,int b) {
            fontColor=new SolidBrush(Color.FromArgb(r,g,b));
        }

        //文字列の描画
        private void DrawString(Graphics g,String text,int x,int y) {
            g.DrawString(text,font,fontColor,x,y);
        }

        //描画イベント
        protected override void OnPaint(PaintEventArgs pea) {
            Graphics g=pea.Graphics;
            int dy=0;
            String str;
            
            //画面サイズ
            SetFontColor(255,0,0);
            SetFontStyle(FontStyle.Bold);
            DrawString(g,"画面サイズ",0,dy);
            dy+=20;

            SetFontColor(0,0,0);
            SetFontStyle(FontStyle.Regular);
            DrawString(g,ClientSize.Width+"x"+ClientSize.Height,0,dy);
            dy+=40;

            //フォント名
            SetFontColor(255,0,0);
            SetFontStyle(FontStyle.Bold);
            DrawString(g,"フォント名",0,dy);
            dy+=20;
            SetFontColor(0,0,0);
            SetFontStyle(FontStyle.Regular);
            
            //フォント名リスト           
            System.Drawing.Text.InstalledFontCollection ifc=
                new System.Drawing.Text.InstalledFontCollection();
            FontFamily[] ffs=ifc.Families;
            foreach (FontFamily ff in ffs) {                
                SetFont(new Font(ff,font.Size,font.Style));
                DrawString(g,font.Name,0,dy);
                dy+=20;
            }
            dy+=20;

            //フォントサイズ
            SetFontColor(255,0,0);
            SetFont(new Font("MS ゴシック",6,FontStyle.Bold));
            DrawString(g,"フォントサイズ",0,dy);
            dy+=20;
            SetFontColor(0,0,0);
            SetFontStyle(FontStyle.Regular);
            
            //フォントサイズ[1-10]
            str="[ 1-10]";
            for (int i=1;i<=10;i++) {
                SetFontSize(i);
                int fw=(int)g.MeasureString("A",font).Width;
                int fh=(int)g.MeasureString("A",font).Height;
                str+=fw+"x"+fh+",";
            }
            SetFontSize(6);
            DrawString(g,str,0,dy);
            dy+=20;

            //フォントサイズ[11-20]
            str="[11-20]";
            for (int i=11;i<=20;i++) {
                SetFontSize(i);
                int fw=(int)g.MeasureString("A",font).Width;
                int fh=(int)g.MeasureString("A",font).Height;
                str+=fw+"x"+fh+",";
            }
            SetFontSize(6);
            DrawString(g,str,0,dy);
            dy+=40;

            //フォントスタイル
            SetFontColor(255,0,0);
            SetFontStyle(FontStyle.Bold);
            DrawString(g,"フォントスタイル",0,dy);
            dy+=20;
            SetFontColor(0,0,0);
            SetFontStyle(FontStyle.Regular);

            //レギュラー
            SetFontStyle(FontStyle.Regular);
            DrawString(g,"Regular",0,dy);
            dy+=20;

            //ボールド
            SetFontStyle(FontStyle.Bold);
            DrawString(g,"Bold",0,dy);
            dy+=20;

            //イタリック
            SetFontStyle(FontStyle.Italic);
            DrawString(g,"Italic",0,dy);
            dy+=20;

            //打ち消し線
            SetFontStyle(FontStyle.Strikeout);
            DrawString(g,"Strikeout",0,dy);
            dy+=20;

            //アンダーライン
            SetFontStyle(FontStyle.Underline);
            DrawString(g,"Underline",0,dy);
            dy+=20;
        }
    }
}



−戻る−