C#でツールを作る その3-デザインの変更-

ツールっぽいデザインに変更してコントロールに対して描画します。

フォーム


Form.spritContainer.Panel1に追加するUserControl。ここに描画します。


実行結果


ソース

Hoge.cs
  • アプリケーションクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace WindowsFormsApplication1
{
    public class Hoge : IDisposable
    {
        private Device device = null;
        private Microsoft.DirectX.Direct3D.Font font = null;

        /// <summary>
        /// Direct3Dの初期化
        /// </summary>
        /// <param name="target">描画対象コントロール</param>
        public void Init( Control target )
        {
            PresentParameters pp = new PresentParameters();
            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;

            device = new Device(
                0,
                DeviceType.Hardware,
                target,
                CreateFlags.HardwareVertexProcessing,
                pp
                );

            font = new Microsoft.DirectX.Direct3D.Font(
                device,
                16, 0,
                FontWeight.Normal,
                0,
                false,
                CharacterSet.ShiftJIS,
                Precision.Default,
                FontQuality.AntiAliased,
                PitchAndFamily.DefaultPitch,
                "MS ゴシック"
                );
        }

        /// <summary>
        /// リソースの破棄
        /// </summary>
        public void Dispose()
        {
            if (font != null)
            {
                font.Dispose();
                font = null;
            }
            if (device != null)
            {
                device.Dispose();
                device = null;
            }
        }

        /// <summary>
        /// 描画
        /// </summary>
        public void Paint()
        {
            if (device == null) return;

            device.Clear(ClearFlags.Target, Color.FromArgb(0x333333), 1.0f, 0);

            device.BeginScene();

            font.DrawText(null, "hoge", 0, 0, Color.White);

            device.EndScene();

            device.Present();
        }

    }
}
Form1.cs
  • フォーム
  • panel1に描画用UserControlを追加します。
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;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1( UserControl1 main_panel )
        {
            InitializeComponent();
            this.splitContainer1.Panel1.Controls.Add(main_panel);
        }
    }
}
UserControl1.cs
  • 描画用パネル
  • フォームのサイズ変更時に画面がちらつかないよう、OnPaintBackgroundをオーバーライドして何もしない。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserControl1 : UserControl
    {
        private Hoge hoge = null;

        public UserControl1( Hoge hoge )
        {
            InitializeComponent();

            Dock = DockStyle.Fill;
            this.hoge = hoge;
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
        }

        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {
            hoge.Paint();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //サイズ変更時にちらつかないようにする
            //base.OnPaintBackground(e);
        }

    }
}
Program.cs
  • エントリポイント
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            using( Hoge hoge = new Hoge() )
            using( UserControl1 main_panel = new UserControl1( hoge ) )
            using( Form1 form = new Form1( main_panel ) )
            {
                hoge.Init( main_panel );
                Application.Run( form );
            }
        }
    }
}