header
CodePanic! > C#.NET Tips > 今ここ

■文字列を矩形内で折り返して描画する

PictureBox内に文字列を折り返して描画する例です。
gra_stringrect.jpg(39751 byte)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.White;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                using (Font font = new Font("MS ゴシック", 24))
                {
                	// PictureBoxのクライアント領域いっぱいを矩形とします
                    RectangleF rect = new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height);

                    g.DrawString("012345678901234567890123456789", font, Brushes.Black, rect);
                }
            }
        }
    }
}





Copyright © 2008.07 - shougo suzaki