public static ImageSource GetNewMessagesNotificationOverlay(Window window, DataTemplate template, int count = 0) { if (window == null) return null; var presentation = PresentationSource.FromVisual(window); if (presentation == null) return null; Matrix m = presentation.CompositionTarget.TransformToDevice; double dx = m.M11; double dy = m.M22; double iconWidth = 16.0 * dx; double iconHeight = 16.0 * dy; string countText = count.ToString(); RenderTargetBitmap bmp = new RenderTargetBitmap((int) iconWidth, (int) iconHeight, 96, 96, PixelFormats.Default); ContentControl root = new ContentControl { ContentTemplate = template, Content = count > 99 ? "…" : countText }; root.Arrange(new Rect(0, 0, iconWidth, iconHeight)); bmp.Render(root); return bmp; }
private static void OnPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var taskbarItemInfo = (TaskbarItemInfo) dependencyObject; object content = GetContent(taskbarItemInfo); DataTemplate template = GetTemplate(taskbarItemInfo); if (template == null || content == null) { taskbarItemInfo.Overlay = null; return; } const int ICON_WIDTH = 32; const int ICON_HEIGHT = 32; var bmp = new RenderTargetBitmap(ICON_WIDTH, ICON_HEIGHT, 96, 96, PixelFormats.Default); var root = new ContentControl { ContentTemplate = template, Content = content }; root.Arrange(new Rect(0, 0, ICON_WIDTH, ICON_HEIGHT)); bmp.Render(root); taskbarItemInfo.Overlay = bmp; }
public PageBuilder(double width, double height, int marginsLeft, int marginsTop, int marginsRight, int marginsBottom, ContentControl frame) { _page = new PageContent(); _fixedPage = new FixedPage {Background = Brushes.White, Width = width, Height = height}; _repeater = new Repeater(); var repeatContainer = new Grid {Margin = new Thickness(marginsLeft, marginsTop, marginsRight, marginsBottom)}; repeatContainer.Children.Add(_repeater); frame.SetValue(FixedPage.LeftProperty, 0.00); frame.SetValue(FixedPage.TopProperty, 0.00); frame.SetValue(FrameworkElement.WidthProperty, _fixedPage.Width); frame.SetValue(FrameworkElement.HeightProperty, _fixedPage.Height); _fixedPage.Children.Add(frame); ((IAddChild)_page).AddChild(_fixedPage); frame.Content = repeatContainer; frame.Measure(new Size(width, height)); frame.Arrange(new Rect(0, 0, width, height)); _repeater.Width = repeatContainer.ActualWidth; _repeater.Height = repeatContainer.ActualHeight; }
/// <summary> /// Renders the vector icon with the specified resource key, as an image of the specified size, and returns an URI for tile icons. /// </summary> /// <remarks> /// The key is for a ControlTemplate, it's the easiest way to store a path in resources. /// </remarks> private static Uri RenderVector( string templateKey, double size ) { size = Math.Round( size ); string fileName = string.Format( "Shared/ShellContent/{0}_{1}.png", templateKey, size ); var control = new ContentControl(); control.Template = (ControlTemplate) Application.Current.Resources[templateKey]; control.Measure( new Size( size, size ) ); control.Arrange( new Rect( 0, 0, size, size ) ); var bitmap = new WriteableBitmap( (int) size, (int) size ); bitmap.Render( control, null ); bitmap.Invalidate(); using ( var store = IsolatedStorageFile.GetUserStoreForApplication() ) { if ( store.FileExists( fileName ) ) { store.DeleteFile( fileName ); } using ( var stream = store.CreateFile( fileName ) ) { new PngWriter( stream, bitmap ).Write(); } } return new Uri( "isostore:/" + fileName, UriKind.Absolute ); }
protected override Size ArrangeOverride(Size finalSize) { _contentControl.Arrange(new Rect(finalSize)); return(finalSize); }
/// <summary> /// Called when the task bar overlay property is changed. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void OnTaskbarOverlayPropertyChanged( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { Window window = (Window)dependencyObject; if ((window.TaskbarOverlay == null) && (window.TaskbarOverlayTemplate == null)) { window.GetTaskbarItemInfoSafely().Overlay = null; } else if ((window.TaskbarOverlay != null) && (window.TaskbarOverlay is ImageSource) && (window.TaskbarOverlayTemplate == null)) { window.GetTaskbarItemInfoSafely().Overlay = (ImageSource)window.TaskbarOverlay; } RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap( OVERLAY_ICON_WIDTH, OVERLAY_ICON_HEIGHT, 96, 96, PixelFormats.Default); ContentControl contentControl = new ContentControl() { Content = window.TaskbarOverlay, ContentTemplate = window.TaskbarOverlayTemplate }; contentControl.Arrange(new Rect(0, 0, OVERLAY_ICON_WIDTH, OVERLAY_ICON_HEIGHT)); renderTargetBitmap.Render(contentControl); window.GetTaskbarItemInfoSafely().Overlay = renderTargetBitmap; }