Hello,
There are two parts need to be scaled, controls and GDI drawings.
1. To scale the controls, we need to loop in the customcontrol's Controls
collection and call each's Scale method.
2. To scale the GDI drawings, we need to add codes in the overrided Paint
method, and call Graphics.ScaleTransform.
Codes,
ZoomControl.cs
----------------------------------------------
public float zoomFactor;
public ZoomControl()
{
InitializeComponent();
zoomFactor = 1.0F;
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.ScaleTransform(zoomFactor, zoomFactor);
pe.Graphics.DrawString("This is a test string", this.Font,
Brushes.Black, new PointF(50.0F, 50.0F));
base.OnPaint(pe);
}
Host Form(I have two buttons to control the zoom)
---------------------------------------------
private int count = 0;
private void button1_Click(object sender, EventArgs e)
{
foreach (Control c in this.zoomControl1.Controls)
{
c.Scale(new SizeF(1.1F, 1.1F));
}
count++;
if (count == 0)
{
this.zoomControl1.zoomFactor = 1;
}
if (count > 0)
{
float rate = 1;
for (int i = 0; i < count; i++)
{
rate *= 1.1F;
}
this.zoomControl1.zoomFactor = rate;
}
if (count < 0)
{
float rate = 1;
for (int i = 0; i > count; i--)
{
rate *= 1 / 1.1F;
}
this.zoomControl1.zoomFactor = rate;
}
this.zoomControl1.Invalidate();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (Control c in this.zoomControl1.Controls)
{
c.Scale(new SizeF(1 / 1.1F, 1 / 1.1F));
}
count--;
if (count == 0)
{
this.zoomControl1.zoomFactor = 1;
}
if (count > 0)
{
float rate = 1;
for (int i = 0; i < count; i++)
{
rate *= 1.1F;
}
this.zoomControl1.zoomFactor = rate;
}
if (count < 0)
{
float rate = 1;
for (int i = 0; i > count; i--)
{
rate *= 1 / 1.1F;
}
this.zoomControl1.zoomFactor = rate;
}
this.zoomControl1.Invalidate();
}
You can get a sample project downloaded from here. This is just a skeleton,
and you need to modify it to align your scenario.
http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/ZoomApp.zip
Regards,
Colbert Zhou(
[email protected])
Microsoft Newsgroup Online Support