Zoom support in custom control

A

Ashutosh

Hi,
I have a custom control and it displays various data. I want to provide the
feature of Zoom in and Zoom out for the custom control.

Is it possible to do this without modifying all the existing drawing code
and draw according to the zoom level.

I tried to change the scale by calling CustomControl.Scale method but it
doesn't work.

Any ideas??

Regards,
Ashu
 
I

Israel

Is it possible to do this without modifying all the existing drawing code
and draw according to the zoom level.

If you already have drawing code then it's pretty easy to just insert
a matrix transformation in your paint handler. However if your
control contains other controls then the issue is a bit more
difficult.
 
J

Jeff Johnson

I have a custom control and it displays various data. I want to provide
the
feature of Zoom in and Zoom out for the custom control.

Is it possible to do this without modifying all the existing drawing code
and draw according to the zoom level.

I tried to change the scale by calling CustomControl.Scale method but it
doesn't work.

You'll probably have to modify your drawing code a little bit. All it should
require is calling the ScaleTransform() method of your Graphics object.
 
C

Colbert Zhou [MSFT]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top