はじめてのYaneSDK.NET番外編1
GDI+を使った文字列描画の例です。
YaneSDK.NETにもFontクラスがありますが、小さい文字を描画する場合、一度拡大してから縮小しているのでイメージ通りにならないこともあるようです。そんなときは、GDI+を使ってみるのも手です。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using YD = Yanesdk.Draw; namespace Sample7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } YD.Win32Window window; YD.GlTexture texture; private void Form1_Load(object sender, EventArgs e) { window = new YD.Win32Window(pictureBox1.Handle); window.Screen.Select(); Font font = new Font("MS 明朝", 12); texture = new StringTexture("こんにちは", font, Brushes.Blue); window.Screen.Unselect(); } private void OnTick(object sender, EventArgs e) { window.Screen.Select(); window.Screen.SetClearColor(255, 255, 255); window.Screen.Clear(); window.Screen.BlendSrcAlpha(); window.Screen.Blt(texture, 100, 100); window.Screen.Update(); } } // 文字列テクスチャ作成用のヘルパー public class StringTexture : YD.GlTexture { public StringTexture(string text, Font font, Brush brush) { SizeF size; YD.Surface surface; // 文字列の大きさを測定 using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) { size = g.MeasureString(text, font); } // ビットマップからサーフェスを作成する using (Bitmap bmp = new Bitmap( (int)size.Width, (int)size.Height)) using (Graphics g = Graphics.FromImage(bmp)) { g.DrawString(text, font, brush, 0, 0); YD.BitmapHelper.BitmapToSurface(bmp, out surface); } SetSurface(surface); } } }