using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Drawing.Drawing2D;
namespace
GDITransformApp
{
public
partial
class
Transform1 : Form
{
public
class
MyMatrix
{
public
float
X
{
get
;
set
;
}
public
float
Y
{
get
;
set
;
}
public
MyMatrix(
float
x,
float
y)
{
X
=
x;
Y
=
y;
}
}
#region
Coordanate
//
坐标轴X与控件边缘的距离
private
const
int
xPad
=
80
;
//
坐标轴Y与控件边缘的距离
private
const
int
yPad
=
60
;
///
<summary>
///
X坐标标签间距
///
</summary>
private
int
coordinateLablePadX
=
20
;
public
int
CoordinateLablePadX
{
get
{
return
coordinateLablePadX;
}
set
{
Invalidate();
coordinateLablePadX
=
value;
}
}
///
<summary>
///
Y坐标标签间距
///
</summary>
private
int
coordinateLablePadY
=
30
;
public
int
CoordinateLablePadY
{
get
{
return
coordinateLablePadY;
}
set
{
Invalidate();
coordinateLablePadY
=
value;
}
}
///
<summary>
///
y坐标轴起点坐标
///
</summary>
public
Point YCoordinateStartPoint
{
get
{
return
new
Point(xPad,
this
.Height
-
yPad);
}
}
///
<summary>
///
y坐标轴终点坐标
///
</summary>
public
Point YCoordinateEndPoint
{
get
{
return
new
Point(xPad, yPad);
}
}
///
<summary>
///
x坐标轴起点坐标
///
</summary>
public
Point XCoordinateStartPoint
{
get
{
return
YCoordinateStartPoint;
}
}
///
<summary>
///
x坐标轴终点坐标
///
</summary>
public
Point XCoordinateEndPoint
{
get
{
Point current
=
new
Point();
current.X
=
Width
-
xPad;
current.Y
=
Height
-
yPad;
return
current;
}
}
private
void
DrawCoordinate(PaintEventArgs e)
{
Pen currentPen
=
new
Pen(Color.Black);
currentPen.EndCap
=
LineCap.ArrowAnchor;
//
绘制Y轴
e.Graphics.DrawLine(
currentPen,
YCoordinateStartPoint,
YCoordinateEndPoint
);
//
绘制Y轴
e.Graphics.DrawLine(
currentPen,
XCoordinateStartPoint,
XCoordinateEndPoint
);
currentPen.Dispose();
//
绘制X,Y 标签
DrawCoordinateLabel(e.Graphics, YCoordinateStartPoint, YCoordinateEndPoint, XCoordinateStartPoint, XCoordinateEndPoint);
}
///
<summary>
///
绘制坐标标记
///
</summary>
private
void
DrawCoordinateLabel(Graphics gp, Point startYPoint, Point endYPoint, Point startXPoint, Point endXPoint)
{
//
int y = YCoordinateStartPoint .Y - YCoordinateEndPoint.Y;
int
currentValue
=
-
1
;
float
labelX
=
0
, labelY
=
0
;
int
pad
=
5
;
//
绘制Y轴
for
(
int
current
=
YCoordinateStartPoint.Y; current
>
YCoordinateEndPoint.Y; current
-=
CoordinateLablePadY)
{
currentValue
++
;
SizeF textSize
=
gp.MeasureString(currentValue.ToString(), Font);
labelX
=
YCoordinateStartPoint.X
-
xPad
/
2
;
labelY
=
current
-
textSize.Height
/
2
;
//
绘制label
gp.DrawString(currentValue.ToString(), Font, Brushes.Black,
new
PointF(labelX, labelY)
);
//
绘制|-
gp.DrawLine(Pens.Black,
new
Point(YCoordinateStartPoint.X, current),
new
Point(YCoordinateStartPoint.X
+
pad, current));
}
currentValue
=
-
1
;
//
绘制X轴
for
(
int
current
=
XCoordinateStartPoint.X; current
<
XCoordinateEndPoint.X; current
+=
CoordinateLablePadX)
{
currentValue
++
;
SizeF textSize
=
gp.MeasureString(currentValue.ToString(), Font);
labelY
=
XCoordinateStartPoint.Y
+
yPad
/
2
;
labelX
=
current;
//
绘制label
gp.DrawString(currentValue.ToString(), Font, Brushes.Black,
new
PointF(labelX, labelY)
);
//
绘制_|_
gp.DrawLine(Pens.Black,
new
Point(current, XCoordinateStartPoint.Y),
new
Point(current, XCoordinateStartPoint.Y
-
pad)
);
}
}
#endregion
float
angle
=
0f;
float
scaleX
=
1f;
float
scaleY
=
1f;
float
translateX
=
1f;
float
translateY
=
1f;
public
Transform1()
{
InitializeComponent();
}
Matrix matrix
=
new
Matrix();
List
<
MyMatrix
>
myList
=
new
List
<
MyMatrix
>
();
protected
override
void
OnPaint(PaintEventArgs e)
{
base
.OnPaint(e);
e.Graphics.Clear(
this
.BackColor);
DrawCoordinate(e);
DrawRectangle();
}
private
void
DrawRectangle()
{
using
(Graphics gh
=
CreateGraphics())
{
Rectangle rect
=
new
Rectangle(
100
,
100
,
200
,
200
);
StringFormat sf
=
new
StringFormat();
sf.Alignment
=
StringAlignment.Center ;
sf.LineAlignment
=
StringAlignment .Center ;
//
平移
gh.TranslateTransform(translateX, translateY);
//
旋转
gh.RotateTransform(angle);
//
缩放
gh.ScaleTransform(scaleX, scaleY);
//
设置gh的变换矩阵
gh.Transform
=
matrix;
gh.DrawRectangle(Pens.Red, rect);
gh.DrawString(
"
AAAAAAAAA
"
,
this
.Font, Brushes.Blue, rect, sf);
matrix
=
gh.Transform;
myList.Add(
new
MyMatrix(matrix.Elements[
0
], matrix.Elements[
1
]));
myList.Add(
new
MyMatrix(matrix.Elements[
2
], matrix.Elements[
3
]));
myList.Add(
new
MyMatrix(matrix.Elements[
4
], matrix.Elements[
5
]));
this
.dgvMatrix.DataSource
=
myList;
}
}
private
void
button1_Click(
object
sender, EventArgs e)
{
angle
+=
10
;
this
.Invalidate();
}
private
void
btnScaleAdd_Click(
object
sender, EventArgs e)
{
scaleX
+=
1
;
scaleY
+=
1
;
this
.Invalidate();
}
private
void
btnScaleSub_Click(
object
sender, EventArgs e)
{
scaleX
-=
1
;
scaleY
-=
1
;
this
.Invalidate();
}
private
void
btnRolateSub_Click(
object
sender, EventArgs e)
{
angle
-=
10
;
this
.Invalidate();
}
private
void
btnBack_Click(
object
sender, EventArgs e)
{
angle
=
0f;
scaleX
=
1f;
scaleY
=
1f;
translateX
=
0f;
translateY
=
0f;
this
.Invalidate();
}
private
void
btnTranslateAdd_Click(
object
sender, EventArgs e)
{
translateX
+=
3f;
translateY
+=
3f;
this
.Invalidate();
}
private
void
btnTranslateSub_Click(
object
sender, EventArgs e)
{
translateX
-=
5f;
translateY
-=
5f;
this
.Invalidate();
}
private
void
btnMatrixBack_Click(
object
sender, EventArgs e)
{
matrix
=
new
Matrix
(
1
,
0
,
0
,
1
,
0
,
0
);
this
.Invalidate();
}
private
void
button1_Click_1(
object
sender, EventArgs e)
{
matrix
=
new
Matrix(
((MyMatrix)
this
.dgvMatrix.Rows[
0
].DataBoundItem).X, ((MyMatrix)
this
.dgvMatrix.Rows[
0
].DataBoundItem).Y ,
((MyMatrix)
this
.dgvMatrix.Rows[
1
].DataBoundItem).X, ((MyMatrix)
this
.dgvMatrix.Rows[
1
].DataBoundItem).Y ,
((MyMatrix)
this
.dgvMatrix.Rows[
2
].DataBoundItem).X, ((MyMatrix)
this
.dgvMatrix.Rows[
2
].DataBoundItem).Y
);
this
.Invalidate();
}
}
}