How can I keep a graphics object centered (like a circle) when zooming in or out on it?
- by sonny5
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace testgrfx
{
public class Form1 : System.Windows.Forms.Form
{
float m_Scalef;
float m_Scalefout;
Rectangle m_r1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.ComponentModel.Container components = null;
private void InitializeComponent()
{
m_Scalef = 1.0f; // for zooming purposes
Console.WriteLine("opening m_Scalef= {0}",m_Scalef);
m_Scalefout = 1.0f;
Console.WriteLine("opening m_Scalefout= {0}",m_Scalefout);
m_r1 = new Rectangle(50,50,100,100);
this.AutoScrollMinSize = new Size(600,700);
this.components = new System.ComponentModel.Container();
this.button2 = new System.Windows.Forms.Button();
this.button2.BackColor = System.Drawing.Color.LightGray;
this.button2.Location = new System.Drawing.Point(120, 30);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 1;
this.button2.Text = "Zoom In";
this.button2.Click += new System.EventHandler(this.mnuZoomin_Click);
this.Controls.Add(button2);
this.button3 = new System.Windows.Forms.Button();
this.button3.BackColor = System.Drawing.Color.LightGray;
this.button3.Location = new System.Drawing.Point(200, 30);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(72, 24);
this.button3.TabIndex = 2;
this.button3.Text = "Zoom Out";
this.button3.Click += new System.EventHandler(this.mnuZoomout_Click);
this.Controls.Add(button3);
//InitMyForm();
}
public Form1()
{
InitializeComponent();
Text = " DrawLine";
BackColor = SystemColors.Window;
// Gotta load these kind at start-up ... not with button assignments
this.Paint+=new PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
/// Clean up any resources being used.
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// autoscroll2.cs does work
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics dc = e.Graphics;
dc.PageUnit = GraphicsUnit.Pixel;
dc.PageScale = m_Scalef;
Console.WriteLine("opening dc.PageScale= {0}",dc.PageScale);
dc.TranslateTransform(this.AutoScrollPosition.X/m_Scalef,
this.AutoScrollPosition.Y/m_Scalef);
Pen pn = new Pen(Color.Blue,2);
dc.DrawEllipse(pn,m_r1);
Console.WriteLine("form_paint_dc.PageUnit= {0}",dc.PageUnit);
Console.WriteLine("form_paint_dc.PageScale= {0}",dc.PageScale);
//Console.Out.NewLine = "\r\n\r\n"; // makes all double spaces
}
private void mnuZoomin_Click(object sender, System.EventArgs e)
{
m_Scalef = m_Scalef * 2.0f;
Console.WriteLine("in mnuZoomin_Click m_Scalef= {0}",m_Scalef);
Invalidate(); // to trigger Paint of entire client area
}
// try: System.Drawing.Rectangle resolution = Screen.GetWorkingArea(someForm);
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Uses the mouse wheel to scroll
Graphics dc = CreateGraphics();
dc.TranslateTransform(this.AutoScrollPosition.X/m_Scalef,
this.AutoScrollPosition.Y/m_Scalef);
Console.WriteLine("opening Form1_MouseDown dc.PageScale= {0}",
dc.PageScale);
Console.WriteLine("Y wheel= {0}", this.AutoScrollPosition.Y/m_Scalef);
dc.PageUnit = GraphicsUnit.Pixel;
dc.PageScale = m_Scalef;
Console.WriteLine("frm1_moudwn_dc.PageScale= {0}",dc.PageScale);
Point [] mousep = new Point[1];
Console.WriteLine("mousep= {0}", mousep);
// make sure to adjust mouse pos.for scroll position
Size scrollOffset = new Size(this.AutoScrollPosition);
mousep[0] = new Point(e.X-scrollOffset.Width, e.Y-scrollOffset.Height);
dc.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device,mousep);
Pen pen = new Pen(Color.Green,1);
dc.DrawRectangle(pen,m_r1);
Console.WriteLine("m_r1= {0}", m_r1);
Console.WriteLine("mousep[0].X= {0}", mousep[0].X);
Console.WriteLine("mousep[0].Y= {0}", mousep[0].Y);
if (m_r1.Contains(new Rectangle(mousep[0].X, mousep[0].Y,1,1)))
MessageBox.Show("click inside rectangle");
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void mnuZoomout_Click(object sender, System.EventArgs e)
{
Console.WriteLine("first line--mnuZoomout_Click m_Scalef={0}",m_Scalef);
if(m_Scalef > 9 )
{
m_Scalef = m_Scalef / 2.0f;
Console.WriteLine("in >9 mnuZoomout_Click m_Scalef= {0}",m_Scalef);
Console.WriteLine("in >9 mnuZoomout_Click__m_Scalefout= {0}",m_Scalefout);
}
else
{
Console.WriteLine("<= 9_Zoom-out B-4 redefining={0}",m_Scalef);
m_Scalef = m_Scalef * 0.5f; // make it same as previous Zoom In
Console.WriteLine("<= 9_Zoom-out after m_Scalef= {0}",m_Scalef);
}
Invalidate();
}
} // public class Form1
}