原文作者 java_liyi, c#+Gdi,怎么移动和改变已经绘制好的图形位置和大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Drawing;
using
System.Data;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
namespace
DrawTest
{
public
delegate
void
DrawMeHandler(Graphics g);
public
delegate
void
MouseMoveHandler(Point p);
public
partial
class
Canvas : UserControl
{
RectangleFigure rectangleFigure =
new
RectangleFigure();
DrawMeHandler drawMeHandler;
MouseMoveHandler mouseMoveHandler;
public
Canvas()
{
InitializeComponent();
this
.DoubleBuffered =
true
;
this
.Paint += Canvas_Paint;
this
.Load += Canvas_Load;
this
.MouseMove += Canvas_MouseMove;
}
void
Canvas_MouseMove(
object
sender, MouseEventArgs e)
{
this
.mouseMoveHandler.Invoke(e.Location);
this
.Refresh();
}
void
Canvas_Load(
object
sender, EventArgs e)
{
rectangleFigure.X = 10;
rectangleFigure.Y = 10;
rectangleFigure.Width = 100;
rectangleFigure.Height = 50;
drawMeHandler =
new
DrawMeHandler(rectangleFigure.DrawMe);
mouseMoveHandler =
new
MouseMoveHandler(rectangleFigure.MouseMove);
}
void
Canvas_Paint(
object
sender, PaintEventArgs e)
{
if
(drawMeHandler !=
null
) drawMeHandler.Invoke(e.Graphics);
}
/// <summary>
/// 矩形对象
/// </summary>
public
class
RectangleFigure
{
public
int
X {
get
;
set
; }
public
int
Y {
get
;
set
; }
public
int
Height {
get
;
set
; }
public
int
Width {
get
;
set
; }
public
bool
Actived {
get
;
set
; }
public
bool
IsExist(Point p)
{
Rectangle rectangle =
new
Rectangle(
this
.X,
this
.Y,
this
.Width,
this
.Height);
return
rectangle.Contains(p);
}
public
void
MouseMove(Point p)
{
Actived = IsExist(p);
}
public
void
DrawMe(Graphics g)
{
Pen p =
new
Pen(Color.Black);
g.FillRectangle(p.Brush,
this
.X,
this
.Y,
this
.Width,
this
.Height);
if
(Actived)
{
p.Color = Color.Red;
g.DrawRectangle(p,
this
.X,
this
.Y,
this
.Width,
this
.Height);
}
}
}
}
}
|
没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。
本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5810773.html
,如需转载请自行联系原作者