0% found this document useful (0 votes)
9 views4 pages

Practice - Chapter 3- Timer

The document outlines two exercises for Windows programming using the Graphic Device Interface. The first exercise involves creating an application where users can move a circle on the screen using arrow keys and adjust its size and color with specific key presses. The second exercise focuses on developing an application where a circle moves automatically across the screen, with additional requirements for reversing direction upon reaching the screen edge and allowing user control for vertical movement.

Uploaded by

buiphat251399
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Practice - Chapter 3- Timer

The document outlines two exercises for Windows programming using the Graphic Device Interface. The first exercise involves creating an application where users can move a circle on the screen using arrow keys and adjust its size and color with specific key presses. The second exercise focuses on developing an application where a circle moves automatically across the screen, with additional requirements for reversing direction upon reaching the screen edge and allowing user control for vertical movement.

Uploaded by

buiphat251399
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WINDOWS PROGRAMMING

Graphic device interface


Dr. Le Van Vinh
FIT - HCMUTE

Exercise 1:
Develop an application having a circle on it screen. Users could use the “Left”, “Right”, “Up”,
and “Down” key to move the graphic object.

plMain (Panel)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControlingObject
{
public partial class Form1 : Form
{
Graphics gp;
SolidBrush myBrush;
Color myColor;
Point pos;

public Form1()
{
InitializeComponent();

gp = this.plMain.CreateGraphics();
myColor = Color.Blue;

myBrush = new SolidBrush(myColor);

pos.X = 100;
pos.Y = 100;

private void Form1_Load(object sender, EventArgs e)


{

private void plMain_Paint(object sender, PaintEventArgs e)


{
gp.FillEllipse(myBrush, pos.X, pos.Y, 50, 50);
this.plMain.Focus();
}

private void plMain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)


{
switch (e.KeyCode)
{
case Keys.Left:
this.pos.X = this.pos.X - 5;
break;
case Keys.Right:
this.pos.X = this.pos.X + 5;
break;
case Keys.Up:
this.pos.Y = this.pos.Y - 5;
break;
case Keys.Down:
this.pos.Y = this.pos.Y + 5;
break;
default:
break;
}
this.plMain.Refresh();
}
}
}

Requirements: Develop other functions:


+ Adjust size of the circle by pressing keys (L: larger, N: smaller).
+ Adjust color of the circel by pressing “C” key.

Exercise 2: Develop an application having a circle on it screen. The circle moves from the left to
the right of the screen automatically.
.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MovingObject
{
public partial class Form1 : Form
{
Graphics gp;
SolidBrush myBrush;
Color myColor;

Point pos;

public Form1()
{
InitializeComponent();
gp = this.plMain.CreateGraphics();
myColor = Color.Blue;

myBrush = new SolidBrush(myColor);

pos.X = 100;
pos.Y = 100;
}

private void Form1_Load(object sender, EventArgs e)


{
this.myTime.Interval = 500;
this.myTime.Start();

private void plMain_Paint(object sender, PaintEventArgs e)


{
gp.FillEllipse(myBrush, pos.X, pos.Y, 50, 50);
}

private void myTime_Tick(object sender, EventArgs e)


{
this.pos.X = this.pos.X + 10;

this.plMain.Refresh();
}
}
}

Requirements: Develop other functions allowing:


+ When the circle reaches the right edge of the screen, it automatically run backwards.
+ Users could use the key to move the circle up or down (while the circle still moves forwards or
backwards)

You might also like