表題の通りで MainWindow の <Grid> ~ </Grid> に表示されているデータをそのまま印刷させることは可能でしょうか?そのイメージをどのように取得して印刷できますか?
xaml
1 <Grid> 2 <Grid.ColumnDefinitions> 3 <ColumnDefinition Width="8.2*"/> 4 <ColumnDefinition Width="1.7*"/> 5 <ColumnDefinition Width="0.1*"/> 6 </Grid.ColumnDefinitions> 7 8 <Grid.RowDefinitions> 9 <RowDefinition Height="Auto"/> 10 <RowDefinition Height="Auto"/> 11 <RowDefinition Height="Auto"/> 12 <RowDefinition Height="*"/> 13 </Grid.RowDefinitions> 14 15 <Border BorderBrush="Gray" BorderThickness="1" Grid.Row="0" Grid.Column="0" /> 16 <Border BorderBrush="Gray" BorderThickness="1 0 1 1" Grid.Row="1" Grid.Column="0" /> 17 <Border BorderBrush="Gray" BorderThickness="0 1 1 1" Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Grid.ColumnSpan="2" /> 18 19 <DockPanel Grid.Row="0" Grid.Column="0"> 20 <TextBlock Text="顧客名" Margin="4" FontSize="12"/> 21 <TextBox x:Name="iinCd" Margin="4" Width="200" Text=""/> 22 <TextBlock Text="標本番号" Margin="4" FontSize="12"/> 23 <TextBox x:Name="hyohon" Margin="4" Width="140" Text=""/> 24 <TextBlock Text="" Margin="4" FontSize="12"/> 25 </DockPanel> 26 <DockPanel Grid.Row="1" Grid.Column="0"> 27 <TextBlock Text="患者名" Margin="4" FontSize="12"/> 28 <TextBox x:Name="kanjaMei" Margin="4" Width="150" Text="" /> 29 <TextBlock Text="性別年齢" Margin="4" FontSize="12"/> 30 <TextBox x:Name="ageSex" Margin="4" Width="60" Text=""/> 31 <TextBlock Text="受付日" Margin="4" FontSize="12"/> 32 <TextBox x:Name="uketsukeBi" Margin="4 4 170 4" Width="100" Text="" /> 33 34 </DockPanel> 35 <DockPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"> 36 <StackPanel Orientation="Vertical"> 37 <Image x:Name="BarCode1" Width="160" Height="45" Margin="3" /> 38 <!-- <TextBlock Name="BarText" Margin="4" VerticalAlignment="Bottom" Width="120" FontSize="12" HorizontalAlignment="Center"/>--> 39 </StackPanel> 40 </DockPanel> 41 42 <Canvas Name="canvas1" Grid.Row="2" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="2" Background="Transparent" 43 ContextMenu="{StaticResource Menu1}" MouseDown="canvas1_MouseDown" AllowDrop="True" > 44 </Canvas> 45 </Grid>
印刷方法はどのような形でもいいのですが、例えば現時点で下記のようにして page を作成しなければ・・・と思っています。表示中のビューをページとしてぶっこむ方法などありますか?
C#
1 public static void Print(System.Windows.Controls.Page page, Canvas canvus, string printerMei) 2 { 3 FixedDocument doc = new FixedDocument(); 4 5 FixedPage fp = CreateFixedPage(page, 1); 6 fp.Children.Add(canvus); 7 PageContent pc = new PageContent(); 8 pc.Child = fp; 9 doc.Pages.Add(pc); 10 11 PrintDialog dlg = new System.Windows.Controls.PrintDialog(); 12 // Create a local print server 13 LocalPrintServer ps = new LocalPrintServer(); 14 15 dlg.PrintQueue = ps.GetPrintQueue(printerMei); 16 17 if (dlg.ShowDialog() == true) 18 { 19 System.Windows.Xps.XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(dlg.PrintQueue); 20 21 doc.PrintTicket = dlg.PrintTicket; 22 23 : 24 25 26 public static System.Windows.Size PageSize { get; set; } 27 private static FixedPage CreateFixedPage(Page page, int size) 28 { 29 Action dummy = () => { }; 30 var disp = System.Windows.Threading.Dispatcher.CurrentDispatcher; 31 32 FixedPage fixpage = new FixedPage(); 33 { 34 try 35 { 36 Frame fm = new Frame(); 37 38 fm.Content = page; 39 disp.Invoke(dummy, System.Windows.Threading.DispatcherPriority.Loaded); //バインディングとかいろいろさせる必要がある場合 40 41 FixedPage.SetLeft(fm, 0); 42 FixedPage.SetTop(fm, 0); 43 44 fixpage.Children.Add(fm); 45 46 // Point 1!! XAML の画面幅を指定する。 47 fixpage.Arrange(new Rect(15, 15, PageSize.Height, PageSize.Width)); 48 49 fixpage.Width = page.Width; 50 fixpage.Height = page.Height; 51 52 fixpage.Measure(sz/*PageSize*/); 53 fixpage.Arrange(new Rect(new System.Windows.Point(), sz/*PageSize*/)); 54 fixpage.UpdateLayout(); 55 } 56 catch (Exception ex) 57 { 58 MessageBox.Show(ex.Message); 59 } 60 61 return fixpage; 62 } 63 }
Windows10 WPF C# VS2019
回答1件
あなたの回答
tips
プレビュー