全局坐标的使用
原创
©著作权归作者所有:来自51CTO博客作者cnming的原创作品,请联系作者获取转载授权,否则将追究法律责任
全局坐标的使用
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace 全局坐标

...{

/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form

...{
private System.Windows.Forms.TextBox textBox1;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()

...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

...{
if( disposing )

...{
if (components != null)

...{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows Form Designer generated code#region Windows Form Designer generated code

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

...{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(88, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(752, 646);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove_1);
this.ResumeLayout(false);

}
#endregion


/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()

...{
Application.Run(new Form1());
}

protected override void OnPaint(PaintEventArgs e)

...{
//本程序建立X轴正向向右,Y轴正向向上,原点在屏幕中心的全局坐标系
//使Y轴朝上,需要作相对于X轴的镜像,变换矩阵为[1,0,0,-1,0,0]
Graphics g= e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
g.DrawEllipse(Pens.Black, -100, -100, 200, 200);

//相对于X轴进行镜像变换
Matrix mat=new Matrix(1, 0, 0, -1, 0, 0);
g.Transform = mat;
Rectangle rect= this.ClientRectangle;
int w = rect.Width;
int h= rect.Height;
//将原点移到屏幕中心
g.TranslateTransform(w / 2, -h / 2);

//以原点为中心,绘一个半径为100的圆
//注意:由于相对于X轴作了镜像变换,所有与上下有关的
// 绘图参数都交换了方向,如左上角变成了左下角
g.DrawEllipse(Pens.Red, -100, -100, 200, 200);

g.TranslateTransform(100, 100);
g.DrawEllipse(Pens.Green, -100, -100, 200, 200);

g.ScaleTransform(2, 2);
g.DrawEllipse(Pens.Blue, -100, -100, 200, 200);

}

private void Form1_MouseMove_1(object sender, MouseEventArgs e)

...{
//在文本框中显示当前点的坐标。屏幕坐标被转换为工作区坐标
Graphics g = CreateGraphics();
Rectangle rect= this.ClientRectangle;
int w = rect.Width;
int h= rect.Height;
textBox1.Text = " ";
textBox1.Text =e.X - w / 2 + " " + (-(e.Y - h / 2));
}

private void textBox1_TextChanged(object sender, System.EventArgs e)

...{
}

}
}
