Việc học C# là khá lâu dài và vất vả, đòi hỏi nỗ lực của mỗi người. Đôi lúc ta nên tạo một sự khác biệt nào đó. Bài viết này sẽ giúp bạn tạo ra một Form hình trái tim một cách rất đơn giản bằng ngôn ngữ C# – thay cho những Form hình chữ nhật mặc định.
- Đầu tiên bạn tạo một project windows form application với ngôn ngữ C# như bình thường.
- Click đúp vào Form để chương trình vào thẳng sự kiện Form_Load.
- Viết code cho sự kiện Form_Load:
+ Kiểu 1:
private void Form1_Load(object sender, EventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(70, 10, 150, 150, 135, 200);
path.AddArc(200, 10, 150, 150, 210, 195);
path.AddLine(92, 139, 210, 270);
path.AddLine(327, 139, 210, 270);
path.AddLine(327, 139, 92, 139);
this.Region = new Region(path);
}
+ Kiểu 2:
private void Form1_Load(object sender, EventArgs e)
{
using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath())
{
path.AddBezier(this.Width >> 1,
this.Height >> 2,
this.Width * 1.25f, 0f,
this.Width,
this.Height * 0.75f,
this.Width >> 1,
this.Height);
path.AddBezier(this.Width >> 1,
this.Height >> 2,
-this.Width * .25f, 0f,
0f,
this.Height * 0.75f,
this.Width >> 1,
this.Height);
this.Region = new Region(path);
}
}
- Còn chờ gì nữa F5 thôi!
Kiểu 1 Kiểu 2
- (Tham khảo code project.com)