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 Button17_Click(object sender, RoutedEventArgs e) { //Will change visualBrush of Rectangle6 to skew Text "Hello World" Rectangle6.Width = 100; Rectangle6.Height = 110; VisualBrush brush17 = new VisualBrush(); StackPanel txt = new StackPanel(); txt.Background = Brushes.White; TextBlock text = new TextBlock(); FontSizeConverter size = new FontSizeConverter(); text.FontSize = (double)size.ConvertFrom("10pt"); text.Text = "Hello, World!"; txt.Children.Add(text); TextBlock text1 = new TextBlock(); FontSizeConverter size2 = new FontSizeConverter(); text1.FontSize = (double)size2.ConvertFrom("10pt"); text1.Text = "Hello, World!"; txt.Children.Add(text1); TextBlock text2 = new TextBlock(); FontSizeConverter size3 = new FontSizeConverter(); text2.FontSize = (double)size3.ConvertFrom("10pt"); text2.Text = "Hello, World!"; txt.Children.Add(text2); TextBlock text3 = new TextBlock(); FontSizeConverter size4 = new FontSizeConverter(); text3.FontSize = (double)size4.ConvertFrom("10pt"); text3.Text = "Hello, World!"; txt.Children.Add(text3); TextBlock text4 = new TextBlock(); FontSizeConverter size5 = new FontSizeConverter(); text4.FontSize = (double)size5.ConvertFrom("2pt"); text4.Text = "Hello, World!"; txt.Children.Add(text4); RotateTransform rotate = new RotateTransform(); rotate.CenterX = 0.5; rotate.CenterY = 0.5; rotate.Angle = -45; brush17.Visual = txt; brush17.RelativeTransform = rotate; Rectangle6.Fill = brush17; }
// <SnippetGraphicsMMVisualBrushAsRectangleBackgroundExample> private void visualBrushAsRectangleFillExample(Panel mainPanel) { // <SnippetGraphicsMMVisualBrushAsRectangleBackgroundExample1> VisualBrush myVisualBrush = new VisualBrush(); // Create the visual brush's contents. StackPanel myStackPanel = new StackPanel(); myStackPanel.Background = Brushes.White; Rectangle redRectangle = new Rectangle(); redRectangle.Width = 25; redRectangle.Height = 25; redRectangle.Fill = Brushes.Red; redRectangle.Margin = new Thickness(2); myStackPanel.Children.Add(redRectangle); TextBlock someText = new TextBlock(); FontSizeConverter myFontSizeConverter = new FontSizeConverter(); someText.FontSize = (double)myFontSizeConverter.ConvertFrom("10pt"); someText.Text = "Hello, World!"; someText.Margin = new Thickness(2); myStackPanel.Children.Add(someText); Button aButton = new Button(); aButton.Content = "A Button"; aButton.Margin = new Thickness(2); myStackPanel.Children.Add(aButton); // Use myStackPanel as myVisualBrush's content. myVisualBrush.Visual = myStackPanel; // Create a rectangle to paint. Rectangle myRectangle = new Rectangle(); myRectangle.Width = 150; myRectangle.Height = 150; myRectangle.Stroke = Brushes.Black; myRectangle.Margin = new Thickness(5, 0, 5, 0); // Use myVisualBrush to paint myRectangle. myRectangle.Fill = myVisualBrush; // </SnippetGraphicsMMVisualBrushAsRectangleBackgroundExample1> mainPanel.Children.Add(myRectangle); }
private void Button16_Click(object sender, RoutedEventArgs e) { //Will change visualBrush of Rectangle6 to Text "Hello World" Rectangle6.Width = 100; Rectangle6.Height = 110; VisualBrush brush16 = new VisualBrush(); StackPanel stack_text = new StackPanel(); stack_text.Background = Brushes.White; TextBlock brushA = new TextBlock(); FontSizeConverter size = new FontSizeConverter(); brushA.FontSize = (double)size.ConvertFrom("10pt"); brushA.Text = "Hello, World!"; stack_text.Children.Add(brushA); TextBlock brushB = new TextBlock(); FontSizeConverter size2 = new FontSizeConverter(); brushB.FontSize = (double)size2.ConvertFrom("10pt"); brushB.Text = "Hello, World!"; stack_text.Children.Add(brushB); TextBlock brushC = new TextBlock(); FontSizeConverter size3 = new FontSizeConverter(); brushC.FontSize = (double)size3.ConvertFrom("10pt"); brushC.Text = "Hello, World!"; stack_text.Children.Add(brushC); TextBlock brushD = new TextBlock(); FontSizeConverter size4 = new FontSizeConverter(); brushD.FontSize = (double)size4.ConvertFrom("10pt"); brushD.Text = "Hello, World!"; stack_text.Children.Add(brushD); brush16.Visual = stack_text; Rectangle6.Fill = brush16; }
internal static Style ToWPFStyle(this XElement elem) { Style style = new Style(); if (elem != null) { var setters = elem.Descendants().Select(elm => { Setter setter = null; if (elm.Name == w + "left" || elm.Name == w + "right" || elm.Name == w + "top" || elm.Name == w + "bottom") { ThicknessConverter tk = new ThicknessConverter(); Thickness thinkness = (Thickness)tk.ConvertFrom(elm.Attribute(w + "sz").Value); BrushConverter bc = new BrushConverter(); Brush color = (Brush)bc.ConvertFrom(string.Format("#{0}", elm.Attribute(w + "color").Value)); setter = new Setter(Block.BorderThicknessProperty, thinkness); //style.Setters.Add(new Setter(Block.BorderBrushProperty,color)); } else if (elm.Name == w + "rFonts") { FontFamilyConverter ffc = new FontFamilyConverter(); setter = new Setter(TextElement.FontFamilyProperty, ffc.ConvertFrom(elm.Attribute(w + "ascii").Value)); } else if (elm.Name == w + "b") { setter = new Setter(TextElement.FontWeightProperty, FontWeights.Bold); } else if (elm.Name == w + "color") { BrushConverter bc = new BrushConverter(); setter = new Setter(TextElement.ForegroundProperty, bc.ConvertFrom(string.Format("#{0}", elm.Attribute(w_val).Value))); } else if (elm.Name == w + "em" || elm.Name == w + "i") { setter = new Setter(TextElement.FontStyleProperty, FontStyles.Italic); } else if (elm.Name == w + "strike") { setter = new Setter(Inline.TextDecorationsProperty, TextDecorations.Strikethrough); } else if (elm.Name == w + "sz") { FontSizeConverter fsc = new FontSizeConverter(); setter = new Setter(TextElement.FontSizeProperty, fsc.ConvertFrom(elm.Attribute(w_val).Value)); } else if (elm.Name == w + "ilvl") { Console.WriteLine(elm.Attribute(w_val)); } else if (elm.Name == w + "numPr") { Console.WriteLine(elm.Value); } else if (elm.Name == w + "numId") { Console.WriteLine(elm.Attribute(w_val)); int value = int.Parse(elm.Attribute(w + "val").Value); if (value == 2) { setter = new Setter(List.MarkerStyleProperty, TextMarkerStyle.Decimal); } } else if (elm.Name == w + "u") { setter = new Setter(Inline.TextDecorationsProperty, TextDecorations.Underline); } else if (elm.Name == w + "jc") { TextAlignment textAlignment = new TextAlignment(); string value = elm.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; } setter = new Setter(Block.TextAlignmentProperty, textAlignment); } else { Console.WriteLine(elm.Name); } return(setter); }); foreach (SetterBase setter in setters) { if (setter != null) { style.Setters.Add(setter); } } } return(style); }
/// <summary> /// Convert an ambient light sensor value (in lux) to a corresponding font size /// for the current lighting conditions based on the default font size (stored in the parameter). /// </summary> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // check to see if the incoming value is a double. This value // should be bound to the current light value, in lux. if (value is double) { /* * Approximate table of lux values as they map to human perceived values. * 0 -> 10 Dark * 10 -> 300 Dim Indoor * 300 -> 800 Normal Indoor * 800 -> 10000 Bright Indoor * 10000 -> 30000 Overcast Outdoor * 30000 -> 100000 Direct sunlight */ double lightLevelInLux = (double)value; // check to see if a converter parameter is provided. If it is, that parameter // is the "base" font size value to convert from. if (parameter != null) { double fontSize = 0.0; if (parameter is double) { // if the font size is a double, then simply cast the double to the font size. fontSize = (double)parameter; } else { // see if we can convert from the parameter value, for example the string "12pt" // will convert to a double fontsize value according to the current DPI settings. FontSizeConverter fontConverter = new FontSizeConverter(); if (fontConverter.CanConvertFrom(parameter.GetType())) { fontSize = (double)fontConverter.ConvertFrom(parameter); } else { // convert from a default 12pt font value. fontSize = (double)fontConverter.ConvertFrom("12pt"); } } System.Diagnostics.Debug.Assert(fontSize > 0); // the correlation between light values in lux and human perceived brightness are // not linear. To fit these two values, we need to convert the values from lux // into a linear range from 0.0 <-> 1.0. We'll use that value to scale our font size // up to compensate for the light. // The calculation of Math.Log10(LightSensorProvider.MaximumSensorReportValue) is shown // here for clarity's sake. Since this is a constant value, the calculation can be optimized // into a constant. double lightLevelRangeValue = Math.Log10(lightLevelInLux) / Math.Log10(LightSensorProvider.MaximumSensorReportValue); // now that lightLevelRangeValue is between 0.0 <-> 1.0, we can scale the font size. // since we don't want a zero size font for low light levels, we'll compensate by a constant // value (0.8) such that we will adjust the font size parameter (stored in fontSize) // by 80% to 180% (0.8 -> 1.8); return((FontSizeMultiplierCompensation + lightLevelRangeValue) * fontSize); } } else { System.Diagnostics.Debug.Assert( false, "Binding value is not a double.", "Expect value to be a double specifying the light value in lux."); } return(value); }