2014年7月1日火曜日

開発環境

Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(コントロールとグラフィックス: 見栄えをよくする)、エクササイズ(p.464)を解いてみる。

エクササイズ(p.464)

コード

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing.Printing;

namespace _3_Beehive_Simulator__controls_
{
    public partial class Form1 : Form
    {
        // 省略...
        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            PrintDocument document = new PrintDocument();
            bool b = false;

            if (timer1.Enabled)
            {
                timer1.Stop();
                b = true;
            }

            document.PrintPage += document_PrintPage;
            PrintPreviewDialog preview = new PrintPreviewDialog();
            preview.Document = document;
            preview.ShowDialog(this);
            if (b)
            {
                timer1.Start();
            }
        }

        void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            // 省略
            tableY = PrintTableRow(g, tableX, tableWith, firstColumnX, secondColumnX, tableY,
                "蜜蜂", Bees.Text);
            tableY = PrintTableRow(g, tableX, tableWith, firstColumnX, secondColumnX, tableY,
                "花", Flowers.Text);
            tableY = PrintTableRow(g, tableX, tableWith, firstColumnX, secondColumnX, tableY,
                "巣の中の蜂蜜", HoneyInHive.Text);
            tableY = PrintTableRow(g, tableX, tableWith, firstColumnX, secondColumnX, tableY,
                "花蜜", NectarInFlowers.Text);
            tableY = PrintTableRow(g, tableX, tableWith, firstColumnX, secondColumnX, tableY,
                "実行フレーム数", FramesRun.Text);
            tableY = PrintTableRow(g, tableX, tableWith, firstColumnX, secondColumnX, tableY,
                "フレームレート", FrameRate.Text);
            g.DrawLine(Pens.Black, tableX, e.MarginBounds.Y, tableX + tableWith, e.MarginBounds.Y);
            g.DrawLine(Pens.Black, tableX, e.MarginBounds.Y, tableX, tableY);
            g.DrawLine(Pens.Black, secondColumnX, e.MarginBounds.Y, secondColumnX, tableY);
            g.DrawLine(Pens.Black, tableX + tableWith, e.MarginBounds.Y, tableX + tableWith, tableY);

            using (Bitmap hiveBitmap = new Bitmap(hiveForm.ClientSize.Width, hiveForm.ClientSize.Height))
            {
                using (Graphics graphics = Graphics.FromImage(hiveBitmap))
                {
                    renderer.PaintHive(graphics);
                }
                g.DrawImage(hiveBitmap,
                    e.MarginBounds.X + (e.MarginBounds.Width - e.MarginBounds.Width / 2) / 2,
                    e.MarginBounds.Height / 3,
                    e.MarginBounds.Width / 2,
                    (float)e.MarginBounds.Width / 2 * hiveBitmap.Height / hiveBitmap.Width);
                using (Pen pen = new Pen(Brushes.Black, 2))
                {
                    g.DrawRectangle(pen,
                        e.MarginBounds.X + (e.MarginBounds.Width - e.MarginBounds.Width / 2) / 2,
                        e.MarginBounds.Height / 3,
                        e.MarginBounds.Width / 2,
                        (float)e.MarginBounds.Width / 2 * hiveBitmap.Height / hiveBitmap.Width);
                }
            }
            using (Bitmap fieldBitmap = new Bitmap(fieldForm.ClientSize.Width, fieldForm.ClientSize.Height))
            {
                using (Graphics graphics = Graphics.FromImage(fieldBitmap))
                {
                    renderer.PaintField(graphics);
                }
                g.DrawImage(fieldBitmap,
                    e.MarginBounds.X,
                    e.MarginBounds.Y + e.MarginBounds.Height - e.MarginBounds.Width * (float)fieldBitmap.Height / fieldBitmap.Width,
                    e.MarginBounds.Width,
                    e.MarginBounds.Width * (float)fieldBitmap.Height / fieldBitmap.Width);
                using (Pen pen = new Pen(Brushes.Black, 2))
                {
                    g.DrawRectangle(pen,
                        e.MarginBounds.X,
                        e.MarginBounds.Y + e.MarginBounds.Height - e.MarginBounds.Width * (float)fieldBitmap.Height / fieldBitmap.Width,
                        e.MarginBounds.Width,
                        e.MarginBounds.Width * (float)fieldBitmap.Height / fieldBitmap.Width);
                }
            }
        }
        // 省略...
    }
}

0 コメント:

コメントを投稿