LoginSignup
0
0

More than 3 years have passed since last update.

C#で消えない描画アプリを作る

Last updated at Posted at 2020-12-30

C#で描画した絵を、そのまま消さずに重ねて描画していきたい。
以下のような仕様のプログラムを作る。
・エンターキーが押されたら、マウス座標に四角形を描画する。
・一度描画した四角形は、アプリを消すまで残り続ける。

描画アプリ作成

描画については、Paintイベントを使うのが標準らしいので、ピクチャボックスのPaintイベントを使用する。
描画した絵を残すしくみは、一度描画した絵の座標を保存し、毎回これまでに描画した絵をすべて描画することで実現した。
※他に良い方法があれば教えてほしいです。

ソースコード

drawpolygon.cs
        // キーボード押下イベント
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                // 画像貼り付け
                pictureBox1.Invalidate();
            }
        }

        // マウスのY座標取得
        private int GetMousePoint()
        {
            return System.Windows.Forms.Cursor.Position.Y; ;
        }

        // Paintイベント
        private void PastePicture(object sender, PaintEventArgs e)
        {
            // 座標取得 
            int mouseY = GetMousePoint();

            Brush brush = new SolidBrush(Color.Black);
            PointF point1 = new PointF(0, mouseY - 10);
            PointF point2 = new PointF(pictureBox1.Width, mouseY - 10);
            PointF point3 = new PointF(pictureBox1.Width, mouseY);
            PointF point4 = new PointF(0, mouseY);

            PointF[] curvePoints =
            {
                 point1,
                 point2,
                 point3,
                 point4
            };

            for (int i = 0; i < list.Count; i++)
            {
                e.Graphics.FillPolygon(brush, list[i]);
            }
            // 古いcurvepointを保存する
            list.Add(curvePoints);
        }

エンターキーを押したら描画されることを確認できた!
2020-12-30_16h21_20.gif

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0