126. KeyDown이벤트를 이용하여 원 그리기
.NET프로그래밍/WinForm 2009. 9. 2. 09:29 |
==> 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;
}
}
}
}
< 실행결과 >
--> 처음 실행 때 화면
--> "화살표 키"로 방향 이동
'.NET프로그래밍 > WinForm' 카테고리의 다른 글
128. CurveList (다른 폼(ex : 메모장)에 의해 의해 내가 마우스로 폼에 그렸던 그림 손상시키지 않기.) (0) | 2009.09.02 |
---|---|
127. 마우스다운(MouseDown) / 마우스무브(MouseMove) 이벤트(= 마우스드래그하여 선그리기) (0) | 2009.09.02 |
125. 키보드(Keyboard) 이벤트 실습 (0) | 2009.09.02 |
119. 초간단 메모장에서 "인쇄 미리 보기" 기능 구현하기 (0) | 2009.08.31 |
118. 초간단 메모장에 파일드롭(Drag & Drop) 기능 추가하기 (0) | 2009.08.31 |