2014-07-04 53 views
-1

如何在打印完整窗口之前为其打印预览功能,这是我用于打印整个窗口的代码。如何在WPF中打印预览,获取完整的窗口?

我的背后代码:

private void Canvas_Print_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      PrintDialog printdlg = new PrintDialog(); 
      MainWindow win = new MainWindow(); 
      this.Width = 350; 
      this.Background = Brushes.White; 
      panel.ScrollToTop(); 
      panel.ScrollToLeftEnd(); 
      win.Tag = this; 
      if (printdlg.ShowDialog() == true) 
      { 
       printdlg.PrintVisual(panel.Content as Visual, "MyApplication"); 
      } 
     } 
+0

你可以使用视觉刷。 – pushpraj

+0

但Visual Brush是用于像位图这样的图像在这种情况下它将如何有用 – Krrish

+0

还有很多不同之处。没有内置的功能。一种选择是使用RenderTargetBitmap并创建当前MainWindow的截图(图像)并将其显示在控件中。我会建议谷歌搜索不同的选项。 – Kcvin

回答

0

这里是打印预览种功能

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="auto" /> 
    </Grid.RowDefinitions> 
    <Viewbox> 
     <Border BorderBrush="Gray" 
       Background="White" 
       BorderThickness="2" 
       Margin=".2in"> 
      <Rectangle Width="8.5in" 
         Height="11in" 
         Margin=".2in"> 
       <Rectangle.Fill> 
        <VisualBrush Visual="{Binding ElementName=visualToPrint}" 
           Stretch="Uniform" /> 
       </Rectangle.Fill> 
      </Rectangle> 
      <Border.Effect> 
       <DropShadowEffect Opacity=".25" /> 
      </Border.Effect> 
     </Border> 
    </Viewbox> 
    <TextBox Text="change this text" 
      Grid.Row="1" 
      x:Name="visualToPrint" /> 
</Grid> 

我创建的纸的大小的矩形(假设8.5" 11" )的样品并设置填充到VisualBrush这是呈现我的视觉visualToPrint(面板在你的情况)。

也包装成像效果,整个页面边框为ViewBox,使调整

结果

result

我选择了一个文本框,例如,你可以选择任何视觉您所选择的在视觉刷,例如Visual="{Binding ElementName=panel}"