2014-07-04 59 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}"