/// <summary> /// 向富文本框中添加新行 /// </summary> /// <param name="rtb">欲添加新行的富文本框</param> /// <param name="appendString">欲添加的新行</param> /// <param name="foregroundColor">新行文本前景色</param> /// <param name="backgroundColor">新行文本背景色</param> /// <param name="scrollToEnd">自动滚动至最后</param> private void AppendRTBLine(RichTextBox rtb, string appendString, SolidColorBrush foregroundColor, SolidColorBrush backgroundColor, bool scrollToEnd = true) { TextRange tr = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd); tr.Text = appendString + "\n"; tr.ApplyPropertyValue(TextElement.ForegroundProperty, foregroundColor); tr.ApplyPropertyValue(TextElement.BackgroundProperty, backgroundColor); if (scrollToEnd) { TestRichTextbox.ScrollToEnd(); } }
/// <summary> /// 执行指令框中的指令 /// </summary> private void ExecuteTextBoxInstrution() { UpdateMaxWidth(InstructionTextBox.Text); // Execute instruction. string retnInfo = ExecuteInstruction(InstructionTextBox.Text); InitializeGUI(ATheme.GetThemeConfig()); if (AInstruction.ADD_CONST_INSTRUCTION == retnInfo) { constInstructionInputMode = true; TestRichTextbox.IsReadOnly = false; InstructionTextBox.IsEnabled = false; //AppendRTBLine(TestRichTextbox, "确认编辑: Alt + S / 取消编辑: Alt + Esc", themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); AppendRTBLine(TestRichTextbox, "Confirm: Alt + S / Cancel: Alt + Esc", themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); TestRichTextbox.BorderThickness = new Thickness(1); // Content Start TestRichTextbox.CaretPosition = TestRichTextbox.Document.ContentEnd; constInstructionContentRange.ContentStart = TestRichTextbox.CaretPosition.Paragraph.ContentStart; TestRichTextbox.CaretPosition.Paragraph.IsEnabled = false; // If ci already exist string constInstruction = AInstruction_Const.GetConstInstructionFromMacroInstruction(InstructionTextBox.Text); if (AConstInstruction.Exist(constInstruction)) { ConstInstruction ci = new ConstInstruction(); if (AConstInstruction.GetConstInstructionFrame(constInstruction, ref ci)) { OldConstInstructionFileName = AConstInstruction.GetFileNameFromConstInstruction(ci); foreach (string insLine in ci.instructionLines) { AppendRTBLine(TestRichTextbox, insLine, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); UpdateMaxWidth(insLine); } } } // Set CaretPosition. TestRichTextbox.CaretPosition = TestRichTextbox.Document.ContentEnd; //UpdateMaxWidth("确认编辑: Alt + S / 取消编辑: Alt + Esc"); UpdateMaxWidth("Confirm: Alt + S / Cancel: Alt + Esc"); Resize(true, constInstructionInputWidthBias); TestRichTextbox.Focus(); showOutput = true; return; } else if (AInstruction.UPDATE_INSTRUCTION == retnInfo) { UpdateMaxWidth(InstructionTextBox.Text); AppendRTBLine(TestRichTextbox, InstructionTextBox.Text, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); showOutput = true; InstructionTextBox.Text = ""; Resize(); AHelper.AppendString appendString = delegate(string content, AInstruction.ReportType type) { // InstructionTextBox 被主线程占用,利用 Dispatcher 进行操作 TestRichTextbox.Dispatcher.BeginInvoke((Action)(() => { switch (type) { case AInstruction.ReportType.NONE: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); break; case AInstruction.ReportType.WARNING: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); break; case AInstruction.ReportType.OK: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputOk, themeConfig.BackgroundOutputOk); break; case AInstruction.ReportType.ERROR: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputError, themeConfig.BackgroundOutputError); break; } InstructionTextBox.Text = ""; UpdateMaxWidth(content); Resize(); })); }; new Thread(AUpdate.UpdateAndRestart).Start(appendString); return; } // Append instruction line. AppendRTBLine(TestRichTextbox, InstructionTextBox.Text, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); // Print report. foreach (var reportInfo in AInstruction.ReportInfo) { AppendRTBLine(TestRichTextbox, reportInfo, themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); UpdateMaxWidth(reportInfo); // If have reportInfo, then show outputBox. showOutput = true; } // Print return information. SolidColorBrush bgcolor; SolidColorBrush fgcolor; switch (AInstruction.reportType) { case AInstruction.ReportType.OK: bgcolor = themeConfig.BackgroundOutputOk; fgcolor = themeConfig.ForegroundOutputOk; if (AInstruction.ReportInfo.Count == 0) { showOutput = false; } break; case AInstruction.ReportType.WARNING: bgcolor = themeConfig.BackgroundOutputWarning; fgcolor = themeConfig.ForegroundOutputWarning; showOutput = true; break; case AInstruction.ReportType.ERROR: bgcolor = themeConfig.BackgroundOutputError; fgcolor = themeConfig.ForegroundOutputError; showOutput = true; break; default: bgcolor = themeConfig.BackgroundOutputWarning; fgcolor = themeConfig.ForegroundOutputWarning; break; } if (retnInfo != "") { AppendRTBLine(TestRichTextbox, retnInfo.Trim(), fgcolor, bgcolor); } if (AInstruction.GetType(InstructionTextBox.Text) == InstructionType.CMD) { InstructionTextBox.Text = "> "; showOutput = true; } else { InstructionTextBox.Text = ""; if (AInstruction.ReportType.OK == AInstruction.reportType && 0 == AInstruction.ReportInfo.Count) { Visibility = Visibility.Hidden; } } InstructionTextBox.SelectionStart = InstructionTextBox.Text.Length; // Update width and resize. UpdateMaxWidth(retnInfo); Resize(); }