교재 623P. CurveList예제 참조(내가 마우스로 그렸던 그림이 캡쳐링되어 다시 패인팅(그려짐)) 되는 동작임. 눈으로 보기에는 폼에 내가 마우스로 그린 그림의 화면이 다른 폼에 의해 손실되지 않는 듯한 것으로 보이는 기능임.) "FrmMouseDown.cs" [디자인]에서 "폼"의 '속성' 중 이벤트 속성에서 "Paint -> FrmMouseDown_Paint"으로 설정하고 더블클릭 --> 생성된 "FrmMouseDown_Paint"이벤트핸들러 코드에 아래 "FrmMouseDown.cs"의 형광펜으로 칠해진 부분으로 코드작성.

* "ArrayList"형 객체 생성하여 좌표값(들)을 보관 / "foreach"문을 사용하여 저장된 좌표값 다시 그리기





==> Program.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MyWinForms
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Mouse.FrmMouseDown());
        }
    }
}



==============================================================================================




==> FrmMouseDown.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.Collections;

namespace MyWinForms.Mouse
{
    public partial class FrmMouseDown : Form
    {
        private ArrayList al;
        public FrmMouseDown()
        {
            InitializeComponent();
            al = new ArrayList();
        }

        private void FrmMouseDown_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button== MouseButtons.Right)
            {
                // 그래픽 객체 생성
                Graphics g = CreateGraphics();
                // 원 그리기
                g.DrawEllipse(Pens.Red, e.X, e.Y, 10, 10);
                // 객체 해제
                g.Dispose();
            }
            p.X = e.X; p.Y = e.Y;
        }

        private Point p; // 좌표
        private void FrmMouseDown_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Graphics g = this.CreateGraphics();
                g.DrawLine(Pens.Blue, p.X, p.Y, e.X, e.Y);

                // 좌표값 보관
                al.Add(Rectangle.FromLTRB(p.X, p.Y, e.X, e.Y));

                p.X = e.X; p.Y = e.Y; // 재설정
                g.Dispose();
            }
        }

        private void FrmMouseDown_Paint(object sender, PaintEventArgs e)
        {
            // 저장된 좌표값 다시 그리기
            foreach (Rectangle r in al)
            {
                e.Graphics.DrawLine(Pens.Blue, r.Left, r.Top, r.Right, r.Bottom);
            }

        }
    }
}





< 실행결과 >

--> 마우스로 '폼'에 그림(선)을 그림 





--> "메모장"을 내가 그림을 그린 '폼' 위로 올려놓음.




--> 내가 그림을 그린 '폼'위의 "메모장" 치우면 그림이 손상되지 않고 원래 상태대로 보여진다.(좌표값(들)을 저장하였다가(캡쳐링같은 역할) 저장된 좌표값을 다시 그리는(Paint) 동작을 하여, 우리가 눈으로 보기에는 '폼'에 그린 그림이 손실되지 않고 그대로 있는 것으로 보이는 것이다. ) 




Posted by holland14
: