2009年12月27日日曜日

年賀状フォントエクスプローラ

Form1.csがここから。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
public partial class NewYearCardFontExplorer : Form
{
public NewYearCardFontExplorer()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
System.Drawing.Text.InstalledFontCollection ifc=new System.Drawing.Text.InstalledFontCollection();
List lstFontFamily = new List();
foreach (FontFamily tmpFontFamily in ifc.Families)
if (tmpFontFamily.IsStyleAvailable(FontStyle.Regular)) lstFontFamily.Add(tmpFontFamily);
comboBoxFont.DataSource = lstFontFamily;
comboBoxFont.DisplayMember = "Name";
comboBoxFont.DropDownStyle = ComboBoxStyle.DropDownList;
trackBarFontSize.Value = 12;
labelFontSize.Text = trackBarFontSize.Value.ToString();
textBoxInputText.Text = "謹賀新年\nあけまして おめでとうございます ことしもよろしくおねがいします。\n Merry Cristmas and Happy New Year";
comboBoxHorizontal.DataSource = Enum.GetNames(typeof(StringAlignment));
comboBoxHorizontal.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxVertical.DataSource = Enum.GetNames(typeof(StringAlignment));
comboBoxVertical.DropDownStyle = ComboBoxStyle.DropDownList;

listBoxFontStyle.DataSource = Enum.GetValues(typeof(FontStyle));
listBoxFontStyle.SelectionMode = SelectionMode.MultiExtended;
}

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Refresh();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
StringFormat sf = new StringFormat();
sf.LineAlignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), (string)comboBoxVertical.SelectedItem);
sf.Alignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), (string)comboBoxHorizontal.SelectedItem);
FontStyle fs = 0;
foreach (FontStyle tfs in listBoxFontStyle.SelectedItems)
{
fs |= tfs;
}
Font font=new Font((comboBoxFont.SelectedItem as FontFamily).Name, trackBarFontSize.Value,fs);
RectangleF rectangleF = new RectangleF(e.Graphics.ClipBounds.X+50, e.Graphics.ClipBounds.Y+50, e.Graphics.ClipBounds.Width-50, e.Graphics.ClipBounds.Height-50);

//まずは黒字で書く。
if (checkBoxDefaultDrawString.Checked)
{

e.Graphics.DrawString(textBoxInputText.Text,
font,
Brushes.Black, rectangleF, sf);
}


//その上に1文字づつ書く。
if (checkBoxEachCharacter.Checked)
{
DrawEachCharacter(e.Graphics, textBoxInputText.Text, font, null, rectangleF, sf);
}
}

private void trackBarFontSize_Scroll(object sender, EventArgs e)
{
labelFontSize.Text=trackBarFontSize.Value.ToString();
pictureBox1.Refresh();
}
private void DrawEachCharacter(Graphics graphics, string str, Font font, Brush brush, RectangleF rectangleF, StringFormat stringFormat)
{
Brush[] brushes = { Brushes.Red, Brushes.Green, Brushes.Blue };

//文字補正のため、左寄せ上寄せのテキストで1文字目を表示する位置を取得する。
Rectangle r_firstCharacter;
{
CharacterRange[] cr_firstCharacter = { new CharacterRange(0, 1) };
StringFormat sf_firstCharacter = new StringFormat(stringFormat);
sf_firstCharacter.LineAlignment = StringAlignment.Near;
sf_firstCharacter.Alignment = StringAlignment.Near;
sf_firstCharacter.SetMeasurableCharacterRanges(cr_firstCharacter);
Region[] mcr_firstCharacter = graphics.MeasureCharacterRanges(str, font, rectangleF, sf_firstCharacter);
r_firstCharacter = Rectangle.Round(mcr_firstCharacter[0].GetBounds(graphics));
}


for (int i = 0; i < str.Length; i++)
{
CharacterRange[] cr = { new CharacterRange(i, 1) };
StringFormat tsf = new StringFormat(stringFormat);
tsf.SetMeasurableCharacterRanges(cr);
Region[] mcr = graphics.MeasureCharacterRanges(str, font, rectangleF, tsf);
Rectangle r = Rectangle.Round(mcr[0].GetBounds(graphics));
if (r.Left == 0 && r.Top == 0 && i != 0)
{
//1文字目でもないのに、(0,0)に文字を描画しようとするのは、
//おかしい。ただし、制御文字等で、(0,0)の描画はあり得るので
//ループを続ける。
continue;
}
Point WritePoint = new Point(r.Left - (r_firstCharacter.X - Convert.ToInt32(rectangleF.X)), r.Top - (r_firstCharacter.Top - Convert.ToInt32(rectangleF.Top)));
graphics.DrawString(textBoxInputText.Text.Substring(i, 1), font,
brush ?? brushes[i % brushes.Length], WritePoint);

}

}

}
}
Form1.csがここまで。

補正もこのぐらいが限界かなぁ。
アンダーラインとか、打ち消し線とか文字をかぶる物は、1文字ごとにやると難しい。




ちなみに
Form1.Designer.csがここから。
namespace WindowsFormsApplication8
{
partial class NewYearCardFontExplorer
{
///
/// 必要なデザイナ変数です。
///

private System.ComponentModel.IContainer components = null;

///
/// 使用中のリソースをすべてクリーンアップします。
///

/// マネージ リソースが破棄される場合 true、破棄されない場合は false です。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows フォーム デザイナで生成されたコード

///
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///

private void InitializeComponent()
{
this.comboBoxFont = new System.Windows.Forms.ComboBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.comboBoxHorizontal = new System.Windows.Forms.ComboBox();
this.label4 = new System.Windows.Forms.Label();
this.comboBoxVertical = new System.Windows.Forms.ComboBox();
this.label5 = new System.Windows.Forms.Label();
this.trackBarFontSize = new System.Windows.Forms.TrackBar();
this.labelFontSize = new System.Windows.Forms.Label();
this.textBoxInputText = new System.Windows.Forms.TextBox();
this.checkBoxDefaultDrawString = new System.Windows.Forms.CheckBox();
this.checkBoxEachCharacter = new System.Windows.Forms.CheckBox();
this.listBoxFontStyle = new System.Windows.Forms.ListBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackBarFontSize)).BeginInit();
this.SuspendLayout();
//
// comboBoxFont
//
this.comboBoxFont.FormattingEnabled = true;
this.comboBoxFont.Location = new System.Drawing.Point(98, 12);
this.comboBoxFont.Name = "comboBoxFont";
this.comboBoxFont.Size = new System.Drawing.Size(121, 20);
this.comboBoxFont.TabIndex = 0;
this.comboBoxFont.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(14, 220);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(497, 196);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(50, 12);
this.label1.TabIndex = 2;
this.label1.Text = "フォント名";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 53);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(67, 12);
this.label2.TabIndex = 2;
this.label2.Text = "フォントサイズ";
//
// comboBoxHorizontal
//
this.comboBoxHorizontal.FormattingEnabled = true;
this.comboBoxHorizontal.Location = new System.Drawing.Point(287, 13);
this.comboBoxHorizontal.Name = "comboBoxHorizontal";
this.comboBoxHorizontal.Size = new System.Drawing.Size(81, 20);
this.comboBoxHorizontal.TabIndex = 0;
this.comboBoxHorizontal.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(231, 17);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 2;
this.label4.Text = "水平方向";
//
// comboBoxVertical
//
this.comboBoxVertical.FormattingEnabled = true;
this.comboBoxVertical.Location = new System.Drawing.Point(430, 11);
this.comboBoxVertical.Name = "comboBoxVertical";
this.comboBoxVertical.Size = new System.Drawing.Size(81, 20);
this.comboBoxVertical.TabIndex = 0;
this.comboBoxVertical.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(374, 15);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(53, 12);
this.label5.TabIndex = 2;
this.label5.Text = "垂直方向";
//
// trackBarFontSize
//
this.trackBarFontSize.Location = new System.Drawing.Point(98, 53);
this.trackBarFontSize.Maximum = 100;
this.trackBarFontSize.Minimum = 1;
this.trackBarFontSize.Name = "trackBarFontSize";
this.trackBarFontSize.Size = new System.Drawing.Size(367, 42);
this.trackBarFontSize.TabIndex = 4;
this.trackBarFontSize.Value = 1;
this.trackBarFontSize.Scroll += new System.EventHandler(this.trackBarFontSize_Scroll);
//
// labelFontSize
//
this.labelFontSize.AutoSize = true;
this.labelFontSize.Location = new System.Drawing.Point(476, 53);
this.labelFontSize.Name = "labelFontSize";
this.labelFontSize.Size = new System.Drawing.Size(0, 12);
this.labelFontSize.TabIndex = 5;
//
// textBoxInputText
//
this.textBoxInputText.Location = new System.Drawing.Point(14, 90);
this.textBoxInputText.Multiline = true;
this.textBoxInputText.Name = "textBoxInputText";
this.textBoxInputText.Size = new System.Drawing.Size(451, 113);
this.textBoxInputText.TabIndex = 6;
this.textBoxInputText.TextChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// checkBoxDefaultDrawString
//
this.checkBoxDefaultDrawString.AutoSize = true;
this.checkBoxDefaultDrawString.Location = new System.Drawing.Point(518, 14);
this.checkBoxDefaultDrawString.Name = "checkBoxDefaultDrawString";
this.checkBoxDefaultDrawString.Size = new System.Drawing.Size(116, 16);
this.checkBoxDefaultDrawString.TabIndex = 7;
this.checkBoxDefaultDrawString.Text = "デフォルト文字表示";
this.checkBoxDefaultDrawString.UseVisualStyleBackColor = true;
this.checkBoxDefaultDrawString.CheckedChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// checkBoxEachCharacter
//
this.checkBoxEachCharacter.AutoSize = true;
this.checkBoxEachCharacter.Checked = true;
this.checkBoxEachCharacter.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBoxEachCharacter.Location = new System.Drawing.Point(518, 37);
this.checkBoxEachCharacter.Name = "checkBoxEachCharacter";
this.checkBoxEachCharacter.Size = new System.Drawing.Size(96, 16);
this.checkBoxEachCharacter.TabIndex = 8;
this.checkBoxEachCharacter.Text = "一文字毎描画";
this.checkBoxEachCharacter.UseVisualStyleBackColor = true;
this.checkBoxEachCharacter.CheckedChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// listBoxFontStyle
//
this.listBoxFontStyle.FormattingEnabled = true;
this.listBoxFontStyle.ItemHeight = 12;
this.listBoxFontStyle.Location = new System.Drawing.Point(478, 90);
this.listBoxFontStyle.Name = "listBoxFontStyle";
this.listBoxFontStyle.Size = new System.Drawing.Size(120, 88);
this.listBoxFontStyle.TabIndex = 9;
this.listBoxFontStyle.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// NewYearCardFontExplorer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(636, 428);
this.Controls.Add(this.listBoxFontStyle);
this.Controls.Add(this.checkBoxEachCharacter);
this.Controls.Add(this.checkBoxDefaultDrawString);
this.Controls.Add(this.textBoxInputText);
this.Controls.Add(this.labelFontSize);
this.Controls.Add(this.trackBarFontSize);
this.Controls.Add(this.label2);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.comboBoxVertical);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBoxHorizontal);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.comboBoxFont);
this.Name = "NewYearCardFontExplorer";
this.Text = "NewYearCardFontExplorer";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackBarFontSize)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.ComboBox comboBoxFont;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox comboBoxHorizontal;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ComboBox comboBoxVertical;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TrackBar trackBarFontSize;
private System.Windows.Forms.Label labelFontSize;
private System.Windows.Forms.TextBox textBoxInputText;
private System.Windows.Forms.CheckBox checkBoxDefaultDrawString;
private System.Windows.Forms.CheckBox checkBoxEachCharacter;
private System.Windows.Forms.ListBox listBoxFontStyle;

}
}


Form1.Designer.csがここまで。