﻿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 Example_512
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        Rectangle circleArea;
        int circlePosition = 0;

        private void SetCircleArea()
        {
            circleArea = new Rectangle((this.Width - 50) / 2, (this.Height - 50) / 2, 24, 24);
        }

        private void DrawCircle(PaintEventArgs e)
        {
            e.Graphics.FillEllipse(Brushes.Red, circleArea);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Here we inform that the form must handle key events before passing them to other child controls of the form
            this.KeyPreview = true;
            SetCircleArea();
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            Refresh();
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                circleArea.Width = 24;
                circleArea.Height = 24;
                this.Text = "x=" + circleArea.X + " y=" + circleArea.Y + " width=" + circleArea.Width + " height=" + circleArea.Height;
            }
            Refresh();
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            DrawCircle(e);
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {

                case Keys.Left:
                    circlePosition++;
                    circleArea.X -= circlePosition;
                    break;

                case Keys.Up:
                    circlePosition++;
                    circleArea.Y -= circlePosition;
                    break;

                case Keys.Right:
                    circlePosition++;
                    circleArea.X += circlePosition;
                    break;

                case Keys.Down:
                    circlePosition++;
                    circleArea.Y += circlePosition;
                    break;
            }

            this.Text = "x=" + circleArea.X + " y=" + circleArea.Y + " width=" + circleArea.Width + " height=" + circleArea.Height;
            Refresh();
        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                circleArea.Width = 48;
                circleArea.Height = 48;
            }

            if (e.KeyChar == 'c' || e.KeyChar == 'C')
                SetCircleArea();

            this.Text = "x=" + circleArea.X + " y=" + circleArea.Y + " width=" + circleArea.Width + " height=" + circleArea.Height;
            Refresh();
        }

        // We implement this even handler to make sure that arrow keys are considered as input keys. If we don't do this, the form, which has other child controls
        // will not react to arrow keys' events.

        private void MainForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                    e.IsInputKey = true;
                    break;
            }
        }
    }
}