private void Ctrl_Write(string str) { try { int Line = ConsoleCtrl.Invoke(() => ConsoleCtrl.GetLineIndexFromCharacterIndex(ConsoleCtrl.SelectionStart)); int start = ConsoleCtrl.Invoke(() => ConsoleCtrl.GetCharacterIndexFromLineIndex(Line)); StringBuilder sb = new StringBuilder(); sb.AppendLine(str); ConsoleCtrl.Invoke(() => { ConsoleCtrl.AppendText(sb.ToString()); ConsoleCtrl.SelectionStart = ConsoleCtrl.Text.Length; }); } catch (Exception) { } }
private void Ctrl_PreKeyDown(object sender, KeyEventArgs e) { try { if (KeydownHandled) { e.Handled = true; return; } int Line = ConsoleCtrl.GetLineIndexFromCharacterIndex(ConsoleCtrl.SelectionStart); int start = ConsoleCtrl.GetCharacterIndexFromLineIndex(Line); if (e.Key == Key.Up || e.Key == Key.Down) { e.Handled = true; return; } else if (e.Key == Key.Left) { //判断是否最后一行 if (start == ConsoleCtrl.SelectionStart) { e.Handled = true; } return; } if (Line != ConsoleCtrl.LineCount - 1) { e.Handled = true; return; } if (Key.Enter == e.Key) { ConsoleThread.Resume(); return; } /* * switch (e.Key) * { * case Key.NumPad0: * case Key.NumPad1: * case Key.NumPad2: * case Key.NumPad3: * case Key.NumPad4: * case Key.NumPad5: * case Key.NumPad6: * case Key.NumPad7: * case Key.NumPad8: * case Key.NumPad9: * string res = Convert.ToString(e.Key - Key.NumPad0); * ConsoleCtrl.SelectionStart++; * string text = ConsoleCtrl.Text; * ConsoleCtrl.Text = text.Substring(0, ConsoleCtrl.SelectionStart - 2) + res + text.Substring(ConsoleCtrl.SelectionStart - 1); * break; * case Key.D0: * case Key.D1: * case Key.D2: * case Key.D3: * case Key.D4: * case Key.D5: * case Key.D6: * case Key.D7: * case Key.D8: * case Key.D9: * ConsoleCtrl.AppendText(Convert.ToString(e.Key - Key.D0)); * e.Handled = true; * break; * } */ } catch (Exception) { } }