在Canvas上绘制的许多小圆。
怎么能实现单击某个小圆让小圆颜色改变!
- C# code
public void GetControl(Canvas can) { try { GeometryGroup gg = new GeometryGroup(); PathGeometry pg = new PathGeometry(); PathFigure pf = new PathFigure(); if (!String.IsNullOrEmpty(MaxConwidth.Text) && !String.IsNullOrEmpty(MinConwidth.Text) && !String.IsNullOrEmpty(MaxConHeight.Text) && !String.IsNullOrEmpty(MinConHeight.Text) && !String.IsNullOrEmpty(Diameter.Text) && !String.IsNullOrEmpty(WidthInterval.Text) && !String.IsNullOrEmpty(HeightInterval.Text) && !String.IsNullOrEmpty(Interval.Text) && !String.IsNullOrEmpty(reCloumns.Text) && !String.IsNullOrEmpty(reRows.Text) && !String.IsNullOrEmpty(Conthickness.Text) && !String.IsNullOrEmpty(Pointthickness.Text)) { //宽度 double width = double.Parse(MaxConwidth.Text) * 10; //高度 double height = double.Parse(MaxConHeight.Text) * 10; //直径 double diameter = double.Parse(Diameter.Text) * 10; double widthinterval = double.Parse(WidthInterval.Text) * 10; double heightinterval = double.Parse(HeightInterval.Text) * 10; //间距 double interval = double.Parse(Interval.Text) * 10; //第一个孔的坐标 double Y = (height - heightinterval) / 2; double X = (width - widthinterval) / 2; for (int j = 0; j < int.Parse(reCloumns.Text); j++) { for (int i = 0; i < int.Parse(reRows.Text); i++) { EllipseGeometry eg = new EllipseGeometry(); //中心点 eg.Center = new Point(X + i * interval, Y + j * interval); eg.RadiusX = diameter / 2; eg.RadiusY = diameter / 2; gg.Children.Add(eg); } } reCounts.Text = (int.Parse(reCloumns.Text) * int.Parse(reRows.Text)).ToString(); pf.StartPoint = new Point(0, 0); pf.Segments.Add(new PolyLineSegment(new Point[3] { new Point(width, 0), new Point(width, height), new Point(0, height) }, true)); pg.Figures.Add(pf); gg.Children.Add(pg); can.Clip = gg; double w = X + interval * (int.Parse(reRows.Text) - 1) + diameter / 2; double r = Y + interval * (int.Parse(reCloumns.Text) - 1) + diameter / 2; if (w > width || r > height || (X - diameter / 2) < 0 || (Y - diameter / 2) < 0) { can.Background = Brushes.Red; } else { can.Background = new SolidColorBrush(Color.FromArgb(62, 00, 00, 00)); } } } catch (Exception) { } }
------解决方案--------------------------------------------------------
自己把每一个图形元素作为一个类存在,点击鼠标时,对所有元素判断是否鼠标在其内,然后改变颜色,然后重绘,这需要你有一定的面对对象和编程模式的基础了,先试着写个简单的玩玩吧