126. KeyDown이벤트를 이용하여 원 그리기
==> 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 Sample.FrmKeyDown());
}
}
}
==============================================================================================
==> FrmKeyDown.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;
namespace MyWinForms.Sample
{
public partial class FrmKeyDown : Form
{
private Point p;
public FrmKeyDown()
{
InitializeComponent();
p.X = 10; p.Y = 10;
}
// 폼이 그려질 때 발생하는 이벤트
private void FrmKeyDown_Paint(object sender, PaintEventArgs e)
{
// 원 그리기
e.Graphics.FillEllipse(Brushes.Black, p.X - 8, p.Y - 8, 16, 16);
}
// 폼에서 키보드가 타이핑 될 때
private void FrmKeyDown_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left: p.X -= 10; Invalidate(); break;
case Keys.Right: p.X += 10; Invalidate(); break;
case Keys.Up: p.Y -= 10; Invalidate(); break;
case Keys.Down: p.Y += 10; Invalidate(); break;
default:
break;
}
}
}
}
< 실행결과 >
--> 처음 실행 때 화면
--> "화살표 키"로 방향 이동