Kare Şekli Oluşturmak
Bu örnekte mouse un sol tuşuna tıklayarak form da istenildiği kadar kare şekli oluşturup, oluşturduğumuz kare sayısını label de göstereceğiz. Aşağıdaki şekli inceleyin.
Şekil 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
List< Rectangle > squares = new List< Rectangle >();
public Form1()
{
InitializeComponent();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(299, 272);
this.Controls.Add(this.label1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "kare";
this.Text = "kare";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.kare_Paint);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.kare_MouseDown);
this.ResumeLayout(false);
}
private void kare_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red, 10);
foreach (Rectangle kare in squares)
{
e.Graphics.DrawRectangle(pen, kare);
}
pen.Dispose();
label1.Text = " " + squares.Count.ToString() + " kare";
}
private void kare_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Rectangle square = new Rectangle(e.X, e.Y, 20, 20);
squares.Add(square);
square.Inflate(1, 1);
Invalidate(square);
}
}
}
}
//Bir sonraki makalede buluşmak üzere. Bahadır ŞAHİN