回答編集履歴
4
VisualBrush
answer
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# 追記
|
2
|
+
`VisualBrush`を使用するほうがスマートだったかもしれません。
|
3
|
+
[WPFでプリントしたい](https://teratail.com/questions/48084#reply-h65xhlz4tdgqrt)
|
4
|
+
|
5
|
+
---
|
6
|
+
|
1
7
|
注)プリンタが手元にないので未確認です。
|
2
8
|
|
3
9
|
そのエラーはすでにウィンドウ等にAddされているコントロール(`canvas1`)を、別のものにAddしようとしたためです。
|
3
見直しキャンペーン中
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
注)プリンタが手元にないので未確認です。
|
2
2
|
|
3
|
-
そのエラーはすでにウィンドウ等にAddされているコントロール(canvas1)を、別のものにAddしようとしたためです。
|
3
|
+
そのエラーはすでにウィンドウ等にAddされているコントロール(`canvas1`)を、別のものにAddしようとしたためです。
|
4
4
|
そのため参考記事ではすべてコードで作成しています。
|
5
5
|
|
6
6
|
`RemoveLogicalChild`で切ることはできると思いますが、切ると当然表示も消えます。
|
2
見直しキャンペーン中
answer
CHANGED
@@ -1,100 +1,100 @@
|
|
1
|
-
注)プリンタが手元にないので未確認です。
|
2
|
-
|
3
|
-
そのエラーはすでにウィンドウ等にAddされているコントロール(canvas1)を、別のものにAddしようとしたためです。
|
4
|
-
そのため参考記事ではすべてコードで作成しています。
|
5
|
-
|
6
|
-
`RemoveLogicalChild`で切ることはできると思いますが、切ると当然表示も消えます。
|
7
|
-
[FrameworkElement.RemoveLogicalChild(Object) メソッド (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.frameworkelement.removelogicalchild)
|
8
|
-
|
9
|
-
あるいはこちらは表示されているものを入れてもいいようです。
|
10
|
-
[PrintDialog.PrintVisual(Visual, String) メソッド (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.printdialog.printvisual)
|
11
|
-
|
12
|
-
---
|
13
|
-
|
14
|
-
プリンタを出すのが面倒なので、仮想プリンタでPDFに出してみましたが出ましたよ?(細かい設定とかはわからないですが
|
15
|
-
|
16
|
-
```
|
17
|
-
<Window
|
18
|
-
x:Class="Questions345252.MainWindow"
|
19
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
-
Width="800"
|
22
|
-
Height="450">
|
23
|
-
<Grid x:Name="grid1">
|
24
|
-
<Grid.RowDefinitions>
|
25
|
-
<RowDefinition Height="Auto" />
|
26
|
-
<RowDefinition />
|
27
|
-
</Grid.RowDefinitions>
|
28
|
-
<StackPanel>
|
29
|
-
<Button Click="Button_Click" Content="印刷 - Remove" />
|
30
|
-
<Button Click="Button_Click_1" Content="印刷 - PrintVisual" />
|
31
|
-
</StackPanel>
|
32
|
-
<Canvas x:Name="canvas1" Grid.Row="1">
|
33
|
-
<Rectangle
|
34
|
-
Canvas.Left="100"
|
35
|
-
Canvas.Top="100"
|
36
|
-
Width="100"
|
37
|
-
Height="100"
|
38
|
-
Fill="Blue" />
|
39
|
-
<TextBlock
|
40
|
-
Canvas.Left="60"
|
41
|
-
Canvas.Top="15"
|
42
|
-
Text="印刷てすと" />
|
43
|
-
<Button
|
44
|
-
Canvas.Left="273"
|
45
|
-
Canvas.Top="65"
|
46
|
-
Width="80"
|
47
|
-
Content="aaa" />
|
48
|
-
</Canvas>
|
49
|
-
</Grid>
|
50
|
-
</Window>
|
51
|
-
```
|
52
|
-
|
53
|
-
```
|
54
|
-
using System.Printing;
|
55
|
-
using System.Windows;
|
56
|
-
using System.Windows.Controls;
|
57
|
-
using System.Windows.Documents;
|
58
|
-
|
59
|
-
namespace Questions345252
|
60
|
-
{
|
61
|
-
public partial class MainWindow : Window
|
62
|
-
{
|
63
|
-
public MainWindow() => InitializeComponent();
|
64
|
-
|
65
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
66
|
-
{
|
67
|
-
var printDialog = new PrintDialog();
|
68
|
-
var result = printDialog.ShowDialog();
|
69
|
-
if (!result.HasValue || !result.Value) return;
|
70
|
-
|
71
|
-
// 切断
|
72
|
-
grid1.Children.Remove(canvas1);
|
73
|
-
|
74
|
-
var page = new FixedPage();
|
75
|
-
|
76
|
-
// 入れる
|
77
|
-
page.Children.Add(canvas1);
|
78
|
-
|
79
|
-
var queue = printDialog.PrintQueue;
|
80
|
-
var writer = PrintQueue.CreateXpsDocumentWriter(queue);
|
81
|
-
writer.Write(page);
|
82
|
-
|
83
|
-
// 終わったのでこっちからも切断
|
84
|
-
page.Children.Remove(canvas1);
|
85
|
-
|
86
|
-
// 戻す
|
87
|
-
grid1.Children.Add(canvas1);
|
88
|
-
}
|
89
|
-
|
90
|
-
private void Button_Click_1(object sender, RoutedEventArgs e)
|
91
|
-
{
|
92
|
-
var printDialog = new PrintDialog();
|
93
|
-
var result = printDialog.ShowDialog();
|
94
|
-
if (!result.HasValue || !result.Value) return;
|
95
|
-
|
96
|
-
printDialog.PrintVisual(canvas1, "Questions345252");
|
97
|
-
}
|
98
|
-
}
|
99
|
-
}
|
1
|
+
注)プリンタが手元にないので未確認です。
|
2
|
+
|
3
|
+
そのエラーはすでにウィンドウ等にAddされているコントロール(canvas1)を、別のものにAddしようとしたためです。
|
4
|
+
そのため参考記事ではすべてコードで作成しています。
|
5
|
+
|
6
|
+
`RemoveLogicalChild`で切ることはできると思いますが、切ると当然表示も消えます。
|
7
|
+
[FrameworkElement.RemoveLogicalChild(Object) メソッド (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.frameworkelement.removelogicalchild)
|
8
|
+
|
9
|
+
あるいはこちらは表示されているものを入れてもいいようです。
|
10
|
+
[PrintDialog.PrintVisual(Visual, String) メソッド (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.printdialog.printvisual)
|
11
|
+
|
12
|
+
---
|
13
|
+
|
14
|
+
プリンタを出すのが面倒なので、仮想プリンタでPDFに出してみましたが出ましたよ?(細かい設定とかはわからないですが
|
15
|
+
|
16
|
+
```xml
|
17
|
+
<Window
|
18
|
+
x:Class="Questions345252.MainWindow"
|
19
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
+
Width="800"
|
22
|
+
Height="450">
|
23
|
+
<Grid x:Name="grid1">
|
24
|
+
<Grid.RowDefinitions>
|
25
|
+
<RowDefinition Height="Auto" />
|
26
|
+
<RowDefinition />
|
27
|
+
</Grid.RowDefinitions>
|
28
|
+
<StackPanel>
|
29
|
+
<Button Click="Button_Click" Content="印刷 - Remove" />
|
30
|
+
<Button Click="Button_Click_1" Content="印刷 - PrintVisual" />
|
31
|
+
</StackPanel>
|
32
|
+
<Canvas x:Name="canvas1" Grid.Row="1">
|
33
|
+
<Rectangle
|
34
|
+
Canvas.Left="100"
|
35
|
+
Canvas.Top="100"
|
36
|
+
Width="100"
|
37
|
+
Height="100"
|
38
|
+
Fill="Blue" />
|
39
|
+
<TextBlock
|
40
|
+
Canvas.Left="60"
|
41
|
+
Canvas.Top="15"
|
42
|
+
Text="印刷てすと" />
|
43
|
+
<Button
|
44
|
+
Canvas.Left="273"
|
45
|
+
Canvas.Top="65"
|
46
|
+
Width="80"
|
47
|
+
Content="aaa" />
|
48
|
+
</Canvas>
|
49
|
+
</Grid>
|
50
|
+
</Window>
|
51
|
+
```
|
52
|
+
|
53
|
+
```cs
|
54
|
+
using System.Printing;
|
55
|
+
using System.Windows;
|
56
|
+
using System.Windows.Controls;
|
57
|
+
using System.Windows.Documents;
|
58
|
+
|
59
|
+
namespace Questions345252
|
60
|
+
{
|
61
|
+
public partial class MainWindow : Window
|
62
|
+
{
|
63
|
+
public MainWindow() => InitializeComponent();
|
64
|
+
|
65
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
66
|
+
{
|
67
|
+
var printDialog = new PrintDialog();
|
68
|
+
var result = printDialog.ShowDialog();
|
69
|
+
if (!result.HasValue || !result.Value) return;
|
70
|
+
|
71
|
+
// 切断
|
72
|
+
grid1.Children.Remove(canvas1);
|
73
|
+
|
74
|
+
var page = new FixedPage();
|
75
|
+
|
76
|
+
// 入れる
|
77
|
+
page.Children.Add(canvas1);
|
78
|
+
|
79
|
+
var queue = printDialog.PrintQueue;
|
80
|
+
var writer = PrintQueue.CreateXpsDocumentWriter(queue);
|
81
|
+
writer.Write(page);
|
82
|
+
|
83
|
+
// 終わったのでこっちからも切断
|
84
|
+
page.Children.Remove(canvas1);
|
85
|
+
|
86
|
+
// 戻す
|
87
|
+
grid1.Children.Add(canvas1);
|
88
|
+
}
|
89
|
+
|
90
|
+
private void Button_Click_1(object sender, RoutedEventArgs e)
|
91
|
+
{
|
92
|
+
var printDialog = new PrintDialog();
|
93
|
+
var result = printDialog.ShowDialog();
|
94
|
+
if (!result.HasValue || !result.Value) return;
|
95
|
+
|
96
|
+
printDialog.PrintVisual(canvas1, "Questions345252");
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
100
|
```
|
1
コード
answer
CHANGED
@@ -7,4 +7,94 @@
|
|
7
7
|
[FrameworkElement.RemoveLogicalChild(Object) メソッド (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.frameworkelement.removelogicalchild)
|
8
8
|
|
9
9
|
あるいはこちらは表示されているものを入れてもいいようです。
|
10
|
-
[PrintDialog.PrintVisual(Visual, String) メソッド (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.printdialog.printvisual)
|
10
|
+
[PrintDialog.PrintVisual(Visual, String) メソッド (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.printdialog.printvisual)
|
11
|
+
|
12
|
+
---
|
13
|
+
|
14
|
+
プリンタを出すのが面倒なので、仮想プリンタでPDFに出してみましたが出ましたよ?(細かい設定とかはわからないですが
|
15
|
+
|
16
|
+
```xaml
|
17
|
+
<Window
|
18
|
+
x:Class="Questions345252.MainWindow"
|
19
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
20
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
21
|
+
Width="800"
|
22
|
+
Height="450">
|
23
|
+
<Grid x:Name="grid1">
|
24
|
+
<Grid.RowDefinitions>
|
25
|
+
<RowDefinition Height="Auto" />
|
26
|
+
<RowDefinition />
|
27
|
+
</Grid.RowDefinitions>
|
28
|
+
<StackPanel>
|
29
|
+
<Button Click="Button_Click" Content="印刷 - Remove" />
|
30
|
+
<Button Click="Button_Click_1" Content="印刷 - PrintVisual" />
|
31
|
+
</StackPanel>
|
32
|
+
<Canvas x:Name="canvas1" Grid.Row="1">
|
33
|
+
<Rectangle
|
34
|
+
Canvas.Left="100"
|
35
|
+
Canvas.Top="100"
|
36
|
+
Width="100"
|
37
|
+
Height="100"
|
38
|
+
Fill="Blue" />
|
39
|
+
<TextBlock
|
40
|
+
Canvas.Left="60"
|
41
|
+
Canvas.Top="15"
|
42
|
+
Text="印刷てすと" />
|
43
|
+
<Button
|
44
|
+
Canvas.Left="273"
|
45
|
+
Canvas.Top="65"
|
46
|
+
Width="80"
|
47
|
+
Content="aaa" />
|
48
|
+
</Canvas>
|
49
|
+
</Grid>
|
50
|
+
</Window>
|
51
|
+
```
|
52
|
+
|
53
|
+
```C#
|
54
|
+
using System.Printing;
|
55
|
+
using System.Windows;
|
56
|
+
using System.Windows.Controls;
|
57
|
+
using System.Windows.Documents;
|
58
|
+
|
59
|
+
namespace Questions345252
|
60
|
+
{
|
61
|
+
public partial class MainWindow : Window
|
62
|
+
{
|
63
|
+
public MainWindow() => InitializeComponent();
|
64
|
+
|
65
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
66
|
+
{
|
67
|
+
var printDialog = new PrintDialog();
|
68
|
+
var result = printDialog.ShowDialog();
|
69
|
+
if (!result.HasValue || !result.Value) return;
|
70
|
+
|
71
|
+
// 切断
|
72
|
+
grid1.Children.Remove(canvas1);
|
73
|
+
|
74
|
+
var page = new FixedPage();
|
75
|
+
|
76
|
+
// 入れる
|
77
|
+
page.Children.Add(canvas1);
|
78
|
+
|
79
|
+
var queue = printDialog.PrintQueue;
|
80
|
+
var writer = PrintQueue.CreateXpsDocumentWriter(queue);
|
81
|
+
writer.Write(page);
|
82
|
+
|
83
|
+
// 終わったのでこっちからも切断
|
84
|
+
page.Children.Remove(canvas1);
|
85
|
+
|
86
|
+
// 戻す
|
87
|
+
grid1.Children.Add(canvas1);
|
88
|
+
}
|
89
|
+
|
90
|
+
private void Button_Click_1(object sender, RoutedEventArgs e)
|
91
|
+
{
|
92
|
+
var printDialog = new PrintDialog();
|
93
|
+
var result = printDialog.ShowDialog();
|
94
|
+
if (!result.HasValue || !result.Value) return;
|
95
|
+
|
96
|
+
printDialog.PrintVisual(canvas1, "Questions345252");
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
```
|