Form veya tüm kontrollerde dikdörtgen çizebileceğiniz bir kod örneği.
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication7 { public partial class Form1 : Form { bool isMouseDown = false; MyCoordinat coor = new MyCoordinat(); public Form1() { InitializeComponent(); this.MouseDown += Form1_MouseDown; this.MouseUp += Form1_MouseUp; this.MouseMove += Form1_MouseMove; this.Paint += Form1_Paint; } protected void Form1_Paint(object sender, PaintEventArgs e) { MyCoordinat newCoor = new MyCoordinat(); // Secim dikdortgeni asagiya dogru suruklenirse, // X2 > X1 ya da Y2 > Y1 olur. // Aksi halde tersi gecerli oldugundan, // X2 ve X1 ya da Y2 ve Y1 degerleri yer degistirilir. if (coor.X2 > coor.X1) { newCoor.X1 = coor.X1; newCoor.X2 = coor.X2; } else { newCoor.X1 = coor.X2; newCoor.X2 = coor.X1; } if (coor.Y2 > coor.Y1) { newCoor.Y1 = coor.Y1; newCoor.Y2 = coor.Y2; } else { newCoor.Y1 = coor.Y2; newCoor.Y2 = coor.Y1; } int width = newCoor.X2 - newCoor.X1; int height = newCoor.Y2 - newCoor.Y1; // Kullanilan e.Graphics; form'a aittir. // Başka bir kontrol uzerinde cizim yapilacaksa // o kontrole ait Graphics nesnesi olusturulur. // ornegin; <control>.CreateGraphics(); e.Graphics.DrawRectangle( new Pen(new SolidBrush(Color.Black)), new Rectangle(newCoor.X1, newCoor.Y1, width, height)); } protected void Form1_MouseMove(object sender, MouseEventArgs e) { if (this.isMouseDown) { coor.X2 = e.X; coor.Y2 = e.Y; this.Refresh(); } } protected void Form1_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; this.Refresh(); } protected void Form1_MouseDown(object sender, MouseEventArgs e) { isMouseDown = true; coor.X1 = e.X; coor.Y1 = e.Y; } } public class MyCoordinat { public int X1 { get; set; } public int Y1 { get; set; } public int X2 { get; set; } public int Y2 { get; set; } } }