整体界面展示
右侧导航说明
流程图操作
根据左侧工具箱进行节点拖入,如下图
拖入部分代码
private void pictureBox1_DragEnter(object sender, DragEventArgs e) { if ((e.Data.GetDataPresent(typeof(Button)))) { e.Effect = DragDropEffects.Copy; } } private void pictureBox1_DragDrop(object sender, DragEventArgs e) { if ((e.Data.GetDataPresent(typeof(Button)))) { Button btnInstance = new Button(); FieldInfo info, info2, info3; object obj, obj2, obj3; info = e.Data.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance); obj = info.GetValue(e.Data); info2 = obj.GetType().GetField("data", BindingFlags.NonPublic | BindingFlags.Instance); obj2 = info2.GetValue(obj); System.Collections.Hashtable dataItems = (obj2 as System.Collections.Hashtable); foreach (var dataItem in dataItems) { System.Collections.DictionaryEntry dictEntry = (System.Collections.DictionaryEntry)dataItem; object key = dictEntry.Key; object value = dictEntry.Value; info3 = value.GetType().GetField("data", BindingFlags.Public | BindingFlags.Instance); obj3 = info3.GetValue(value); btnInstance = obj3 as Button; // 此处转化成功,成功获取到传递进来的button实例。 } try { Button bt = new Button(); bt.ImageAlign = ContentAlignment.MiddleLeft; bt.TextAlign = ContentAlignment.MiddleRight; bt.ImageList = imageList1; bt.ImageIndex = int.Parse(btnInstance.Tag.ToString()); bt.AutoSize = false; bt.Location = this.PointToClient(new Point(e.X-124, e.Y-48/2)); bt.Size = new Size(136, 48); bt.ForeColor = Color.Black; bt.Text = btnInstance.Text; bt.Name = btnInstance.Name; bt.BackColor = Color.Transparent; // 在控件增加到窗体里时做一个判断,如果是开始 和结束只能存在一个 foreach (Control control in this.pictureBox1.Controls) { if (control.Name == btnInstance.Name || control.Name == btnInstance.Name) { MessageBox.Show("已存在,不能添加"); return; } } pictureBox1.Controls.Add(bt); //拖拽移动 MoveBlock(bt); bt.ContextMenuStrip = this.contextMenuStrip1; bt.Click += new System.EventHandler(bt_click); huoqu_kongjian(); bt.BringToFront(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
Image ImgDeal(Image img, byte alpha, int width, int height) { var bmpSize = new Bitmap(width, height); using (var g = Graphics.FromImage(bmpSize)) { g.DrawImage(img, new Rectangle(0, 0, width, height)); } var bmpAlpha = new Bitmap(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { var color = bmpSize.GetPixel(i, j); bmpAlpha.SetPixel(i, j, Color.FromArgb(alpha, color)); } } return (Image)bmpAlpha.Clone(); }
// 定义按钮拖动 轨迹的样式 private void bt_GiveFeedback(object sender, GiveFeedbackEventArgs e) { e.UseDefaultCursors = false; if (e.Effect == DragDropEffects.None) { //var img = ImgDeal(imageList1.Images[1], 70, 136,48); //Cursor.Current = new Cursor(((Bitmap)img).GetHicon()); } else { var img = ImgDeal(imageList1.Images[1], 70, 136, 48); Cursor.Current = new Cursor(((Bitmap)img).GetHicon()); } }
支持鼠标经过 连接线变色且支持右键删除连线
删除
右键节点支持删除和属性设置
右键属性
属性设置的部分代码
try { c_bt.AutoSize = btn.AutoSize; c_bt.Top = btn.Location.Y; c_bt.Left = btn.Location.X; c_bt.Width = btn.Width; c_bt.Height = btn.Height; c_bt.BackColor = btn.BackColor; //c_bt.BorderStyle = ((Button)this.panel1.Controls.Find(bt.Name, true)[0]).BorderStyle; c_bt.ForeColor = btn.ForeColor; c_bt.Font = btn.Font; c_bt.Text = btn.Text; c_bt.TextAlign = btn.TextAlign; propertyGrid1.SelectedObject = c_bt; } catch { }
标签:流程图,C#,Button,bt,var,new,btn,btnInstance,轻量级 From: https://www.cnblogs.com/cxyyn/p/17012609.html