private void ShapeButton_MouseDown(object sender, MouseEventArgs e) { // Each button holds a shape definition from the xml file // here, we create an actual shape object based on the definition // then initiate a drag/drop operation Button b = ((Button)sender); ShapeDef shapeDefinition = (ShapeDef)b.Tag; // this code is duplicated! (same as clicking Select toolstrip item) deselectAll(); currentTool = Tool.SELECT; textToolStripButton.Checked = false; selectToolStripButton.Checked = true; lineToolStripButton.Checked = false; mouseOffset = new Point(0, 0); tempShape = new Shape(shapeDefinition, 0, 0, 100, 100); ((Button)sender).DoDragDrop(tempShape, DragDropEffects.Move); }
public Shape(ShapeDef shapeDefinition, int x, int y, int width, int height) { this.ShapeDef = shapeDefinition; SolidBrush fillBrush = new SolidBrush(Color.WhiteSmoke); Pen pen = new Pen(Color.Black, 1); x1 = x; y1 = y; bitmap = new Bitmap(width, height); buffer = (Bitmap)bitmap.Clone(); using (Graphics g = Graphics.FromImage(this.bitmap)) { int h = this.bitmap.Height - 1; int w = this.bitmap.Width - 1; if (ShapeDef.Filledpolygon != null) { Point[] points = new Point[ShapeDef.Filledpolygon.Points.Point.Count]; for (int zz=0; zz < ShapeDef.Filledpolygon.Points.Point.Count; zz++) { PointDef p = ShapeDef.Filledpolygon.Points.Point[zz]; points[zz].X = Convert.ToInt32(parser.EvaluateStringExpression(p.X.Replace("w", w.ToString()).Replace("h", h.ToString()))); points[zz].Y = Convert.ToInt32(parser.EvaluateStringExpression(p.Y.Replace("w", w.ToString()).Replace("h", h.ToString()))); } g.FillPolygon(fillBrush, points); g.DrawPolygon(pen, points); } if (ShapeDef.Ellipse != null) { int tw = Convert.ToInt32(parser.EvaluateStringExpression(ShapeDef.Ellipse.Width.Replace("w", w.ToString()).Replace("h", h.ToString()))); int th = Convert.ToInt32(parser.EvaluateStringExpression(ShapeDef.Ellipse.Height.Replace("w", w.ToString()).Replace("h", h.ToString()))); g.FillEllipse(fillBrush, 0, 0, tw, th); g.DrawEllipse(pen, 0, 0, tw, th); } } }
private void GroupButton_Click(object sender, EventArgs e) { int offset = panel1.Height; foreach (Control c in panel1.Controls) { if (c is Panel) { c.Controls.Clear(); c.Dispose(); } } foreach (Control c in panel1.Controls) { if (c is Button) { if (c.Name != ((Button)sender).Name) { // Move unselected group buttons to bottom c.Top = offset - c.Height; offset = c.Top; c.Tag = ""; } else { // This is the selected group button c.Top = 0; c.Tag = "selected"; // Inner panel to contain the shape buttons Panel p = new Panel(); p.Width = panel1.Width; p.Height = panel1.Height; p.Top = 40; XmlSerializer ser = new XmlSerializer(typeof(GroupsDef)); // build the shape buttons and add them to the new inner panel using (FileStream fileStream = new FileStream("shapes.xml", FileMode.Open)) { GroupsDef g = (GroupsDef)ser.Deserialize(fileStream); int buttonTop = 0; for (int x = 0; x < g.Group.Count; x++) { if (g.Group[x].Name == c.Text) { GroupDef group = g.Group[x]; for (int z = 0; z < group.Shape.Count; z++) { ShapeDef shapeDefinition = group.Shape[z]; Button b = new Button(); b.Name = "btnShape" + shapeDefinition.Name.Trim().Replace(" ", ""); b.Text = shapeDefinition.Name; b.BackColor = Color.Beige; b.Width = p.Width; b.Top = buttonTop; b.Height = 35; b.Tag = shapeDefinition; b.MouseDown += ShapeButton_MouseDown; buttonTop += b.Height + 5; p.Controls.Add(b); } panel1.Controls.Add(p); } } } } } } }