public double MeasureTextSize(string text, double width, double fontSize, string fontName = null) { TextBlock tb = new TextBlock(); if (fontName == null) { fontName = "Segoe UI"; } tb.TextWrapping = TextWrapping.Wrap; tb.Text = text; tb.FontFamily = new Windows.UI.Xaml.Media.FontFamily(fontName); tb.FontSize = fontSize; tb.Measure(new Size(width, Double.PositiveInfinity)); return tb.DesiredSize.Height + 5; }
public TitleBarView() { var settingsService = LazyResolver<ISettingsService>.Service; _textblock = new TextBlock { FontSize = 20, Foreground = new SolidColorBrush(Colors.Black), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(10, 0, 0, 0) }; _redRect = new StackPanel { Height = 60, Width = 60, Background = new SolidColorBrush(Colors.Red) }; _imagecategory = new Image { Height = 60, Width = 60 }; _textblock.Measure(new Size(0, 0)); Height = 60; SetLeft(_textblock, _redRect.Width); SetTop(_textblock, (Height - _textblock.ActualHeight) / 2); Background=new SolidColorBrush(Colors.White); const string sourcelogo = "ms-appx:///Assets/logoIndiaRose.png"; var logo = new Image { Source = new BitmapImage(new Uri(sourcelogo)), Width = 256 }; SetLeft(logo, LazyResolver<IScreenService>.Service.Width - logo.Width); Children.Insert(0, _imagecategory); Children.Insert(1, _textblock); Children.Insert(2, logo); }
private Size InternalGetSize(char c, double fontSize, bool bold, bool italic) { var @event = new AutoResetEvent(false); var size = new Size(); Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { var textBlock = new TextBlock { Text = Convert.ToString(c), FontSize = fontSize, FontFamily = FontFamily, FontStyle = italic ? FontStyle.Italic : FontStyle.Normal, FontWeight = bold ? FontWeights.Bold : FontWeights.Normal }; textBlock.Measure(new Size(1024.0, 1024.0)); size = new Size(textBlock.ActualWidth, textBlock.ActualHeight); @event.Set(); }); @event.WaitOne(); return size; }
/// <summary> /// Generates a new task panel /// </summary> /// <param name="cl">Class to generate it for</param> /// <param name="parent">ListView associated with the class</param> /// <param name="task">Task to add</param> /// <returns>A new ListViewItem for the task</returns> public static ListViewItem GenerateTaskPanel(Class cl, ListView parent, Task task) { #region Containers // Create the content area Grid content = new Grid { Margin = new Thickness(0), Height = 75, Background = MainPage.LightGrayBrush, Tag = "Task" }; // Create the list of content items and a delegate to add them to the list List<UIElement> contentItems = new List<UIElement>(); Action<UIElement> registerItem = i => { contentItems.Add(i); }; // The main ListViewItem ListViewItem panel = new ListViewItem { Background = MainPage.TransparentBrush, Margin = new Thickness(0, 0, 0, 5), Height = 75, Tag = false, // Is Panel Expanded HorizontalContentAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Top, Padding = new Thickness(0), BorderBrush = MainPage.MediumGrayBrush, BorderThickness = new Thickness(1), }; #endregion #region Title // Task title TextBlock title = new TextBlock { Text = task.Title.Trim(), TextAlignment = TextAlignment.Left, FontSize = 25, Foreground = MainPage.BlueBrush, Margin = new Thickness(5, 0, 0, 0) }; // Sizing title.Measure(new Size(0, 0)); title.Arrange(new Rect(0, 0, 0, 0)); registerItem(title); #endregion #region Finish Button // The check mark that marks the task as completed Button finish = new Button { FontFamily = Icons.IconFont, Content = task.Complete == 1 ? Icons.Cancel : Icons.Accept, Margin = new Thickness(title.ActualWidth + 5, 5, 0, 0), HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Background = MainPage.TransparentBrush }; // Remove the task when the button is pressed finish.Tapped += (sender, args) => { TaskList.RemoveTask(cl, parent, panel, task); }; registerItem(finish); #endregion #region Info Box // Info box TextBox info = new TextBox { TextAlignment = TextAlignment.Left, Foreground = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50)), HorizontalAlignment = HorizontalAlignment.Left, TextWrapping = TextWrapping.Wrap, BorderThickness = new Thickness(0), AcceptsReturn = true, Width = parent.Width / 3 + 25, Tag = false, // Edit mode IsReadOnly = true, // Edit mode property Background = MainPage.TransparentBrush, // Edit mode property IsHitTestVisible = false // Edit mode property }; // Add the text - only works if you append each character foreach (char c in task.Info) { info.Text += c.ToString(); } // Border around the info box Border infoBorder = new Border { Child = info, Background = MainPage.MediumGrayBrush, Margin = new Thickness(5, 36, 0, 7), HorizontalAlignment = HorizontalAlignment.Left, BorderThickness = new Thickness(0), Padding = new Thickness(0) }; registerItem(infoBorder); // Edit button for the info box Button edit = new Button { FontFamily = Icons.IconFont, Content = Icons.Edit, Width = 20, Height = 20, FontSize = 10, Padding = new Thickness(0), Background = MainPage.TransparentBrush, VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Left, Margin = new Thickness(parent.Width / 3 + 10, 36, 0, 7) }; // Toggles edit mode for the info box edit.Click += (sender, args) => { if (!(bool) info.Tag) { info.Tag = true; edit.Content = Icons.Save; info.IsReadOnly = false; info.Background = new SolidColorBrush(Colors.White); info.IsHitTestVisible = true; info.Focus(FocusState.Pointer); info.SelectionStart = info.Text.Length; info.SelectionLength = 0; } else { info.Tag = false; edit.Content = Icons.Edit; info.IsReadOnly = true; info.Background = MainPage.MediumGrayBrush; info.IsHitTestVisible = false; DataHandler.EditTask(task.Id, "Info", info.Text); } }; registerItem(edit); #endregion #region Circular Progress Bar // Progress bar background (gray) CircularProgressBar progressBack = new CircularProgressBar { Percentage = 100, SegmentColor = MainPage.MediumGrayBrush, Radius = 30, StrokeThickness = 5, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(0, 2.75, 2.75 + 30, 0) }; registerItem(progressBack); // Progress bar foreground (blue) CircularProgressBar progressFront = new CircularProgressBar { Percentage = 0, SegmentColor = MainPage.BlueBrush, Radius = 30, StrokeThickness = 5, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(0, 2.75, 2.75 + 30, 0), Transitions = new TransitionCollection() }; registerItem(progressFront); // Text inside the progress bar that shows the time remaining TextBlock timeLeft = new TextBlock { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Text = string.Empty, FontSize = 12, Foreground = MainPage.BlueBrush, TextAlignment = TextAlignment.Center }; // Helps with alignment for the progress bar Border border = new Border { BorderBrush = MainPage.BlueBrush, BorderThickness = new Thickness(0), Child = timeLeft, Height = 70, Width = 70, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Margin = progressFront.Margin }; registerItem(border); // Delegate for Action updateProgress = () => { // Clamp the progress to 100% progressFront.Percentage = Math.Min(Util.GetPercentTime(task) * 100, 100); // Finished tasks get an orange color bool finished = false; if (progressFront.Percentage.Equals(100.0)) { finished = true; progressFront.SegmentColor = MainPage.OrangeRedBrush; timeLeft.Foreground = MainPage.OrangeRedBrush; } // Set the new text timeLeft.Text = $"{Util.GetTimeString(task)}{(finished ? " ago" : string.Empty)}"; }; updateProgress(); // Update the progress every second ThreadPoolTimer.CreatePeriodicTimer( async poolTimer => { await progressFront.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { updateProgress(); }); }, new TimeSpan(0, 0, 0, 1)); #endregion #region Assigned/Due Times // Assigned/due info RichTextBlock timeInfo = new RichTextBlock { FontSize = 14, Margin = new Thickness(-5), TextAlignment = TextAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch, TextWrapping = TextWrapping.NoWrap }; // "Assigned on" and "Due on" text // Separated with different colors Run assignedText = new Run { Text = "Assigned: ", Foreground = MainPage.BlueBrush }; Run assignedDate = new Run { Text = $"{task.AssignedOn}", Foreground = MainPage.GreenBrush }; Run dueText = new Run { Text = "Due: ", Foreground = MainPage.BlueBrush }; Run dueDate = new Run { Text = $"{task.DueOn}", Foreground = MainPage.OrangeRedBrush }; // Set up lines Paragraph p1 = new Paragraph(); p1.Inlines.Add(assignedText); p1.Inlines.Add(assignedDate); Paragraph p2 = new Paragraph(); p2.Inlines.Add(dueText); p2.Inlines.Add(dueDate); // Add the info to the text box timeInfo.Blocks.Add(p1); timeInfo.Blocks.Add(p2); // Container for time info Border timeInfoBorder = new Border { Child = timeInfo, BorderBrush = MainPage.BlueBrush, BorderThickness = new Thickness(0), Margin = new Thickness(parent.Width / 3 + 35, 36, 105, 5), Padding = new Thickness(0), VerticalAlignment = VerticalAlignment.Stretch }; registerItem(timeInfoBorder); #endregion #region Expand Button // Button that expands the box Button expand = new Button { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Height = 73, Width = 25, Margin = new Thickness(0, 0, 0, 0), Content = Icons.Down, FontFamily = Icons.IconFont, Padding = new Thickness(0) }; // This makes sure all other task panels collapse smoothly if a new one is opened expand.Tapped += (sender, args) => { // Get the new height based on the tag that tells you if the panel is open or closed int to = !(bool) panel.Tag ? 412 : 75; // Set the new button content to the opposite arrow expand.Content = !(bool) panel.Tag ? Icons.Up : Icons.Down; // If the panel is not opened if (!(bool) panel.Tag) { // For each task that is open and has content foreach (ListViewItem item in parent.Items.Cast<ListViewItem>() .Where(item => (bool?) item.Tag ?? false && item.Content != null)) { // If the grid content is not null and the grid tag is not null if (!((item.Content as Grid)?.Tag as bool? ?? false)) { // Find the button in the grid that has the Up icon foreach (Button btn in (item.Content as Grid).Children.OfType<Button>() .Where(btn => (string) btn.Content == Icons.Up)) { // ... And change it to the down icon btn.Content = Icons.Down; } // Collapse the panel Util.CreateAnimation(150, item, null, 75, "Height").Begin(); // Set the tag to false indicating the panel is closed item.Tag = false; } } } // Once all other panels have been collapsed, expand the new one Util.CreateAnimation(150, panel, null, to, "Height").Begin(); // Set its tag appropriately panel.Tag = !(bool) panel.Tag; }; registerItem(expand); #endregion #region Calendar // Calendar showing date assigned, date due, today CalendarView calendar = new CalendarView { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top, SelectionMode = CalendarViewSelectionMode.None, BlackoutForeground = new SolidColorBrush(Colors.White), Background = MainPage.MediumGrayBrush, CalendarItemBackground = MainPage.TransparentBrush, BorderBrush = MainPage.TransparentBrush, CalendarItemBorderBrush = MainPage.TransparentBrush, OutOfScopeBackground = MainPage.TransparentBrush, OutOfScopeForeground = new SolidColorBrush(Colors.Gray), Margin = new Thickness(parent.Width / 3 + 35, 75, 3, 5), }; // Override item rendering to change colors calendar.CalendarViewDayItemChanging += (sender, args) => { if (args.Item == null) { return; } DateTime day = args.Item.Date.DateTime; if (day.Date == DateTime.Parse(task.AssignedOn.ToString()).Date) { args.Item.Background = MainPage.GreenBrush; args.Item.IsBlackout = true; // Hack to change the foreground color } else if (day.Date == DateTime.Parse(task.DueOn.ToString()).Date) { args.Item.Background = MainPage.OrangeRedBrush; args.Item.IsBlackout = true; } if (day.Date == DateTime.Today) { args.Item.IsBlackout = false; } }; calendar.SetDisplayDate(DateTime.Today); registerItem(calendar); #endregion #region Sizing and Creation // Sizing parent.SizeChanged += (sender, args) => { info.Width = args.NewSize.Width / 3 + 25; edit.Margin = new Thickness(args.NewSize.Width / 3 + 10, 36, 0, 7); calendar.Margin = new Thickness(args.NewSize.Width / 3 + 35, 75, 3, 5); timeInfoBorder.Margin = new Thickness(args.NewSize.Width / 3 + 35, 36, 105, 5); }; panel.SizeChanged += (sender, args) => { content.Height = args.NewSize.Height; }; // Add each element in the contentItems list foreach (UIElement element in contentItems) { content.Children.Add(element); } // Set the container content panel.Content = content; return panel; #endregion }
public void AddRangeCaption(LineConstraintRange range, string caption) { if (_rangeTxtMapping.ContainsKey(range)) return; var txt = new TextBlock(); txt.FontSize = 16; txt.Text = caption; txt.Measure(new Size(1000, 1000)); Canvas.SetLeft(txt, range.Min*Canvas.ActualWidth + ((range.Max - range.Min)*Canvas.ActualWidth - txt.ActualWidth)/2); Canvas.SetTop(txt, 13); _rangeTxtMapping.Add(range, txt); DrawRanges(); }
private string trimHeaderText(string headerText, bool addIcon) { TextBlock textBlock = new TextBlock(); textBlock.Text = headerText; textBlock.FontSize = GRID_HEADER_FONT_SIZE; if (addIcon) { Run text = new Run(); text.Text = " \u25B2"; textBlock.Inlines.Add(text); } textBlock.Measure(new Size(double.MaxValue, double.MaxValue)); if(textBlock.ActualWidth > COLUMN_WIDTH - 10){ while (textBlock.ActualWidth > COLUMN_WIDTH - 10) { headerText = headerText.Remove(headerText.Length - 1).Trim(); textBlock.Text = String.Concat(headerText, "..."); if (addIcon) { Run text = new Run(); text.Text = " \u25B2"; textBlock.Inlines.Add(text); } textBlock.Measure(new Size(double.MaxValue, double.MaxValue)); } headerText = String.Concat(headerText, "..."); } return headerText; }
private double GetOWidth(ITextRenderAttributeState attributes, TextBlock r) { var key = GetCacheKey(new TextRenderCommand(attributes, new TextRenderTextContent("o"))); if (!OWidthCache.ContainsKey(key)) { var s2 = r.Text; r.Text = "o"; r.Measure(new Size(0, 0)); OWidthCache[key] = r.ActualWidth; r.Text = s2; } return OWidthCache[key]; }
/// <summary> /// The draw text. /// </summary> /// <param name="p"> /// The p. /// </param> /// <param name="text"> /// The text. /// </param> /// <param name="fill"> /// The fill. /// </param> /// <param name="fontFamily"> /// The font family. /// </param> /// <param name="fontSize"> /// The font size. /// </param> /// <param name="fontWeight"> /// The font weight. /// </param> /// <param name="rotate"> /// The rotate. /// </param> /// <param name="halign"> /// The horizontal alignment. /// </param> /// <param name="valign"> /// The vertical alignment. /// </param> /// <param name="maxSize"> /// The maximum size of the text. /// </param> public void DrawText( ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, OxyPlot.HorizontalAlignment halign, OxyPlot.VerticalAlignment valign, OxySize? maxSize) { var tb = new TextBlock { Text = text, Foreground = fill.ToBrush() }; // tb.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Animated); if (fontFamily != null) { tb.FontFamily = new FontFamily(fontFamily); } if (fontSize > 0) { tb.FontSize = fontSize; } tb.FontWeight = GetFontWeight(fontWeight); tb.Measure(new Size(1000, 1000)); double dx = 0; if (halign == OxyPlot.HorizontalAlignment.Center) { dx = -tb.ActualWidth / 2; } if (halign == OxyPlot.HorizontalAlignment.Right) { dx = -tb.ActualWidth; } double dy = 0; if (valign == OxyPlot.VerticalAlignment.Middle) { dy = -tb.ActualHeight / 2; } if (valign == OxyPlot.VerticalAlignment.Bottom) { dy = -tb.ActualHeight; } var transform = new TransformGroup(); transform.Children.Add(new TranslateTransform { X = (int)dx, Y = (int)dy }); if (!rotate.Equals(0.0)) { transform.Children.Add(new RotateTransform { Angle = rotate }); } transform.Children.Add(new TranslateTransform { X = (int)p.X, Y = (int)p.Y }); tb.RenderTransform = transform; if (this.clip.HasValue) { // add a clipping container that is not rotated var c = new Canvas(); c.Children.Add(tb); this.Add(c); } else { this.Add(tb); } }
private void DrawLabels(TimeSeries ts) { Title.Text = ts.MetaData.Name; double priceOpen = ts.Values.First(); double priceClose = ts.Values.Last(); Value.Text = " " + ts.Values.Last(); var delta = (float)Math.Round(priceClose - priceOpen, 3); bool isNegative = delta < 0; Growth.Text = " " + (!isNegative ? "+" : "") + delta; GrowthRate.Text = " " + (!isNegative ? "+" : "") + Math.Round(delta / priceOpen, 3) * 100 + "%"; Growth.Foreground = new SolidColorBrush(GetTrendColor(delta)); GrowthRate.Foreground = new SolidColorBrush(GetTrendColor(delta)); var labelOpen = new TextBlock(); labelOpen.FontSize = 16; labelOpen.Text = ts.MetaData.Min.ToString(); xCanvas.Children.Add(labelOpen); labelOpen.Measure(new Size(int.MaxValue, int.MaxValue)); Canvas.SetTop(labelOpen, Height - 16); Canvas.SetLeft(labelOpen, -labelOpen.ActualWidth - 7); var labelClose = new TextBlock(); labelClose.FontSize = 16; labelClose.Text = ts.MetaData.Max.ToString(); xCanvas.Children.Add(labelClose); labelClose.Measure(new Size(int.MaxValue, int.MaxValue)); Canvas.SetTop(labelClose, 2); Canvas.SetLeft(labelClose, -labelClose.ActualWidth - 7); var labelStartDate = new TextBlock(); labelStartDate.FontSize = 16; labelStartDate.Text = "1"; xCanvas.Children.Add(labelStartDate); Canvas.SetTop(labelStartDate, Height + 7); Canvas.SetLeft(labelStartDate, 0); var labelEndDate = new TextBlock(); labelEndDate.FontSize = 16; labelEndDate.Text = ts.Values.Count.ToString(); labelEndDate.Measure(new Size(int.MaxValue, int.MaxValue)); xCanvas.Children.Add(labelEndDate); Canvas.SetTop(labelEndDate, Height + 7); Canvas.SetLeft(labelEndDate, Width - labelEndDate.ActualWidth); }
private double BlockHeightOf( TextBlock t ) { t.Measure( new Size( double.PositiveInfinity, double.PositiveInfinity ) ); t.Arrange( new Rect( new Point(), t.DesiredSize ) ); /* INTENSIVE_LOG if( t.FontSize == 16 ) { Logger.Log( ID, string.Format( "FontSize 16 detected {0}, Should be {1}. Text {2}", t.FontSize, FontSize, t.Text ), LogType.DEBUG ); } //*/ return t.ActualHeight; }
public void DrawLoadingMessage() { WaveCanvas.Children.Clear(); double w = WaveCanvas.ActualWidth; double h = WaveCanvas.ActualHeight; TextBlock tbx = new TextBlock(); tbx.FontSize = 16; tbx.Text = "Loading " + Utterance.AudioFile.Name; tbx.HorizontalAlignment = HorizontalAlignment.Center; tbx.VerticalAlignment = VerticalAlignment.Center; tbx.Foreground = new SolidColorBrush(Windows.UI.Colors.White); WaveCanvas.Children.Add(tbx); tbx.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); double tw = tbx.DesiredSize.Width; double th = tbx.DesiredSize.Height; Canvas.SetLeft(tbx, (w - tw) / 2.0); Canvas.SetTop(tbx, (h - th) / 2.0); tbx.Visibility = Visibility.Visible; }
private void DrawGraph(TimeSeries timeSeriesData, TimeSeries ts) { double width = ActualWidth; double height = ActualHeight; TimeSeries tsn = ts.Clone(); tsn.Normalize(); DrawLine(0, 0, width, 0, 1f); DrawLine(width, 0, width, height, 1f); DrawLine(width, height, 0, height, 1f); DrawLine(0, height, 0, 0, 1f); int numLines = 3; float gap = (float) height/numLines; for (int i = 1; i < numLines; ++i) { DrawLine(0, (int) (i*gap), width, (int) (i*gap), 0.5f); } IList<double> values = tsn.Values; float w = ((float) width - 40)/values.Count; float h = (float) height - 40; for (int i = 1; i < values.Count; ++i) { var line = new Line { X1 = 20 + (i - 1)*w, Y1 = 20 + (1 - values[i - 1])*h, X2 = 20 + i*w, Y2 = 20 + (1 - values[i])*h, StrokeThickness = 4, StrokeEndLineCap = PenLineCap.Round, Stroke = new SolidColorBrush(Colors.White) }; LineGraphCanvas.Children.Add(line); } MathLine trend = tsn.GetTrend(); var trendline = new Line { X1 = 20 + 0, Y1 = 20 + (1 - trend.GetY(0))*h, X2 = 20 + (values.Count - 1)*w, Y2 = 20 + (1 - trend.GetY(values.Count - 1))*h, StrokeThickness = 2.5, Stroke = new SolidColorBrush(GetTrendColor(trend.B)) }; LineGraphCanvas.Children.Add(trendline); Title.Text = timeSeriesData.Meta.Name; double priceOpen = ts.Values.First(); double priceClose = ts.Values.Last(); Value.Text = " " + ts.Values.Last(); var delta = (float) Math.Round(priceClose - priceOpen, 3); bool isNegative = delta < 0; Growth.Text = " " + (!isNegative ? "+" : "") + delta; GrowthRate.Text = " " + (!isNegative ? "+" : "") + Math.Round(delta/priceOpen, 3)*100 + "%"; Growth.Foreground = new SolidColorBrush(GetTrendColor(delta)); GrowthRate.Foreground = new SolidColorBrush(GetTrendColor(delta)); var labelOpen = new TextBlock(); labelOpen.FontSize = 16; labelOpen.Text = ts.Meta.Min.ToString(); LineGraphCanvas.Children.Add(labelOpen); labelOpen.Measure(new Size(int.MaxValue, int.MaxValue)); Canvas.SetTop(labelOpen, height - 16); Canvas.SetLeft(labelOpen, -labelOpen.ActualWidth - 7); var labelClose = new TextBlock(); labelClose.FontSize = 16; labelClose.Text = ts.Meta.Max.ToString(); LineGraphCanvas.Children.Add(labelClose); labelClose.Measure(new Size(int.MaxValue, int.MaxValue)); Canvas.SetTop(labelClose, 2); Canvas.SetLeft(labelClose, -labelClose.ActualWidth - 7); var labelStartDate = new TextBlock(); labelStartDate.FontSize = 16; labelStartDate.Text = "1"; LineGraphCanvas.Children.Add(labelStartDate); Canvas.SetTop(labelStartDate, height + 7); Canvas.SetLeft(labelStartDate, 0); var labelEndDate = new TextBlock(); labelEndDate.FontSize = 16; labelEndDate.Text = timeSeriesData.Values.Count.ToString(); labelEndDate.Measure(new Size(int.MaxValue, int.MaxValue)); LineGraphCanvas.Children.Add(labelEndDate); Canvas.SetTop(labelEndDate, height + 7); Canvas.SetLeft(labelEndDate, width - labelEndDate.ActualWidth); }
private void temp() { MainGrid.Children.Clear(); MainGrid.ColumnDefinitions.Clear(); MainGrid.RowDefinitions.Clear(); MainGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(CellWidth) }); MainGrid.RowDefinitions.Add(new RowDefinition()); TextBlock block = null; for (int x = 0; x < Columns.Count + 1; x++) { for (int y = 0; y < Rows.Count + 1; y++) { Rectangle rect = new Rectangle() { Fill = ((y) / 2 == (double)(y) / 2.0) ? new SolidColorBrush(Color.FromArgb(255, 56, 56, 56)) : new SolidColorBrush(Color.FromArgb(255, 64, 64, 64)), Margin = new Thickness(0,0,1,0) }; Grid.SetColumn(rect, x); Grid.SetRow(rect, y); MainGrid.Children.Add(rect); if (x == 0) { if (y > 0) { string rowTitle = Rows[y - 1]; MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(CellHeight) }); bool sizeFound = false; double candidateWidth = MainGrid.ColumnDefinitions[0].Width.Value; double candidateHeight; while (!sizeFound) { block = new TextBlock() { Text = rowTitle, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, TextWrapping = TextWrapping.WrapWholeWords, FontSize = 8, Margin = new Thickness(CellMargin) }; block.Measure(new Size(candidateWidth - CellMargin * 2, CellHeight * 2)); candidateHeight = block.ActualHeight; block.Width = CellWidth; if (candidateHeight <= CellHeight - CellMargin * 2) sizeFound = true; else candidateWidth += 5; } block = new TextBlock() { Text = rowTitle, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, TextWrapping = TextWrapping.WrapWholeWords, FontSize = 8, Margin = new Thickness(CellMargin) }; MainGrid.ColumnDefinitions[0].Width = new GridLength(candidateWidth); Grid.SetRow(block, y); MainGrid.Children.Add(block); } } } if (x > 0) { string columnTitle = Columns[x - 1]; MainGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(CellWidth) }); block = new TextBlock() { Text = columnTitle, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, TextWrapping = TextWrapping.WrapWholeWords, FontSize = 8, Margin = new Thickness(CellMargin) }; Grid.SetColumn(block, x); MainGrid.Children.Add(block); } } }
/// <summary> /// Создать элемент. /// </summary> /// <param name="command">Команда.</param> /// <returns>Элемент.</returns> public FrameworkElement Create(ITextRenderCommand command) { count++; string text; var textCnt = command.Content as ITextRenderTextContent; if (textCnt != null) { text = textCnt.Text ?? ""; } else { text = ""; } var r = new TextBlock() { Foreground = Application.Current.Resources["PostNormalTextBrush"] as Brush, TextWrapping = TextWrapping.NoWrap, TextTrimming = TextTrimming.None, FontSize = StyleManager.Text.PostFontSize, TextLineBounds = TextLineBounds.Full, IsTextSelectionEnabled = false, TextAlignment = TextAlignment.Left }; FrameworkElement result = r; Border b = new Border(); bool needBorder = false; Grid g = new Grid(); bool needGrid = false; Grid g2 = new Grid(); bool needOuterGrid = false; if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Bold)) { r.FontWeight = FontWeights.Bold; } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Italic)) { r.FontStyle = FontStyle.Italic; } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Fixed)) { r.FontFamily = new FontFamily("Courier New"); } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Spoiler)) { needBorder = true; b.Background = Application.Current.Resources["PostSpoilerBackgroundBrush"] as Brush; r.Foreground = Application.Current.Resources["PostSpoilerTextBrush"] as Brush; } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Quote)) { r.Foreground = Application.Current.Resources["PostQuoteTextBrush"] as Brush; } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Link)) { r.Foreground = Application.Current.Resources["PostLinkTextBrush"] as Brush; } b.BorderBrush = r.Foreground; b.BorderThickness = new Thickness(0); if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Undeline) || command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Link)) { needBorder = true; b.BorderThickness = new Thickness(b.BorderThickness.Left, b.BorderThickness.Top, b.BorderThickness.Right, 1.2); } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Overline)) { needBorder = true; b.BorderThickness = new Thickness(b.BorderThickness.Left, 1.2, b.BorderThickness.Right, b.BorderThickness.Bottom); } Border strikeBorder = null; if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Strikethrough)) { needGrid = true; strikeBorder = new Border() { Background = r.Foreground, Height = 1.2, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Top, }; g.Children.Add(strikeBorder); } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Subscript) || command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Superscript)) { needOuterGrid = true; r.Measure(new Size(0, 0)); var fh = r.ActualHeight; r.FontSize = r.FontSize * 2.0 / 3.0; r.Measure(new Size(0, 0)); var fh2 = r.ActualHeight; var delta = fh - fh2; if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Subscript) && !command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Superscript)) { g2.Padding = new Thickness(0, delta, 0, 0); } else if (!command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Subscript) && command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Superscript)) { g2.Padding = new Thickness(0, 0, 0, delta); } else { g2.Padding = new Thickness(0, delta / 2, 0, delta / 2); } } var spaceWidth = GetOWidth(command.Attributes, r); int endSpaces = 0; var s2 = text; while (s2.EndsWith(" ")) { endSpaces++; s2 = s2.Substring(0, s2.Length - 1); } r.Text = s2; r.Measure(new Size(0, 0)); r.Height = r.ActualHeight; r.Width = r.ActualWidth + endSpaces * spaceWidth; if (strikeBorder != null) { var oh = r.ActualHeight; const double koef = 0.6; strikeBorder.Margin = new Thickness(0, koef * oh, 0, 0); } if (needGrid) { g.Height = result.Height; g.Width = result.Width; g.Children.Add(result); result = g; } if (needBorder) { b.Height = result.Height; b.Width = result.Width; b.Child = result; result = b; } if (needOuterGrid) { g2.Height = result.Height + g2.Padding.Bottom + g2.Padding.Top; g2.Width = result.Width; g2.Children.Add(result); result = g2; } if (command.Attributes.Attributes.ContainsKey(CommonTextRenderAttributes.Link)) { var linkAttribute = command.Attributes.Attributes[CommonTextRenderAttributes.Link] as ITextRenderLinkAttribute; if (linkAttribute != null) { RenderLinkClickHelper.SetupLinkActions(result, linkAttribute, linkClickCallback); } } return result; }
private void DrawGraph() { Line line; for (double xx = 0; xx < width; xx += zoom) { double x = xx + (width / 2 - xOffset); double x2 = (width / 2 - xOffset) - xx; line = new Line() { X1 = x, Y1 = -height / 2 + yOffset, X2 = x, Y2 = height + height / 2 + yOffset, StrokeThickness = 1, Stroke = new SolidColorBrush(Colors.Gray), Opacity = 0.3 }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); line = new Line() { X1 = x2, Y1 = -height / 2 + yOffset, X2 = x2, Y2 = height + height / 2 + yOffset, StrokeThickness = 1, Stroke = new SolidColorBrush(Colors.Gray), Opacity = 0.3 }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); if (xx / zoom != 0) { TextBlock textBlock = new TextBlock(); textBlock.Text = "" + (-xx / zoom); textBlock.Foreground = new SolidColorBrush(Colors.DarkTurquoise); textBlock.FontSize = 20; textBlock.Measure(new Size(10, 10)); Canvas.SetTop(textBlock, height / 2 + yOffset - textBlock.ActualHeight + 10); Canvas.SetLeft(textBlock, x + textBlock.ActualWidth / 2); textBlock.RenderTransform = new RotateTransform(); ((RotateTransform)textBlock.RenderTransform).Angle = 180; this.graphElements.Add(textBlock); this.GraphCanvas.Children.Add(textBlock); textBlock = new TextBlock(); textBlock.Text = "" + (xx / zoom); textBlock.Foreground = new SolidColorBrush(Colors.DarkTurquoise); textBlock.FontSize = 20; textBlock.Measure(new Size(10, 10)); Canvas.SetTop(textBlock, height / 2 + yOffset - textBlock.ActualHeight + 10); Canvas.SetLeft(textBlock, x2 + textBlock.ActualWidth / 2); textBlock.RenderTransform = new RotateTransform(); ((RotateTransform)textBlock.RenderTransform).Angle = 180; this.graphElements.Add(textBlock); this.GraphCanvas.Children.Add(textBlock); } } for (double yy = 0; yy < height; yy += zoom) { double y = yy + (height / 2 + yOffset); double y2 = (height / 2 + yOffset) - yy; line = new Line() { X1 = -width / 2 - xOffset, Y1 = y, X2 = width + width / 2 - xOffset, Y2 = y, StrokeThickness = 1, Stroke = new SolidColorBrush(Colors.Gray), Opacity = 0.3 }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); line = new Line() { X1 = -width / 2 - xOffset, Y1 = y2, X2 = width + width / 2 - xOffset, Y2 = y2, StrokeThickness = 1, Stroke = new SolidColorBrush(Colors.Gray), Opacity = 0.3 }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); if (yy / zoom != 0) { TextBlock textBlock = new TextBlock(); textBlock.Text = "" + (yy / zoom); textBlock.Foreground = new SolidColorBrush(Colors.DarkTurquoise); textBlock.FontSize = 20; textBlock.Measure(new Size(10, 10)); Canvas.SetTop(textBlock, y + textBlock.ActualHeight / 2); Canvas.SetLeft(textBlock, width / 2 - xOffset + textBlock.ActualWidth + 10); textBlock.RenderTransform = new RotateTransform(); ((RotateTransform)textBlock.RenderTransform).Angle = 180; this.graphElements.Add(textBlock); this.GraphCanvas.Children.Add(textBlock); textBlock = new TextBlock(); textBlock.Text = "" + (-yy / zoom); textBlock.Foreground = new SolidColorBrush(Colors.DarkTurquoise); textBlock.FontSize = 20; textBlock.Measure(new Size(10, 10)); Canvas.SetTop(textBlock, y2 + textBlock.ActualHeight / 2); Canvas.SetLeft(textBlock, width / 2 - xOffset + textBlock.ActualWidth + 10); textBlock.RenderTransform = new RotateTransform(); ((RotateTransform)textBlock.RenderTransform).Angle = 180; this.graphElements.Add(textBlock); this.GraphCanvas.Children.Add(textBlock); } } line = new Line() { X1 = -width / 2 - xOffset, Y1 = height / 2 + yOffset, X2 = width + width / 2 - xOffset, Y2 = height / 2 + yOffset, StrokeThickness = thickness, Stroke = new SolidColorBrush(Colors.DarkTurquoise) }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); line = new Line() { X1 = width / 2 - xOffset, Y1 = -height / 2 + yOffset, X2 = width / 2 - xOffset, Y2 = height + height / 2 + yOffset, StrokeThickness = thickness, Stroke = new SolidColorBrush(Colors.DarkTurquoise) }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); sample = zoom / width; double lastX = 0; double lastY = 0; Debug.Text = ""; //Draw equation line for (double xi = -20; xi <= 20; xi += 0.01) { double x = xi; double y = 0; String yString = equation.SolveEquationDouble(-x); //Debug.Text += "X: " + x + ", Y: " + yString + "\n"; if (!double.TryParse(yString, out y)) { Debug.Text = yString; return; } x *= zoom; y *= zoom; x += width / 2 - xOffset; y += height / 2 + yOffset; if (firstPass) { lastX = x; lastY = y; firstPass = false; } line = new Line() { X1 = lastX, Y1 = lastY, X2 = x, Y2 = y, StrokeThickness = thickness, Stroke = new SolidColorBrush(Colors.LimeGreen) }; this.graphElements.Add(line); this.GraphCanvas.Children.Add(line); lastX = x; lastY = y; } }
private Size GetTextSize(double availableWidth, double fontSize, TextWrapping textWrapping) { var block = new TextBlock { Text = $"{this.Text}", TextAlignment = this.TextAlignment, FontSize = fontSize, TextWrapping = textWrapping }; block.Measure(new Size(availableWidth, Double.PositiveInfinity)); return block.DesiredSize; }
/// <summary> /// Draws the meter, running all necessary math operations and making sure that pixels appear /// </summary> public void Draw() { Path.Radius = Diameter / 2; Path.Intervals = Intervals; Path.MeterStartValue = MeterStartValue; Path.MeterEndValue = MeterEndValue; Path.MeterRadius = MeterRadius; // All dep props are checked for null Path.StartAngle = (double)(StartAngle * (Math.PI / 180)); Path.LabelOffset = 10; Path.Draw(); // Set the meter arc if (Path.MeterTickPoints.Count > 0) { MeterStartPoint = Path.MeterTickPoints[0].Point; MeterEndPoint = Path.MeterTickPoints[Path.MeterTickPoints.Count - 1].Point; } foreach (var tick in Path.MeterTickPoints) { var tickLabel = new TextBlock { Text = tick.Value.ToString(CultureInfo.CurrentCulture), FontSize = 8 }; tickLabel.Measure(new Size()); LayoutRoot.Children.Add(tickLabel); Canvas.SetTop(tickLabel, tick.LabelPoint.Y - (tickLabel.ActualHeight / 2)); Canvas.SetLeft(tickLabel, tick.LabelPoint.X - (tickLabel.ActualWidth / 2)); Canvas.SetZIndex(tickLabel, 100); } }
/// <summary> /// Measures the text. /// </summary> /// <param name="text"> /// The text. /// </param> /// <param name="fontFamily"> /// The font family. /// </param> /// <param name="fontSize"> /// Size of the font. /// </param> /// <param name="fontWeight"> /// The font weight. /// </param> /// <returns> /// The text size. /// </returns> public OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text)) { return OxySize.Empty; } var tb = new TextBlock { Text = text }; if (fontFamily != null) { tb.FontFamily = new FontFamily(fontFamily); } if (fontSize > 0) { tb.FontSize = fontSize; } tb.FontWeight = GetFontWeight(fontWeight); tb.Measure(new Size(1000, 1000)); return new OxySize(tb.ActualWidth, tb.ActualHeight); }
private void DrawLabels() { for (int i = 0; i < _numPoints; ++i) { var label = new TextBlock { Text = _availableSlices[i].Label, FontSize = 16 }; float angle = i*(float) (Math.PI*2)/_numPoints; int radius = (int) OuterCirlce.Width/2; double pointX = _center.X + Math.Cos(angle)*radius; double pointY = _center.Y - Math.Sin(angle)*radius; var center = new Vector2(_center.X, _center.Y); var point = new Vector2(pointX, pointY); Vector2 np = center + (point - center)*1.3; label.Measure(new Size(int.MaxValue, int.MaxValue)); np.X -= label.ActualWidth/2 + (np.Y == _center.Y ? -22 : 10); np.Y -= label.ActualHeight/2; Canvas.SetLeft(label, np.X); Canvas.SetTop(label, np.Y); Canvas.Children.Add(label); } }