Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //// MainWindow.xaml.cs
  2.  
  3. using System.Windows;
  4. using System.Windows.Controls;
  5.  
  6. namespace WpfApplication1
  7. {
  8.     public partial class MainWindow : Window
  9.     {
  10.         public MainWindow()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.  
  15.         private void Button_Click(object sender, RoutedEventArgs e)
  16.         {
  17.             var pd = new PrintDialog();
  18.             pd.PrintVisual(myVisual, "Test print");
  19.         }
  20.     }
  21. }
  22.  
  23. //// MainWindow.xaml
  24.  
  25. <Window x:Class="WpfApplication1.MainWindow"
  26.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  27.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  28.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  29.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  30.         xmlns:local="clr-namespace:WpfApplication1"
  31.         mc:Ignorable="d"
  32.         Title="MainWindow" Height="350" Width="525">
  33.     <StackPanel>
  34.         <Button Click="Button_Click" Height="20">click to print</Button>
  35.         <local:MyVisual x:Name="myVisual" Width="100" Height="100"/>
  36.     </StackPanel>
  37. </Window>
  38.  
  39. //// MyVisual.cs
  40. using System.Globalization;
  41. using System.Windows;
  42. using System.Windows.Media;
  43.  
  44. namespace WpfApplication1
  45. {
  46.     public class MyVisual : FrameworkElement
  47.     {
  48.         private static Typeface _tf = new Typeface("Carlito");
  49.         private VisualCollection _visuals;
  50.  
  51.         public MyVisual()
  52.         {
  53.             _visuals = new VisualCollection(this);
  54.             Loaded += MyVisual_Loaded;
  55.         }
  56.  
  57.         protected override Visual GetVisualChild(int index) => _visuals[index];
  58.         protected override int VisualChildrenCount => _visuals.Count;
  59.  
  60.         private void MyVisual_Loaded(object sender, RoutedEventArgs e)
  61.         {
  62.             var visual = new DrawingVisual();
  63.             using (DrawingContext dc = visual.RenderOpen())
  64.             {
  65.                 var someText = new FormattedText("Delightful", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, _tf, 52, Brushes.Black);
  66.                 dc.DrawText(someText, new Point(0, 0));
  67.             }
  68.             _visuals.Add(visual);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement