Tải bản đầy đủ (.doc) (12 trang)

[WF] Bai tap lap trinh window docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (310.17 KB, 12 trang )

Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Bai_6_4
{
public class GDIPlusDemo: System.Windows.Forms .Form
{
public GDIPlusDemo()
{
Text = "GDI+ Demo 6.4";
Size = new Size(240, 250);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen = new Pen(Color.Black, 1);
int X = 40, Y = 30;
//ve elip
int w = 150, h = 150;
Rectangle rect = new Rectangle(new Point(X - 2, Y), new Size(w, h));
g.DrawEllipse(pen, rect);
int X2 = X + 30, Y2 = Y + 35;
//ve con mat trai hinh tam giac
Point pt1 = new Point(X2 , Y2);
Point pt2 = new Point(X2 + 25 , Y2);
Point pt3 = new Point(X2 + 11 , Y2 + 20);
Point[] points = { pt1, pt2, pt3 };


g.DrawPolygon(pen, points);
//ve con mat phai hinh tam giac
X2 = X2 + 60;
pt1 = new Point(X2 , Y2);
pt2 = new Point(X2 + 25 , Y2);
pt3 = new Point(X2 + 11 , Y2 + 20);
points = new Point [] { pt1, pt2, pt3 };
g.DrawPolygon(pen, points);
//ve chiec mui
X2 = X + 60; Y2 = Y + 70;
pt1 = new Point(X2 + 10 , Y2);
pt2 = new Point(X2 + 20 , Y2 + 20);
pt3 = new Point(X2 , Y2 + 20);
1
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
points = new Point[] { pt1, pt2, pt3 };
g.DrawPolygon(pen, points);
X2 = X + 52; Y2 = Y + 115;
//ve mieng
g.DrawRectangle(pen, X2, Y2, 40, 20);
Y2 = Y2 + 10 ;
// ve duong ngang mieng
g.DrawLine(pen, new Point ( X2, Y2), new Point ( X2 + 40, Y2));
// ve 7 duong xuong o mieng
X2 += 40 / 8; Y2 = Y2 - 10;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));
X2 += 40 / 8;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));
X2 += 40 / 8;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));

X2 += 40 / 8;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));
X2 += 40 / 8;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));
X2 += 40 / 8;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));
X2 += 40 / 8;
g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 20));
// ve hinh chu nhat o tren dau
rect = new Rectangle(new Point(X + 70, Y - 15), new Size(10, 15));
g.DrawRectangle(pen, rect);
}
static void Main()
{
Application.Run(new GDIPlusDemo ());
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
2
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
using System.Windows.Forms;
namespace Bai_6._v2
{
public partial class Form1 : Form
{
public Form1()

{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
int X = 150, Y = 220;
Graphics g = e.Graphics;
Font ftd = new Font("Arial", 10, FontStyle.Regular);
StringFormat sf = new StringFormat();
float[] ts = { 100, 100, 100, 100 };
sf.SetTabStops(0, ts);
string Toa_do = "0\t100\t200\t300\t400\t500";
g.DrawString(Toa_do, ftd, Brushes.Blue, X, Y + 10, sf);
int w = 4;
Pen pen = new Pen(Color.Blue, 4);
Rectangle rect = new Rectangle(new Point(X, Y),new Size (w,w));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 100, Y), new Size(w, w));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 200, Y), new Size(w, w));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 300, Y), new Size(w, w));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 400, Y), new Size(w, w));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 500, Y), new Size(w, w));
g.DrawRectangle(pen, rect);
pen = new Pen(Color.Blue, 2);
g.DrawLine (pen, new Point (X , Y + 2), new Point (X + 500, Y + 2));

}
private void btnVe_Click(object sender, EventArgs e)
{
if (txtL1.Text != "")
{
try
{
uint kt = uint.Parse(txtL1.Text);
if (kt > 500)
throw new Exception("Nằm trong khoảng 0 - 500!");
}
catch (FormatException)
{
MessageBox.Show("Nhập số! Ohke?", "Thông báo");
txtL1.Clear();
return;
}
catch (OverflowException)
{
MessageBox.Show("Số dương! OKe?", "Thông báo");
txtL1.Clear();
3
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
txtL1.Clear();
return;

}
}
if (txtL2.Text != "")
{
try
{
uint kt = uint.Parse(txtL2.Text);
if (kt > 500)
throw new Exception("Nằm trong khoảng 0 - 500!");
}
catch (FormatException)
{
MessageBox.Show("Nhập số! Ohke?", "Thông báo");
txtL2.Clear();
return;
}
catch (OverflowException)
{
MessageBox.Show("Số dương! OKe?", "Thông báo");
txtL2.Clear();
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
txtL2.Clear();
return;
}
}
if (txtL3.Text != "")

{
try
{
uint kt = uint.Parse(txtL3.Text);
if (kt > 500)
throw new Exception("Nằm trong khoảng 0 - 500!");
}
catch (FormatException)
{
MessageBox.Show("Nhập số! Ohke?", "Thông báo");
txtL3.Clear();
return;
}
catch (OverflowException)
{
MessageBox.Show("Số dương! OKe?", "Thông báo");
txtL3.Clear();
return;
}
catch (Exception ex)
{
4
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
MessageBox.Show(ex.Message);
txtL3.Clear();
return;
}
}
if ((txtL1.Text != "") && (txtL2.Text != "") && (txtL3.Text != ""))
{

int X = 150, Y = 95;
int max = int.Parse (txtL1.Text );
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue, 20);
g.DrawLine(pen, new Point(X, Y), new Point(max + X, Y));
Y += 50;
max = int.Parse(txtL2.Text);
pen = new Pen(Color.HotPink , 20);
g.DrawLine(pen, new Point(X , Y), new Point(max + X, Y));
Y += 50;
max = int.Parse(txtL3.Text);
pen = new Pen(Color.Green , 20);
g.DrawLine(pen, new Point(X, Y), new Point(max + X, Y));
}
else
{
MessageBox.Show("Chưa nhập đầy đủ thông tin! Oke?","Thông báo");
return;
}
}
private void btnXoa_Click(object sender, EventArgs e)
{
Invalidate();
txtL1.Clear();
txtL2.Clear();
txtL3.Clear();
}
private void Form1_Load(object sender, EventArgs e)
{
btnXoa_Click(sender, e);

}
}
}
5
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Bai_6._v2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int tempX, tempY;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Font toado = new Font("Arial", 10, FontStyle.Regular);
int X = 35, Y = 150;
Pen pen = new Pen(Color.Blue, 4);
g.DrawString("200", toado, Brushes.Blue, new Point(X, Y));
g.DrawString("150", toado, Brushes.Blue, new Point(X, Y + 50));
g.DrawString("100", toado, Brushes.Blue, new Point(X, Y + 100));
g.DrawString(" 50", toado, Brushes.Blue, new Point(X, Y + 150));

g.DrawString(" 0", toado, Brushes.Blue, new Point(X, Y + 200));
Rectangle rect = new Rectangle(new Point(X + 30, Y), new Size(4, 4));
g.DrawRectangle(pen, rect );
rect = new Rectangle(new Point(X + 30, Y + 50), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 30, Y + 100), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 30, Y + 150), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 30, Y + 200), new Size(4, 4));
g.DrawRectangle(pen, rect);
6
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
Y += 220; X += 20;
g.DrawString(" 0", toado, Brushes.Blue, new Point(X, Y));
g.DrawString(" 50", toado, Brushes.Blue, new Point(X + 50, Y));
g.DrawString("100", toado, Brushes.Blue, new Point(X + 100, Y));
g.DrawString("150", toado, Brushes.Blue, new Point(X + 150, Y));
g.DrawString("200", toado, Brushes.Blue, new Point(X + 200, Y));
Y -= 20; X += 30 -20;
rect = new Rectangle(new Point(X, Y), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 50, Y), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 100, Y), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 150, Y), new Size(4, 4));
g.DrawRectangle(pen, rect);
rect = new Rectangle(new Point(X + 200, Y), new Size(4, 4));
g.DrawRectangle(pen, rect);

pen = new Pen (Color.Red , 2);
g.DrawLine(pen, new Point(X+2, 140), new Point(X+1, Y));
g.DrawLine(pen, new Point(X, Y+2), new Point(X + 200 +20, Y+2));
tempX = X;
tempY = Y;
}
private void btnVe_Click(object sender, EventArgs e)
{
if (txtY.Text != "")
{
try
{
uint kt = uint.Parse(txtX.Text);
if (kt > 200)
throw new Exception("Nằm trong khoảng 0 - 200!");
}
catch (FormatException)
{
MessageBox.Show("Nhập số! Ohke?", "Thông báo");
txtX.Clear();
return;
}
catch (OverflowException)
{
MessageBox.Show("Số dương! OKe?", "Thông báo");
txtX.Clear();
return;
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
txtX.Clear();
return;
}
}
if (txtY.Text != "")
{
try
{
uint kt = uint.Parse(txtY.Text);
if (kt > 200)
7
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
throw new Exception("Nằm trong khoảng 0 - 200!");
}
catch (FormatException)
{
MessageBox.Show("Nhập số! Ohke?", "Thông báo");
txtY.Clear();
return;
}
catch (OverflowException)
{
MessageBox.Show("Số dương! OKe?", "Thông báo");
txtY.Clear();
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

txtY.Clear();
return;
}
}
if (txtR.Text != "")
{
try
{
uint kt = uint.Parse(txtR.Text);
if (kt > 100)
throw new Exception("Ban kinh Qua lon!");
}
catch (FormatException)
{
MessageBox.Show("Nhập số! Ohke?", "Thông báo");
txtR.Clear();
return;
}
catch (OverflowException)
{
MessageBox.Show("Số dương! OKe?", "Thông báo");
txtR.Clear();
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
txtR.Clear();
return;
}

}
if ((txtX.Text != "") && (txtY.Text != "") && (txtR.Text != ""))
{
int X = int.Parse(txtX.Text), Y = int.Parse(txtY.Text), R =
int.Parse(txtR.Text);
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue, 2);
g.DrawEllipse(pen, tempX + X - R, tempY - Y -R, R*2, R*2);
}
else
{
8
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
MessageBox.Show("Chưa nhập đầy đủ thông tin! Oke?", "Thông báo");
return;
}
}
private void btnXoa_Click(object sender, EventArgs e)
{
Invalidate();
txtR.Clear();
txtX.Clear();
txtY.Clear();
}
}
}
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;

using System.Windows.Forms;
namespace bai6_7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0; string chuoi=null;
private ArrayList MyLL = new ArrayList();
private ArrayList MyLL1 = new ArrayList();
private ArrayList MyLL2 = new ArrayList();
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
i = i + 1; chuoi = i.ToString();
Pen pen = new Pen(Color.Blue, 2);
Graphics g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias;
if (mnuvua.Checked == true)
{
g.DrawRectangle(pen, e.X - 20, e.Y - 20, 40, 40);
MyLL.Add(new Point(e.X, e.Y));
9
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
g.DrawString(chuoi, Font, Brushes.DarkBlue, e.X-5, e.Y-5);
}
else
{
g.DrawRectangle(pen, e.X - 15, e.Y - 15, 30, 30);

MyLL1.Add(new Point(e.X, e.Y));
g.DrawString(chuoi, Font, Brushes.DarkBlue, e.X-5, e.Y-5);
}
MyLL2.Add(new Point(e.X, e.Y));
}
private void mnunho_Click(object sender, EventArgs e)
{
mnunho.Checked = true;
mnuvua.Checked = false;
}
private void mnuvua_Click(object sender, EventArgs e)
{
mnuvua.Checked = true;
mnunho.Checked = false;
}
private void Form1_Load(object sender, EventArgs e)
{
mnunho.Checked = true;
}
private void Form1_MinimumSizeChanged(object sender, EventArgs e)
{
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g=e.Graphics;
Pen pen = new Pen(Color.Blue, 2);
foreach (Point pt in MyLL)
{
g.DrawRectangle(pen, pt.X - 20, pt.Y - 20, 40, 40);

}
foreach (Point pt1 in MyLL1)
{
g.DrawRectangle(pen, pt1.X - 15, pt1.Y - 15, 30, 30);
}
int i = 0; string chuoi;
foreach (Point pt2 in MyLL2)
{
i = i + 1;
chuoi = i.ToString();
g.DrawString(chuoi, Font, Brushes.DarkBlue, pt2.X - 5, pt2.Y - 5);
}
}
private void mnuxoa_Click(object sender, EventArgs e)
{
MyLL.Clear();
MyLL1.Clear();
MyLL2.Clear();
i = 0;
mnunho_Click(sender, e);
Invalidate();
}
}
10
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
}
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;

using System.Windows.Forms;
namespace bai6_7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0;
string chuoi=null;
private ArrayList MyLL = new ArrayList();
private ArrayList MyLL1 = new ArrayList();
private ArrayList MyLL2 = new ArrayList();
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
i = i + 1;
chuoi = i.ToString();
Graphics g;
Pen pen = new Pen(Color.Blue, 2);
g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias ;
if (menuNet_lientuc.Checked == true)
{
pen.DashStyle = DashStyle.Solid;
Rectangle rect = new Rectangle(new Point(e.X, e.Y), new Size(40, 40));
g.DrawEllipse(pen, rect);
MyLL.Add(new Point(e.X, e.Y));
g.DrawString(chuoi, Font, Brushes.DarkBlue, e.X+15, e.Y+15);
}

else
{
pen.DashStyle = DashStyle.Dash;
Rectangle rect = new Rectangle(new Point(e.X, e.Y), new Size(40, 40));
g.DrawEllipse(pen, rect);
MyLL1.Add(new Point(e.X, e.Y));
11
Bài tập lập trình Windows Form C# - Visual Studio .NET 2005
g.DrawString(chuoi, Font, Brushes.DarkBlue, e.X+15, e.Y+15);
}
MyLL2.Add(new Point (e.X, e.Y) );
}
private void mnunho_Click(object sender, EventArgs e)
{
menuNet_lientuc.Checked = true;
menuNet_gachgach.Checked = false;
}
private void mnuvua_Click(object sender, EventArgs e)
{
menuNet_gachgach.Checked = true;
menuNet_lientuc.Checked = false;
}
private void Form1_Load(object sender, EventArgs e)
{
menuNet_lientuc.Checked = true;
}
private void Form1_MinimumSizeChanged(object sender, EventArgs e)
{
Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g=e.Graphics;
Pen pen = new Pen(Color.Blue, 2);
foreach (Point pt in MyLL)
{
pen.DashStyle = DashStyle.Solid;
Rectangle rect = new Rectangle(new Point (pt.X , pt.Y), new Size(40, 40));
g.DrawEllipse(pen, rect);
}
foreach (Point pt1 in MyLL1)
{
pen.DashStyle = DashStyle.Dash;
Rectangle rect = new Rectangle(new Point(pt1.X, pt1.Y), new Size(40, 40));
g.DrawEllipse(pen, rect);
}
int i = 0; string chuoi;
foreach (Point pt2 in MyLL2)
{
i = i + 1;
chuoi = i.ToString();
g.DrawString(chuoi , Font, Brushes.DarkBlue, pt2.X + 15, pt2.Y + 15);
}
}
private void mnuxoa_Click(object sender, EventArgs e)
{
MyLL.Clear();
MyLL1.Clear();
MyLL2.Clear();
i = 0;

Invalidate();
}
}
}
12

×