忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【2024年04月29日05:27 】 |
C#3D立方体ワイヤーフレーム 終(第4回) for VS2013 Express
C#3D立方体ワイヤーフレーム 終(第4回) for VS2013 Express



CRubic.cs  → C#3D立方体ワイヤーフレーム マウスの座標渡し(第3回) for VS2013 Express
Form1.cs       ← 今回

今回は、作成したクラスを使用します。
プロジェクト名は変更なしでそのままで
ボタンと、picturebox1とtextbox1、textbox2を追加
してペタペタ貼り付けて下さい。





Form1 クラス


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        CRubic m_pRubic;// = new CRubic();
        bool nFlags = false;
        public Form1()
        {
            //int scale = int.Parse(textBox2.Text);
            m_pRubic = new CRubic(600);
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ondraw();
        }
        void ondraw()
        {
            //描画先とするImageオブジェクトを作成する
            Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            //ImageオブジェクトのGraphicsオブジェクトを作成する
            Graphics gra = Graphics.FromImage(canvas);
            //Penオブジェクトの作成(幅1の黒色)
            //(この場合はPenを作成せずに、Pens.Blackを使っても良い)
            Pen pen = new Pen(Color.Blue, 1);
            //立方体
            int scale = int.Parse(textBox2.Text);
            m_pRubic.DrawRubic(gra, pen, scale);
            pen.Dispose();
            gra.Dispose();
            //PictureBox1に表示する
            pictureBox1.Image = canvas;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Point point = new Point();
            point.X = e.X;
            point.Y = e.Y;
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                nFlags = true;
                m_pRubic.SetClickPoint(point);
            }
            else if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                nFlags = true;
                m_pRubic.SetClickPoint(point);
            }
            ondraw();
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Point point = new Point();
            point.X = e.X;
            point.Y = e.Y;
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                nFlags = false;
            }
            else if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                nFlags = false;
            }
            ondraw();
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Point point = new Point();
            point.X = e.X;
            point.Y = e.Y;
            
            if (nFlags && (e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                m_pRubic.TurnViewPoint( point );
                ondraw();
            }
            else if (nFlags && (e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                m_pRubic.MoveCubes(point);
                ondraw();
            }
        }
    }
}




        void ondraw()
        {
            //描画先とするImageオブジェクトを作成する
            Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            //ImageオブジェクトのGraphicsオブジェクトを作成する
            Graphics gra = Graphics.FromImage(canvas);
            //Penオブジェクトの作成(幅1の黒色)
            //(この場合はPenを作成せずに、Pens.Blackを使っても良い)
            Pen pen = new Pen(Color.Blue, 1);
            //立方体
            int scale = int.Parse(textBox2.Text);
            m_pRubic.DrawRubic(gra, pen, scale);
            pen.Dispose();
            gra.Dispose();
            //PictureBox1に表示する
            pictureBox1.Image = canvas;
        }

で使用したのをそのまま使用。

            //立方体
            int scale = int.Parse(textBox2.Text);
            m_pRubic.DrawRubic(gra, pen, scale);


この部分だけを差し替えただけです。
pictureBox1上でマウスがクリックされたとき以下の関数がよばれます。

pictureBox1_MouseDown   ・・・マウスがクリックされたとき
pictureBox1_MouseUp    ・・・マウスがクリックから離されたとき
pictureBox1_MouseMove   ・・・マウスをクリックしたまま動したとき


イベントといいます。
他にもいろいろイベントがありますが、ここではマウスのイベントを拾っています。




エディタ上で、右クリックしてプロパティをクリックすると
以下のような画面が表示されます。雷マークみたいをクリックします。
これが、このpictureBox1で拾えるイベントとなります。

pictureBox1_MouseDown   ・・・マウスがクリックされたとき
pictureBox1_MouseUp    ・・・マウスがクリックから離されたとき
pictureBox1_MouseMove   ・・・マウスをクリックしたまま動したとき

のそれぞれをWクリックすると、ソースに追加されます。
それを編集することになります。





  private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Point point = new Point();
            point.X = e.X;
            point.Y = e.Y;
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                nFlags = true;
                m_pRubic.SetClickPoint(point);
            }
            else if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                nFlags = true;
                m_pRubic.SetClickPoint(point);
            }
            ondraw();
        }

左マウスがクリックされた時、m_pRubic.SetClickPoint( point );が処理される。
左マウスが離された時、nFlags true;になる。
右マウスがクリックされた時、m_pRubic.SetClickPoint( point );が処理される。
右マウスが離された時、nFlags がtrueになる。


        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Point point = new Point();
            point.X = e.X;
            point.Y = e.Y;
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                nFlags = false;
            }
            else if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                nFlags = false;
            }
            ondraw();
        }


左マウスが離された時、nFlags false;になる。
右マウスが離された時、nFlagsfalse;になる。

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Point point = new Point();
            point.X = e.X;
            point.Y = e.Y;
            
            if (nFlags && (e.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                m_pRubic.TurnViewPoint( point );
                ondraw();
            }
            else if (nFlags && (e.Button & MouseButtons.Right) == MouseButtons.Right)
            {
                m_pRubic.MoveCubes(point);
                ondraw();
            }
        }


左マウスがクリックされた状態で動いた時、m_pRubic.TurnViewPoint( point );が処理される。
右マウスがクリックされた状態で動いた時、m_pRubic.MoveCubes( point );が処理される。


このイベントという概念は、C#だけのものではなく他の言語にもあります。
Windowsだけでなく、Linuxにもあり、似たり寄ったりなので
こういうもんだと思って頂ければよいかと思います。




終わり。


PR
【2016年09月11日19:57 】 | グラフィック | コメント(0)
| ホーム | 次ページ

忍者ブログ [PR]