今天我做了一个用GDI+在窗体的pannel中画了两个图形,鼠标移动的区域改变其中的颜色,下面是我做的第一例子,哎也是做完了感觉不是最优的方案,在文章的最后讨论了一下比较好的方法:
做完后我又想到了第二种方法;即把刷子颜色改变,等一系列操作在pannel的paint中做,而在panel1_MouseMove中只判定鼠标在那个区域,然后让panne刷新就好了。我想应该可以的,有意者自己可以试试。我知道你看在这块的时候要说我好笨呀,是啊刚开始的时候,我是定义两个bool型的变量,但是当时没怎么沿着这个思路走。哈哈现在可以借助第二种思路了,同时也只需要定义两个刷子,当在区域内时候,我们在MOusemove中只改变两个bool型的变量就ok,其他不用什么操作了。你说了?
Code
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Drawing.Drawing2D;
10
11 namespace MouseUpdateColor
12 {
13 public partial class Form1 : Form
14 {
15 GraphicsPath path;
16 Rectangle rec;
17 public Form1()
18 {
19 InitializeComponent();
20 path = new GraphicsPath();
21 Point[] points = {
22 new Point(200, 25),
23 new Point(240, 140),
24 new Point(270, 120),
25 new Point(350, 100)};
26 path.StartFigure();
27 path.AddLine(290, 50, 350, 50);
28 path.AddCurve(points, 3);
29 path.CloseFigure();
30 rec = new Rectangle(65, 30, 100, 100);
31 }
32 //矩形画刷颜色
33 private SolidBrush sb = new SolidBrush(Color.Blue);
34 /// <summary>
35 /// 设定画刷的颜色
36 /// </summary>
37 public SolidBrush Sb
38 {
39 get { return sb; }
40 set
41 {
42 //判断是否重绘
43 if (value.Color != sb.Color)
44 {
45 sb = value;
46 this.panel1.Refresh();
47 }
48 }
49 }
50
51 //不规则图形画刷颜色
52 private SolidBrush sb2 = new SolidBrush(Color.Blue);
53 /// <summary>
54 /// 设定画刷的颜色
55 /// </summary>
56 public SolidBrush Sb2
57 {
58 get { return sb2; }
59 set
60 {
61 //判断是否重绘
62 if (value.Color != sb2.Color)
63 {
64 sb2 = value;
65 this.panel1.Refresh();
66 }
67 }
68 }
69
70
71 private void panel1_Paint(object sender, PaintEventArgs e)
72 {
73 Graphics g = e.Graphics;
74 {
75 g.Clear(Color.White);
76 g.FillRectangle(sb, rec); //填充矩形
77 g.FillRegion (sb2, new Region ( path)); //填充不规则图形
78 }
79 }
80
81 private void panel1_MouseMove(object sender, MouseEventArgs e)
82 {
83 if (path == null || rec == null) //区域没有形成
84 {
85 return;
86 }
87 else
88 {//在多边形中
89 if (path.IsVisible(new Point(e.X, e.Y)))
90 {
91 SolidBrush bu = new SolidBrush(Color.Blue);
92 bu.Color = Color.Red;
93 this.Sb2 = bu;
94 }
95 //在矩形中
96 if (rec.Contains(new Point(e.X,e.Y)))
97 {
98 SolidBrush buh = new SolidBrush(Color.Blue);
99 buh.Color = Color.Green;
100 this.Sb = buh;
101 }
102 //不再两个图形中
103 if (!path.IsVisible(new Point(e.X, e.Y))&&!rec.Contains(new Point(e.X,e.Y)))
104 {
105 SolidBrush bs = new SolidBrush(Color.Blue);
106 this.Sb = bs;
107 this.Sb2 = bs;
108 }
109 }
110 }
111 }
112 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Drawing.Drawing2D;
10
11 namespace MouseUpdateColor
12 {
13 public partial class Form1 : Form
14 {
15 GraphicsPath path;
16 Rectangle rec;
17 public Form1()
18 {
19 InitializeComponent();
20 path = new GraphicsPath();
21 Point[] points = {
22 new Point(200, 25),
23 new Point(240, 140),
24 new Point(270, 120),
25 new Point(350, 100)};
26 path.StartFigure();
27 path.AddLine(290, 50, 350, 50);
28 path.AddCurve(points, 3);
29 path.CloseFigure();
30 rec = new Rectangle(65, 30, 100, 100);
31 }
32 //矩形画刷颜色
33 private SolidBrush sb = new SolidBrush(Color.Blue);
34 /// <summary>
35 /// 设定画刷的颜色
36 /// </summary>
37 public SolidBrush Sb
38 {
39 get { return sb; }
40 set
41 {
42 //判断是否重绘
43 if (value.Color != sb.Color)
44 {
45 sb = value;
46 this.panel1.Refresh();
47 }
48 }
49 }
50
51 //不规则图形画刷颜色
52 private SolidBrush sb2 = new SolidBrush(Color.Blue);
53 /// <summary>
54 /// 设定画刷的颜色
55 /// </summary>
56 public SolidBrush Sb2
57 {
58 get { return sb2; }
59 set
60 {
61 //判断是否重绘
62 if (value.Color != sb2.Color)
63 {
64 sb2 = value;
65 this.panel1.Refresh();
66 }
67 }
68 }
69
70
71 private void panel1_Paint(object sender, PaintEventArgs e)
72 {
73 Graphics g = e.Graphics;
74 {
75 g.Clear(Color.White);
76 g.FillRectangle(sb, rec); //填充矩形
77 g.FillRegion (sb2, new Region ( path)); //填充不规则图形
78 }
79 }
80
81 private void panel1_MouseMove(object sender, MouseEventArgs e)
82 {
83 if (path == null || rec == null) //区域没有形成
84 {
85 return;
86 }
87 else
88 {//在多边形中
89 if (path.IsVisible(new Point(e.X, e.Y)))
90 {
91 SolidBrush bu = new SolidBrush(Color.Blue);
92 bu.Color = Color.Red;
93 this.Sb2 = bu;
94 }
95 //在矩形中
96 if (rec.Contains(new Point(e.X,e.Y)))
97 {
98 SolidBrush buh = new SolidBrush(Color.Blue);
99 buh.Color = Color.Green;
100 this.Sb = buh;
101 }
102 //不再两个图形中
103 if (!path.IsVisible(new Point(e.X, e.Y))&&!rec.Contains(new Point(e.X,e.Y)))
104 {
105 SolidBrush bs = new SolidBrush(Color.Blue);
106 this.Sb = bs;
107 this.Sb2 = bs;
108 }
109 }
110 }
111 }
112 }
做完后我又想到了第二种方法;即把刷子颜色改变,等一系列操作在pannel的paint中做,而在panel1_MouseMove中只判定鼠标在那个区域,然后让panne刷新就好了。我想应该可以的,有意者自己可以试试。我知道你看在这块的时候要说我好笨呀,是啊刚开始的时候,我是定义两个bool型的变量,但是当时没怎么沿着这个思路走。哈哈现在可以借助第二种思路了,同时也只需要定义两个刷子,当在区域内时候,我们在MOusemove中只改变两个bool型的变量就ok,其他不用什么操作了。你说了?