2008. 11. 20. 16:25

c# 도형그리기

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromHwnd(this.Handle);
            g.DrawEllipse(new Pen(Color.Red), 10, 10, 50, 50);     //원
        } 

        아니면 

        int i;
        public Form1()
        {
            InitializeComponent();
            i = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Invalidate();
            i = 1;      //사각형
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Invalidate();
            i = 2;      //원
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Red);
           switch (i)
            {
                case 1:
                    g.DrawRectangle(pen, 10, 10, 50, 50);
                    break;
                case 2:
                    g.DrawEllipse(pen, 20, 20, 50, 50);
                    break;
            }

        }