Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //// MainWindow.xaml.cs
- using System.Windows;
- using System.Windows.Controls;
- namespace WpfApplication1
- {
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- var pd = new PrintDialog();
- pd.PrintVisual(myVisual, "Test print");
- }
- }
- }
- //// MainWindow.xaml
- <Window x:Class="WpfApplication1.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:WpfApplication1"
- mc:Ignorable="d"
- Title="MainWindow" Height="350" Width="525">
- <StackPanel>
- <Button Click="Button_Click" Height="20">click to print</Button>
- <local:MyVisual x:Name="myVisual" Width="100" Height="100"/>
- </StackPanel>
- </Window>
- //// MyVisual.cs
- using System.Globalization;
- using System.Windows;
- using System.Windows.Media;
- namespace WpfApplication1
- {
- public class MyVisual : FrameworkElement
- {
- private static Typeface _tf = new Typeface("Carlito");
- private VisualCollection _visuals;
- public MyVisual()
- {
- _visuals = new VisualCollection(this);
- Loaded += MyVisual_Loaded;
- }
- protected override Visual GetVisualChild(int index) => _visuals[index];
- protected override int VisualChildrenCount => _visuals.Count;
- private void MyVisual_Loaded(object sender, RoutedEventArgs e)
- {
- var visual = new DrawingVisual();
- using (DrawingContext dc = visual.RenderOpen())
- {
- var someText = new FormattedText("Delightful", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, _tf, 52, Brushes.Black);
- dc.DrawText(someText, new Point(0, 0));
- }
- _visuals.Add(visual);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement