private void CommandLineBox_KeyPress(object sender, KeyPressEventArgs e) { if (Tclient != null) { if ((Tclient.Client != null) && (Tclient.Connected)) { CommandLineBox.SelectionColor = Color.Black; if (e.KeyChar == '\r') { CommandLineBox.Select(CommandLineBox.TextLength > 64 ? CommandLineBox.TextLength / 2 : 0, CommandLineBox.TextLength); int iStartIndex = CommandLineBox.SelectedText.LastIndexOf(@">"); if (iStartIndex == -1) { return; } command = CommandLineBox.SelectedText.Substring(iStartIndex + 1, CommandLineBox.SelectionLength - iStartIndex - 1); //exec command ExecCommand(command); // command = string.Empty;//可以用一个array存储历史command,就更像一个shell了 return; } command = command + e.KeyChar; } } }
public void AddtoBox(Color fontcolor, string strcommand) { if (CommandLineBox.InvokeRequired)//不是同一线程调用,调用控件的委托 { AddBoxCallback d = new AddBoxCallback(AddtoBox); this.Invoke(d, new object[] { fontcolor, strcommand }); } else//同一线程,直接操作 { if (CommandLineBox.TextLength > 150000) { CommandLineBox.Select(0, CommandLineBox.TextLength / 3); CommandLineBox.Cut(); } CommandLineBox.SelectionColor = fontcolor; CommandLineBox.AppendText(strcommand); CommandLineBox.ScrollToCaret(); CommandLineBox.SelectionColor = Color.Black; strcommand = strcommand.Replace(@"/>", ""); MainForm.pMainForm.mapdoc.WriteNetLog(strcommand); if (NetLogFile.logfile.ws == null)//还未创建文件 { NetLogFile.OpenFile(MainForm.pMainForm.RecordInfo); } NetLogFile.writeLine("\r\n" + strcommand); if (NetLogFile.length >= 600 * 1024)//不允许大于600K { NetLogFile.close(); } if (MainForm.pMainForm.AutoConnectWin.Visible) { MainForm.pMainForm.AutoConnectWin.AddToBox(strcommand); } } }
private string GetCurrentLineText() { var caretIndex = CommandLineBox.CaretIndex; var lineIndex = CommandLineBox.GetLineIndexFromCharacterIndex(caretIndex); return(lineIndex >= 0 && lineIndex < CommandLineBox.LineCount ? CommandLineBox.GetLineText(lineIndex) : ""); }
private void ReplaceCurrentLineText(string newText) { var caretIndex = CommandLineBox.CaretIndex; var lineIndex = CommandLineBox.GetLineIndexFromCharacterIndex(caretIndex); var characterIndex = CommandLineBox.GetCharacterIndexFromLineIndex(lineIndex); var textOnLine = CommandLineBox.GetLineText(lineIndex); CommandLineBox.Select(characterIndex, textOnLine.Length); CommandLineBox.SelectedText = newText; CommandLineBox.Focus(); CommandLineBox.Select(CommandLineBox.Text.Length, 0); CommandLineBox.ScrollToLine(lineIndex); }
private void CommandLineBox_OnLoaded(object sender, RoutedEventArgs e) { CommandLineBox.Focus(); CommandLineBox.CaretIndex = CommandLineBox.Text.Length; }
private void ClearCommandLine_Click(object sender, EventArgs e) { CommandLineBox.Clear(); SendCommand("\n"); AddtoBox(Color.Black, @"/>"); }
/// <summary> /// 执行输入或按键命令,即下发命令字符串 /// </summary> /// <param name="strcommand"></param> public bool ExecCommand(string strcommand) { try { strcommand = strcommand.TrimEnd('\n'); switch (strcommand) { case "disconnect": { if (ConnNodeBtn.Text == "断开连接") { ConnNodeBtn.PerformClick(); } if (ConnNodeBtn.Text == "取消连接") { ConnNodeBtn.PerformClick(); } break; } case "exit": CommandLineBox.SelectionColor = Color.Chocolate; CommandLineBox.AppendText("关闭命令行" + "\r\n"); SendCommand("\n"); this.Hide(); break; case "reboot": { AddtoBox(Color.Black, "重启节点\r\n"); if (Tstream.CanWrite) { byte[] cbuf = System.Text.Encoding.ASCII.GetBytes(strcommand + "\r"); Tstream.Write(cbuf, 0, strcommand.Length + 1); } break; } case "ad": AddtoBox(Color.Black, "发送AD命令\r\n"); SendCommand(strcommand); break; case "ad -d": AddtoBox(Color.Black, "发送AD命令,工作模式\r\n"); SendCommand(strcommand); break; case "gd": { AddtoBox(Color.Black, "下载数据文件(本命令只能通过主面板按钮实现)\r\n"); SendCommand(strcommand); break; } case "gd -l": { AddtoBox(Color.Black, "在线加载程序文件(本命令只能通过主面板按钮实现)\r\n"); SendCommand(strcommand); break; } case "gd -u": { AddtoBox(Color.Black, "更新程序文件(本命令只能通过主面板按钮实现)\r\n"); SendCommand(strcommand); break; } case "gd -t": { AddtoBox(Color.Black, "更新程序文件(本命令只能通过主面板按钮实现)\r\n"); SendCommand(strcommand); break; } case "gd -f": { AddtoBox(Color.Black, "更新fpga文件(本命令只能通过主面板按钮实现)\r\n"); SendCommand(strcommand); break; } case "gd -b": { AddtoBox(Color.Black, "更新loader文件(本命令只能通过主面板按钮实现)\r\n"); SendCommand(strcommand); break; } default: SendCommand(strcommand); break; } return(true); // } catch (Exception MyEx) { SendStatusLabel(MyEx.Message); AddtoBox(Color.Black, MyEx.Message + ":" + MyEx.StackTrace + "\r\n/>"); return(false); } }