GDI+ (Font)
開発環境: Visual Studio 2003
1.目次 | ||||||||||||||||||||||
2.目的 | ||||||||||||||||||||||
GDI+ の特にフォント周りについてです。基本的なことなので、押さえておかないとね。 | ||||||||||||||||||||||
3.参考 | ||||||||||||||||||||||
(1) Microsoft チュートリアル | ||||||||||||||||||||||
4.フォントの扱い | ||||||||||||||||||||||
Font には、ご存知のように山のようにフォントがあります。これは、コントロールパネル→フォントで確認できます。 | ||||||||||||||||||||||
4-1.フォントのスタイル確認用テストコード | ||||||||||||||||||||||
2003/1/18 初版作成 簡単なコードなので、コード中のコメントを読んでもらえればわかると思います。using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace EnumFonts
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
DrawFonts();
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(200, 176);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pictureBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private System.Windows.Forms.PictureBox pictureBox1;
private Bitmap bitmap = null;
/// <summary>
/// 表示速度の高速化のため、一度すべてのフォントを Bitmap に描画してから、
/// PictureBox に OnPaint で描画するようにしています。
/// </summary>
private void DrawFonts()
{
float x = 0f; // フォントのX 描画位置
float y = 0f; // フォントのY 描画位置
// おおもとのBitmap の大きさは、決めうちでかなり大きめに取ってます。
Size winSize = SystemInformation.PrimaryMonitorMaximizedWindowSize;
bitmap = new Bitmap(winSize.Width, winSize.Height*2);
Graphics g = Graphics.FromImage(bitmap);
// フォントスタイルを列挙して、Array に入れておく。
Array fontStyles = Enum.GetValues(typeof(FontStyle));
Font font = null;
// デフォルトのフォント色は、SystemColors.WindowText がお勧め。
SolidBrush brush = new SolidBrush(SystemColors.WindowText);
SolidBrush redBrush = new SolidBrush(Color.Red);
// 横方向にフォントスタイル
foreach(FontStyle fs in fontStyles)
{
// 縦方向にフォントファミリー
float width = 0f;
foreach(FontFamily ff in FontFamily.Families)
{
// スタイルがサポートされているかどうか確認する。
if(ff.IsStyleAvailable(fs))
{
font = new Font(ff, 10, fs);
g.DrawString(ff.Name, font, brush, x, y);
width = Math.Max(width, g.MeasureString(ff.Name, font).Width);
}
else
{
// スタイルがサポートされていなければ、デフォルトフォントで書く。
g.DrawString("※※※NA※※※", this.Font, redBrush, x, y);
width = Math.Max(width, g.MeasureString("※※※NA※※※", this.Font).Width);
}
y += font.Height;
this.pictureBox1.Height = (int)y;
}
x += width;
this.pictureBox1.Width = (int)x;
y = 0;
}
}
/// <summary>
/// PictureBox の OnPaint で Bitmap から DrawImage することにより、
/// 毎回フォントを作成して描画することを避ける。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(new Point(0, 0), this.pictureBox1.Size);
g.DrawImage(bitmap, rect, rect, GraphicsUnit.Pixel);
}
}
}
| ||||||||||||||||||||||
5.フォントのパス | ||||||||||||||||||||||
GraphicsPath.AddString(...) でフォントのアウトラインを取得することができます。(下図)
|


