//创建文本框 public RichTextBoxEx CreateRtb() { count++; RichTextBoxEx rtb = new RichTextBoxEx { Width = 60, Height = 30, Location = new Point(50, 260), BorderStyle = BorderStyle.Fixed3D, Name = $"rtb{count}" }; rtbs.Add(rtb); rtb.Enter += new EventHandler(events.rtb_FocusEnter); rtb.Leave += new EventHandler(events.rtb_FocusLeave); rtb.Move += new EventHandler(events.rtb_Move); rtb.SelectionChanged += new EventHandler(events.rtb_SelectionChanged); //rtb.SelectionChanged += new EventHandler(events.ShowFont); rtb.MouseDown += new MouseEventHandler(events.PanelToFront); rtb.MouseClick += new MouseEventHandler(events.PanelToFront); events.rtbs = rtbs; events.panels = panels; rtb.SetFrame(); return(rtb); }
//焦点离开文本框时隐藏工具栏 public void rtb_FocusLeave(object sender, EventArgs e) { RichTextBoxEx rtb = sender as RichTextBoxEx; try { string StrIndex = rtb.Name.Substring(3, 1); int index = int.Parse(StrIndex); if (rtb.Name == $"rtb{StrIndex}") { if (!IsFocused(panels[index - 1])) { panels[index - 1].Visible = false; rtb.Parent.Controls[0].Visible = false; } } else { return; } } catch (Exception) { return; } }
//点击文本框时工具栏在最前显示 public void PanelToFront(object sender, EventArgs e) { RichTextBoxEx rtb = sender as RichTextBoxEx; string StrIndex = rtb.Name.Substring(3, 1); int index = int.Parse(StrIndex); panels[index - 1].BringToFront(); }
private void 插入文本框ToolStripMenuItem_Click(object sender, EventArgs e) { RichTextBoxEx rtb = CtrlCreate.CreateRtb(); FlowLayoutPanel panel = CtrlCreate.CreatePanel(rtb); this.Controls.Add(rtb); rtb.BringToFront(); this.Controls.Add(panel); }
//选中文本更改时,遍历文本,获取每个字的字体 public void rtb_SelectionChanged(object sender, EventArgs e) { fonts = new List <Font>(); RichTextBoxEx rtb = sender as RichTextBoxEx; RichTextBox temp = new RichTextBox(); int len = rtb.SelectionLength;//选中文本长度 temp.Rtf = rtb.SelectedRtf; for (int i = 0; i < len; i++) { temp.Select(i, 1); //逐个选中副本中的文字 fonts.Add(temp.SelectionFont); } }
public static void SetFrame(this Control control) { FrameControl fControl = null;//边框控件 //按下鼠标键时 control.MouseDown += (sender, e) => { control.BringToFront(); //清除所有控件的边框区域,最主要的是清除上次点击控件的边框,恢复原来状态 foreach (Control ctrl in control.Parent.Controls) { if (ctrl is FrameControl) { ctrl.Visible = false; } } fControl = new FrameControl(control); control.Parent.Controls.Add(fControl); }; //鼠标单击时 control.MouseClick += (sender, e) => { control.BringToFront(); try { RichTextBoxEx rtb = sender as RichTextBoxEx; rtb.BorderStyle = BorderStyle.None; } catch (NullReferenceException) { return; } }; //鼠标键释放时 control.MouseUp += (sender, e) => { control.Refresh(); //显示虚线 fControl.DrawDottedLines(); }; }
//文本框移动时对应的工具栏随之移动 public void rtb_Move(object sender, EventArgs e) { RichTextBoxEx rtb = sender as RichTextBoxEx; try { string StrIndex = rtb.Name.Substring(3, 1); int index = int.Parse(StrIndex); if (rtb.Name == $"rtb{StrIndex}") { panels[index - 1].Location = new Point(rtbs[index - 1].Location.X, rtbs[index - 1].Location.Y - 65); } else { return; } } catch (Exception) { return; } }
//焦点进入文本框时显示工具栏 public void rtb_FocusEnter(object sender, EventArgs e) { RichTextBoxEx rtb = sender as RichTextBoxEx; try { string StrIndex = rtb.Name.Substring(3, 1); int index = int.Parse(StrIndex); if (rtb.Name == $"rtb{StrIndex}") { panels[index - 1].Visible = true; } else { return; } } catch (Exception) { return; } }