public partial class Form1 : Form
{
Graphics myGraphics;
public Form1()
{
InitializeComponent();
this.Shown += new EventHandler(Form1_Shown);
}
void Form1_Shown(object sender, EventArgs e)
{
Button ButtonZoomIn = new Button();
ButtonZoomIn.Text = "Zoom In";
ButtonZoomIn.Location = new Point(0, 0);
Controls.Add(ButtonZoomIn);
Button ButtonZoomOut = new Button();
ButtonZoomOut.Text = "Zoom Out";
ButtonZoomOut.Location = new Point(100, 0);
Controls.Add(ButtonZoomOut);
ButtonZoomIn.Click += (s, args) =>
{
drawRectangle();
myGraphics.ScaleTransform(2.0F, 2.0F);
drawRectangle();
};
ButtonZoomOut.Click += (s, args) =>
{
drawRectangle();
myGraphics.ScaleTransform(0.5F, 0.5F);
drawRectangle();
};
myGraphics = this.CreateGraphics();
drawRectangle();
}
void drawRectangle()
{
myGraphics.Clear(this.BackColor);
Rectangle rect = new Rectangle(50, 50, 100, 100);
SolidBrush brush = new SolidBrush(Color.Black);
myGraphics.FillRectangle(brush, rect);
}
}