public void addPhoto(string path) { StackPanel sp = new StackPanel(); Label label = new Label(); label.HorizontalContentAlignment = HorizontalAlignment.Center; label.FontStyle = FontStyles.Italic; FontFamilyConverter ffc = new FontFamilyConverter(); FontFamily f = ffc.ConvertFromString("Calibri") as FontFamily; label.FontFamily = f; Image img = new Image(); sp.Children.Add(img); sp.Children.Add(label); BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(path); bitmap.EndInit(); img.ToolTip = "Press space to preview photo"; img.Source = bitmap; label.Content = System.IO.Path.GetFileName(img.Source.ToString()); img.Height = 128; img.Width = 135; selectListBox.Items.Add(sp); }
private void UpdateEmpiricalFormula(string empiricalFormula) { var paragraph = new Paragraph(); var ffc = new FontFamilyConverter(); paragraph.FontFamily = (FontFamily)ffc.ConvertFromString("Palatino Linotype"); foreach (var empiricalCharacter in empiricalFormula) { var run = new Run(empiricalCharacter.ToString()); // Subscript any numbers if (Char.IsNumber(empiricalCharacter)) { run.Typography.Variants = FontVariants.Subscript; } paragraph.Inlines.Add(run); } var flowDocument = new FlowDocument(); flowDocument.Blocks.Add(paragraph); EmpiricalFormulaRichTextBlock.Document = flowDocument; }
private bool GuardarDatos() { object tamano = 0; object familia = null; Style estiloNuevo = new Style(); FontSizeConverter convertidor = new FontSizeConverter(); FontFamilyConverter conFami = new FontFamilyConverter(); try { tamano = txtTamano.Text.ToString(); familia = listFonts.SelectedItem.ToString(); Setter Size = new Setter(FontSizeProperty, convertidor.ConvertFrom(tamano)); Setter Family = new Setter(FontFamilyProperty, conFami.ConvertFrom(familia)); Setter color = new Setter(ForegroundProperty, new SolidColorBrush(Color.FromRgb(byte.Parse(sldRojo.Value.ToString()), byte.Parse(sldVerde.Value.ToString()), byte.Parse(sldAzul.Value.ToString())))); estiloNuevo.Setters.Add(Size); estiloNuevo.Setters.Add(Family); estiloNuevo.Setters.Add(color); App.Current.Resources["txtEnter"] = estiloNuevo; return(true); } catch (Exception e) { MessageBox.Show("ERROR: " + e.Message); return(false); } }
private void SetFont(TextBox textBox, System.Drawing.Font font) { FontFamilyConverter ffc = new FontFamilyConverter(); textBox.FontSize = font.Size; textBox.FontFamily = (FontFamily)ffc.ConvertFromString(font.Name); if (font.Bold) { textBox.FontWeight = FontWeights.Bold; } else { textBox.FontWeight = FontWeights.Normal; } if (font.Italic) { textBox.FontStyle = FontStyles.Italic; } else { textBox.FontStyle = FontStyles.Normal; } }
private void button8_Click(object sender, RoutedEventArgs e) { if (selTB != null) { selTB.Foreground = ChooseColor(); using (System.Windows.Forms.FontDialog fd = new System.Windows.Forms.FontDialog()) { try { fd.AllowScriptChange = false; fd.AllowSimulations = false; if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FontFamilyConverter ffc = new FontFamilyConverter(); selTB.FontSize = fd.Font.Size; selTB.FontFamily = (FontFamily)ffc.ConvertFromString(fd.Font.Name); } } catch { //Not a truetype font MessageBox.Show("Шрифт не изменен"); } } } }
public void ChangeFontStyle(string fontType, int size) { FontFamilyConverter converter = new FontFamilyConverter(); txtMainArea.FontFamily = converter.ConvertFromString(fontType) as FontFamily; txtMainArea.FontSize = size; }
private FontFamily GetFontFamily() { var fontFamilyConverter = new FontFamilyConverter(); var fontFamily = (FontFamily)fontFamilyConverter.ConvertFromString(FontFamily); return(fontFamily); }
private void FountSetting(Font font) { FontFamilyConverter ffc = new FontFamilyConverter(); _showMainWindow.FontSize = font.Size; _showMainWindow.FontFamily = (FontFamily)ffc.ConvertFromString(font.Name); if (font.Bold) { _showMainWindow.FontWeight = FontWeights.Bold; } else { _showMainWindow.FontWeight = FontWeights.Normal; } if (font.Italic) { _showMainWindow.FontStyle = FontStyles.Italic; } else { _showMainWindow.FontStyle = FontStyles.Normal; } }
private void Window_Closing(object sender, CancelEventArgs e) { isRunning = false; gHook.unhook(); var isItalic = NpsBlock.FontStyle == FontStyles.Italic; var isUnderline = Equals(NpsBlock.TextDecorations, TextDecorations.Underline); var cvt = new FontFamilyConverter(); AppConfig.SetAppConfig("Exists", "True"); AppConfig.SetAppConfig("Font", cvt.ConvertToString(NpsBlock.FontFamily)); AppConfig.SetAppConfig("Italic", isItalic.ToString()); AppConfig.SetAppConfig("Underline", isUnderline.ToString()); AppConfig.SetAppConfig("Gradation", isGradation.ToString()); var fontClr = ((SolidColorBrush)NpsBlock.Foreground).Color; AppConfig.SetAppConfig("FontColor", $"{fontClr.A},{fontClr.R},{fontClr.G},{fontClr.B}"); if (MainGrid.Background is SolidColorBrush) { var backClr = ((SolidColorBrush)MainGrid.Background).Color; AppConfig.SetAppConfig("Background", $"{backClr.A},{backClr.R},{backClr.G},{backClr.B}"); } else if (MainGrid.Background is ImageBrush) { AppConfig.SetAppConfig("Background", "I>" + filePath); } AppConfig.SetAppConfig("Decimal", isDecimal.ToString()); }
private void MenuItemFontChange_Clicked(object sender, RoutedEventArgs e) { System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog(); fontDialog.Font = new System.Drawing.Font(EditText1.FontFamily.ToString(), (float)EditText1.FontSize); if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FontFamilyConverter ffc = new FontFamilyConverter(); EditText1.FontSize = (double)fontDialog.Font.Size; EditText1.FontFamily = (FontFamily)ffc.ConvertFromString(fontDialog.Font.Name); /* * if (fontDialog.Font.Bold) * textAnnotation.FontWeight = FontWeights.Bold; * else * textAnnotation.FontWeight = FontWeights.Normal; * * if (fontDialog.Font.Italic) * textAnnotation.FontStyle = FontStyles.Italic; * else * textAnnotation.FontStyle = FontStyles.Normal; */ } }
/// <summary> /// Internal API for loading text. Sets a text block's font family. (INLINE OVERLOAD) /// </summary> /// <param name="FBlockToSet">The Block to set the fontfamily of.</param> /// <param name="FontFamily">The font family in string form to set the Block's font family to.</param> /// <returns>An Inline with the font family set.</returns> internal Inline FMXAML_TextAPI_SetFontFamily(Inline FBlockToSet, string FontFamily) // Sets the font family. { FontFamilyConverter _FontFamilyConverter = new FontFamilyConverter(); FontFamily _FinalFontFamily = (FontFamily)_FontFamilyConverter.ConvertFromString(FontFamily); FBlockToSet.FontFamily = _FinalFontFamily; return(FBlockToSet); }
private void ComboBoxFonts_SelectionChanged(object sender, SelectionChangedEventArgs e) { FontFamilyConverter converter = new FontFamilyConverter(); ComboBoxItem selectedItem = e.AddedItems[0] as ComboBoxItem; string fontSelected = selectedItem.Content.ToString(); fontStyle = fontSelected; TextBoxPresentText.FontFamily = converter.ConvertFromString(fontSelected) as FontFamily; }
private void initialStyleSettings() { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; try { doc.Load("BasicConfiguration.xml"); } catch (System.IO.FileNotFoundException) { //If file will not exists create it! doc.LoadXml("<style> \n" + " <fontFamily>Linux Libertine Display G</fontFamily> \n" + " <fontSize>12</fontSize> \n" + " <background>#FFFFFFFF</background> \n" + " <foreground>#FF000000</foreground> \n" + "</style>"); } XmlNode root = doc.FirstChild; if (root.HasChildNodes) { for (int i = 0; i < root.ChildNodes.Count; i++) { XmlElement elementXml = root.ChildNodes[i] as XmlElement; //It means that given element is \r\n if (elementXml != null) { BrushConverter c = new BrushConverter(); FontFamilyConverter converter = new FontFamilyConverter(); SolidColorBrush b; switch (elementXml.LocalName) { case "fontFamily": txtMainArea.FontFamily = converter.ConvertFromString(elementXml.InnerText) as FontFamily; break; case "fontSize": txtMainArea.FontSize = Convert.ToInt32(elementXml.InnerText); break; case "background": b = c.ConvertFromString(elementXml.InnerText) as SolidColorBrush; txtMainArea.Background = b; break; case "foreground": b = c.ConvertFromString(elementXml.InnerText) as SolidColorBrush; txtMainArea.Foreground = b; break; } } } } }
public void Should_not_return_font_family() { DISetup.SetupContainer(); var fontManager = new Mock <ILocalizationManager>(); DISetup.Container.RegisterInstance(fontManager.Object); var converter = new FontFamilyConverter(); var result = converter.Convert(null, null, null, null); result.Should().BeNull(); }
private void SetFont(string fontName) { FontFamilyConverter fontConverter = new FontFamilyConverter(); FontFamily font = fontConverter.ConvertFromString(fontName).To <FontFamily>(); this.uxMainMenu.FontFamily = font; foreach (Window window in Application.Current.Windows) { window.FontFamily = font; } }
public void Should_return_font_family() { DISetup.SetupContainer(); var fontManager = new Mock <IFontFamilyManager>(); var family = new NotoSansFontFamily(); fontManager.Setup(s => s.ResolveFontFamily(It.Is <string>(s => s == "test"))).Returns(family); DISetup.Container.RegisterInstance(fontManager.Object); var converter = new FontFamilyConverter(); var result = converter.Convert("test", null, null, null); result.Should().Be(family.GetFontFamily()); }
/// <summary> /// Returns the width of the given string, using Segoe UI font /// </summary> /// <param name="str">String whose width we would like to calculate</param> /// <param name="fontSize">Fontsize to use for the string</param> /// <returns>Width the string will take on the UI using the Segoe UI font</returns> private double GetStringWidth(string str, double fontSize) { FontFamilyConverter converter = new FontFamilyConverter(); FormattedText formattedText = new FormattedText( str, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface((FontFamily)converter.ConvertFromString("Segoe UI"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), fontSize, Brushes.Black); return(formattedText.Width); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string str = value as string; FontFamilyConverter converter = new FontFamilyConverter(); if (str != null && str.Length > 0) { return(converter.ConvertFromString(str)); } else { return(converter.ConvertFromString("宋体")); } }
private void prepareTextEditor(TextEditor textEditor) { FontFamilyConverter conv = new FontFamilyConverter(); BrushConverter brconv = new BrushConverter(); System.Windows.Markup.XmlLanguageConverter langconv = new System.Windows.Markup.XmlLanguageConverter(); textEditor.FontFamily = (FontFamily)conv.ConvertFromString("Consolas"); textEditor.FontSize = 12; textEditor.Background = (Brush)brconv.ConvertFromString("White"); textEditor.Language = (System.Windows.Markup.XmlLanguage)langconv.ConvertFromString("XML"); textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("XML"); textEditor.ShowLineNumbers = true; textEditor.IsReadOnly = false; textEditor.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; }
private void Window_Loaded(object sender, RoutedEventArgs e) { if (AppConfig.GetAppConfig("Exists") == null || AppConfig.GetAppConfig("Exists") != "True") { return; } var cvt = new FontFamilyConverter(); NpsBlock.FontFamily = KeyBlock.FontFamily = (FontFamily)cvt.ConvertFromString(AppConfig.GetAppConfig("Font")); NpsBlock.FontStyle = KeyBlock.FontStyle = Convert.ToBoolean(AppConfig.GetAppConfig("Italic")) ? FontStyles.Italic : FontStyles.Normal; NpsBlock.TextDecorations = KeyBlock.TextDecorations = Convert.ToBoolean(AppConfig.GetAppConfig("Underline")) ? TextDecorations.Underline : null; isGradation = GradationMenu.IsChecked = Convert.ToBoolean(AppConfig.GetAppConfig("Gradation")); var fontclr = AppConfig.GetAppConfig("FontColor").Split(','); NpsBlock.Foreground = KeyBlock.Foreground = new SolidColorBrush(new Color { A = Convert.ToByte(fontclr[0]), R = Convert.ToByte(fontclr[1]), G = Convert.ToByte(fontclr[2]), B = Convert.ToByte(fontclr[3]) }); var back = AppConfig.GetAppConfig("Background"); if (back.StartsWith("I>")) { MainGrid.Background = new ImageBrush(new BitmapImage(new Uri(back.Substring(2)))); } else { var split = back.Split(','); MainGrid.Background = new SolidColorBrush(new Color { A = Convert.ToByte(split[0]), R = Convert.ToByte(split[1]), G = Convert.ToByte(split[2]), B = Convert.ToByte(split[3]) }); } isDecimal = DecimalMenu.IsChecked = Convert.ToBoolean(AppConfig.GetAppConfig("Decimal")); }
public void AppendBlock(IEnumerable <RichText> richTexts) { Application.Current?.Dispatcher.InvokeAsync(() => { BrushConverter brushConverter = new BrushConverter(); FontFamilyConverter fontFamilyConverter = new FontFamilyConverter(); Paragraph para = new Paragraph(); foreach (var item in richTexts) { Brush foreground = null; try { foreground = (Brush)brushConverter.ConvertFromString(item.color); } catch { } Run run = new Run(item.content); if (foreground != null) { run.Foreground = foreground; } para.Inlines.Add(run); } _richTextBox.BeginChange(); while (_richTextBox.Document.Blocks.Count > maximumBlockCount) { _richTextBox.Document.Blocks.Remove(_richTextBox.Document.Blocks.FirstBlock); } _richTextBox.Document.Blocks.Add(para); _richTextBox.EndChange(); // 如果控件可见并且没有选中文本,则将编辑控件的视图滚动到内容的末尾。 if (_richTextBox.IsVisible && _richTextBox.IsSelectionActive == false) { _richTextBox.ScrollToEnd(); } }); }
private void SettingFont() { using (var fontDialog = new System.Windows.Forms.FontDialog()) { fontDialog.Font = new System.Drawing.Font(m_strFontName, m_fFontSize, m_fontStyle); fontDialog.ShowColor = true; fontDialog.Color = m_fontColor; if (fontDialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel) { FontFamilyConverter ffc = new FontFamilyConverter(); this.messageEditBox.FontSize = fontDialog.Font.Size / 72 * 96; this.messageEditBox.FontFamily = (System.Windows.Media.FontFamily)ffc.ConvertFromString(fontDialog.Font.Name); if (fontDialog.Font.Bold) { this.messageEditBox.FontWeight = FontWeights.Bold; } else { this.messageEditBox.FontWeight = FontWeights.Normal; } if (fontDialog.Font.Italic) { this.messageEditBox.FontStyle = FontStyles.Italic; } else { this.messageEditBox.FontStyle = FontStyles.Normal; } BrushConverter bc = new BrushConverter(); this.messageEditBox.Foreground = (System.Windows.Media.Brush)bc.ConvertFromString(fontDialog.Color.Name); m_strFontName = fontDialog.Font.Name; m_fFontSize = fontDialog.Font.Size; m_fontStyle = fontDialog.Font.Style; m_fontColor = fontDialog.Color; } } }
public void Should_return_supported_name_block_font_family() { DISetup.SetupContainer(); var fontManager = new Mock <IFontFamilyManager>(); var languageService = new Mock <ILanguagesService>(); var family = new NotoSansSCFontFamily(); fontManager.Setup(s => s.ResolveFontFamily(It.Is <string>(s => s == "test"))).Returns(family); languageService.Setup(p => p.GetLanguageBySupportedNameBlock(It.IsAny <string>())).Returns(new Language() { Abrv = "en", Font = "test" }); DISetup.Container.RegisterInstance(fontManager.Object); var converter = new FontFamilyConverter(); var result = converter.Convert("test", null, null, null); result.Should().Be(family.GetFontFamily()); }
private void btnReset_Click(object sender, RoutedEventArgs e) { try { FontFamilyConverter converter = new FontFamilyConverter(); FontFamily consolas = (FontFamily)converter.ConvertFromString("Consolas"); Properties.Settings.Default.EditorFontFace = consolas; this.fontSizeSlider.Value = 13d; Properties.Settings.Default.EditorForeground = Colors.Black; Properties.Settings.Default.EditorBackground = Colors.White; Properties.Settings.Default.Save(); } catch { //Can't reset settings } //Show settings this.ShowSettings(); }
private void FontTypeClick(object sender, RoutedEventArgs e) { System.Windows.Forms.FontDialog fd = new System.Windows.Forms.FontDialog(); var fontstyle = video.FontWeight == FontWeights.Normal ? System.Drawing.FontStyle.Regular : System.Drawing.FontStyle.Bold; fontstyle |= video.FontStyle == FontStyles.Italic ? System.Drawing.FontStyle.Italic : System.Drawing.FontStyle.Regular; fd.Font = new System.Drawing.Font( video.FontFamily.Source, (float)(video.FontSize * 72d / 96d), fontstyle); if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FontFamilyConverter ffc = new FontFamilyConverter(); var selectedFont = fd.Font; video.FontFamily = (FontFamily)ffc.ConvertFromString(selectedFont.Name); video.FontSize = selectedFont.Size * 96.0 / 72.0; video.FontWeight = selectedFont.Bold ? FontWeights.Bold : FontWeights.Regular; video.FontStyle = selectedFont.Italic ? FontStyles.Italic : FontStyles.Normal; } }
/// <summary> /// Opens a .docx file through a string path, /// converts the file and its contents to an in-memory /// FlowDocument and returns the FlowDocument. /// </summary> /// <param name="path">Full path of the file.</param> /// <returns>new FlowDocument populated with the contents of the .docx file.</returns> public FlowDocument LoadDocument(string path) { FlowDocument flowDoc = new FlowDocument(); Package package = Package.Open(path); Uri documentUri = new Uri("/word/document.xml", UriKind.Relative); PackagePart documentPart = package.GetPart(documentUri); XElement wordDoc = XElement.Load(new StreamReader(documentPart.GetStream())); XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; var paragraphs = from p in wordDoc.Descendants(w + "p") select p; foreach (var p in paragraphs) { var style = from s in p.Descendants(w + "pPr") select s; var font = (from f in style.Descendants(w + "rFonts") select f.FirstAttribute).FirstOrDefault(); var size = (from s in style.Descendants(w + "sz") select s.FirstAttribute).FirstOrDefault(); Paragraph par = new Paragraph(); Run r = new Run(p.Value); if (font != null) { FontFamilyConverter converter = new FontFamilyConverter(); r.FontFamily = (FontFamily)converter.ConvertFrom(font.Value); } if (size != null) { r.FontSize = double.Parse(size.Value); } par.Inlines.Add(r); flowDoc.Blocks.Add(par); } return(flowDoc); }
/// <summary> /// Font colour document. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Font_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog(); // If ok. if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // Get the selected text. TextSelection text = mainRTB.Selection; if (!text.IsEmpty) { // Font family converter. FontFamilyConverter ffc = new FontFamilyConverter(); // Apply the properties to the text. text.ApplyPropertyValue(RichTextBox.FontSizeProperty, (double)fontDialog.Font.Size); text.ApplyPropertyValue(RichTextBox.FontFamilyProperty, (FontFamily)ffc.ConvertFromString(fontDialog.Font.Name)); // Apply the properties to the text. if (fontDialog.Font.Bold) { text.ApplyPropertyValue(RichTextBox.FontWeightProperty, FontWeights.Bold); } else { text.ApplyPropertyValue(RichTextBox.FontWeightProperty, FontWeights.Normal); } // Apply the properties to the text. if (fontDialog.Font.Italic) { text.ApplyPropertyValue(RichTextBox.FontStyleProperty, FontStyles.Italic); } else { text.ApplyPropertyValue(RichTextBox.FontStyleProperty, FontStyles.Normal); } } } }
public async Task TestAttachedPropertyButtonFontFamily() { await TestHost.SwitchToAppThread(); var window = await WindowHelpers.CreateInvisibleWindowAsync <TextBoxHelperTestWindow>().ConfigureAwait(false); window.Invoke(() => { var fontFamily = new FontFamilyConverter().ConvertFromString("Arial"); window.TestTextBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestTextBox.FindChild <Button>("PART_ClearText").FontFamily); window.TestButtonTextBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestButtonTextBox.FindChild <Button>("PART_ClearText").FontFamily); window.TestPasswordBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestPasswordBox.FindChild <Button>("PART_ClearText").FontFamily); window.TestButtonPasswordBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestButtonPasswordBox.FindChild <Button>("PART_ClearText").FontFamily); window.TestButtonRevealedPasswordBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestButtonRevealedPasswordBox.FindChild <Button>("PART_ClearText").FontFamily); window.TestComboBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); var toggleButton = window.TestComboBox.FindChild <ToggleButton>("PART_DropDownToggle"); Assert.Equal(fontFamily, toggleButton.FindChild <Button>("PART_ClearText").FontFamily); window.TestEditableComboBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); var edTextBox = window.TestEditableComboBox.FindChild <TextBox>("PART_EditableTextBox"); Assert.Equal(fontFamily, edTextBox.FindChild <Button>("PART_ClearText").FontFamily); window.TestNumericUpDown.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestNumericUpDown.FindChild <Button>("PART_ClearText").FontFamily); window.TestHotKeyBox.SetValue(TextBoxHelper.ButtonFontFamilyProperty, fontFamily); Assert.Equal(fontFamily, window.TestHotKeyBox.FindChild <Button>("PART_ClearText").FontFamily); }); }
public void OpenDocxFile(string fileName) { XElement wordDoc = null; try { Package package = Package.Open(fileName); Uri documentUri = new Uri("/word/document.xml", UriKind.Relative); PackagePart documentPart = package.GetPart(documentUri); wordDoc = XElement.Load(new StreamReader(documentPart.GetStream())); } catch (Exception) { this.rtbDocument.Document.Blocks.Add(new Paragraph(new Run("Cannot open file!"))); return; } XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; var paragraphs = from p in wordDoc.Descendants(w + "p") select p; foreach (var p in paragraphs) { var style = from s in p.Descendants(w + "rPr") select s; var parStyle = from s in p.Descendants(w + "pPr") select s; var font = (from f in style.Descendants(w + "rFonts") select f.FirstAttribute).FirstOrDefault(); var size = (from s in style.Descendants(w + "sz") select s.FirstAttribute).FirstOrDefault(); var rgbColor = (from c in style.Descendants(w + "color") select c.FirstAttribute).FirstOrDefault(); var bold = (from b in style.Descendants(w + "b") select b).FirstOrDefault(); var italic = (from i in style.Descendants(w + "i") select i).FirstOrDefault(); var underline = (from u in style.Descendants(w + "u") select u).FirstOrDefault(); var alignment = (from a in parStyle.Descendants(w + "jc") select a).FirstOrDefault(); var textElements = from t in p.Descendants(w + "t") select t; var list = (from l in parStyle.Descendants(w + "numPr") select l).FirstOrDefault(); StringBuilder text = new StringBuilder(); foreach (var element in textElements) { text.Append(element.Value); } Paragraph par = new Paragraph(); Run run = new Run(text.ToString()); List items = new List(); if (list != null) { //List items = new List(); items.ListItems.Add(new ListItem(par)); var markerStyleElement = (from l in list.Descendants(w + "numId") select l).FirstOrDefault(); TextMarkerStyle markerStyle = new TextMarkerStyle(); if (markerStyleElement != null) { int value = int.Parse(markerStyleElement.Attribute(w + "val").Value); switch (value) { case 1: markerStyle = TextMarkerStyle.Disc; break; case 2: markerStyle = TextMarkerStyle.Decimal; break; } } items.MarkerStyle = markerStyle; } if (font != null) { FontFamilyConverter converter = new FontFamilyConverter(); run.FontFamily = (FontFamily)converter.ConvertFrom(font.Value); } if (size != null) { run.FontSize = double.Parse(size.Value); } if (rgbColor != null) { Color color = ConvertRgbToColor(rgbColor.Value); run.Foreground = new SolidColorBrush(color); } if (bold != null) { if (bold.Attribute(w + "val") != null) { if (bold.Attribute(w + "val").Value == "off") { run.FontWeight = FontWeights.Normal; } else { run.FontWeight = FontWeights.Bold; } } else { run.FontWeight = FontWeights.Bold; } } if (italic != null) { if (italic.Attribute(w + "val") != null) { if (italic.Attribute(w + "val").Value == "off") { run.FontStyle = FontStyles.Normal; } else { run.FontStyle = FontStyles.Italic; } } else { run.FontStyle = FontStyles.Italic; } } if (underline != null) { run.TextDecorations.Add(TextDecorations.Underline); } if (alignment != null) { TextAlignment textAlignment = new TextAlignment(); string value = alignment.Attribute(w + "val").Value; switch (value) { case "left": textAlignment = TextAlignment.Left; break; case "center": textAlignment = TextAlignment.Center; break; case "right": textAlignment = TextAlignment.Right; break; case "justify": textAlignment = TextAlignment.Justify; break; } par.TextAlignment = textAlignment; } else { par.TextAlignment = TextAlignment.Left; } par.Inlines.Add(run); if (list != null) { this.rtbDocument.Document.Blocks.Add(items); } else { this.rtbDocument.Document.Blocks.Add(par); } } }
private void canvas_MouseDown(object sender, MouseButtonEventArgs e) { //khoi tao gia tri startPoint = e.GetPosition(canvas); tempStart = startPoint; color = cboColor.Text; stroke = int.Parse(cboStroke.Text); sizeEraser = int.Parse(cboSizeEraser.Text); //bien convert color var bc = new BrushConverter(); //if (arrayMN != null) //{ // arrayMN[0].sRect.IsHitTestVisible = true; // arrayMN[0].sContent.SetValue(Selector.IsSelectedProperty, true); // arrayMN[0].sContent = null; // arrayMN[0].sRect = null; //} //set gia tri tung truong hop switch (key) { case 1: { _line = new Line(); _line.Stroke = (Brush)bc.ConvertFrom(color); _line.StrokeThickness = stroke; canvas.Children.Add(_line); } break; case 2: { _rectange = new Rectangle(); _rectange.Stroke = (Brush)bc.ConvertFrom(color); _rectange.StrokeThickness = stroke; if (chekFill.IsChecked == true) { _rectange.Fill = (Brush)bc.ConvertFrom(color); } canvas.Children.Add(_rectange); } break; case 3: { _ellipes = new Ellipse(); _ellipes.Stroke = (Brush)bc.ConvertFrom(color); _ellipes.StrokeThickness = stroke; if (chekFill.IsChecked == true) { _ellipes.Fill = (Brush)bc.ConvertFrom(color); } canvas.Children.Add(_ellipes); } break; case 5: { flagSizeEraser = true; } break; case 6: { var mn = new FontFamilyConverter(); TextBox lb = new TextBox(); lb.Text = "Add text...."; lb.FontFamily = (FontFamily)mn.ConvertFromString(cboFontFamily.Text); lb.FontSize = int.Parse(cboFontSize.Text); lb.Foreground = (Brush)bc.ConvertFrom(color); lb.BorderBrush = Brushes.White; canvas.Children.Add(lb); Canvas.SetTop(lb, startPoint.Y); Canvas.SetLeft(lb, startPoint.X); } break; case 7: { _star = new System.Windows.Shapes.Path(); _star.Stroke = (Brush)bc.ConvertFrom(color); _star.StrokeThickness = stroke; if (chekFill.IsChecked == true) { _star.Fill = (Brush)bc.ConvertFrom(color); } canvas.Children.Add(_star); } break; case 8: { _heart = new System.Windows.Shapes.Path(); _heart.Stroke = (Brush)bc.ConvertFrom(color); _heart.StrokeThickness = stroke; if (chekFill.IsChecked == true) { _heart.Fill = (Brush)bc.ConvertFrom(color); } canvas.Children.Add(_heart); } break; case 9: { _arrow = new System.Windows.Shapes.Path(); _arrow.Stroke = (Brush)bc.ConvertFrom(color); _arrow.StrokeThickness = stroke; if (chekFill.IsChecked == true) { _arrow.Fill = (Brush)bc.ConvertFrom(color); } canvas.Children.Add(_arrow); } break; } }