teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/29 12:45

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,151 +1,151 @@
1
- 閲覧者向けに雑実装^^;
2
-
3
- 閉じるボタンは消していませんが、押しても閉じません。
4
- コードからキャンセルとかアイコン表示とかいろいろあるでしょうが、わたしは使わないのでこれ以上凝る気はありません(ダイアログ嫌いなので^^;
5
-
6
- ```xaml
7
- <Window
8
- x:Class="Questions374184.MainWindow"
9
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
- Width="800"
12
- Height="450">
13
- <StackPanel>
14
- <Button Content="TaskDialog" Click="Button_Click" />
15
- <Button Content="ProgressDialog" Click="Button_Click_1" />
16
- </StackPanel>
17
- </Window>
18
- ```
19
- ```C#
20
- using System;
21
- using System.Threading;
22
- using System.Threading.Tasks;
23
- using System.Windows;
24
- using Microsoft.WindowsAPICodePack.Dialogs;
25
-
26
- namespace Questions374184
27
- {
28
- public partial class MainWindow : Window
29
- {
30
- public MainWindow() => InitializeComponent();
31
-
32
- private void Button_Click(object sender, RoutedEventArgs e)
33
- {
34
- var dialog = new TaskDialog
35
- {
36
- Caption = "Caption",
37
- InstructionText = "InstructionText",
38
- Text = "Text",
39
- ProgressBar = new TaskDialogProgressBar(),
40
- };
41
-
42
- IProgress<int> progress = new Progress<int>(i =>
43
- {
44
- dialog.ProgressBar.Value = i;
45
- if (100 <= i) dialog.Close();
46
- });
47
-
48
- Task.Run(() =>
49
- {
50
- for (var i = 0; i <= 100; i++)
51
- {
52
- Thread.Sleep(100);
53
- progress.Report(i);
54
- }
55
- });
56
-
57
- dialog.Show();
58
- }
59
-
60
- private void Button_Click_1(object sender, RoutedEventArgs e)
61
- {
62
- var dialog = new ProgressDialog
63
- {
64
- Caption = "Caption",
65
- InstructionText = "InstructionText",
66
- Text = "Text",
67
- Owner = this,
68
- };
69
-
70
- Task.Run(() =>
71
- {
72
- for (var i = 0; i <= 100; i++)
73
- {
74
- Thread.Sleep(100);
75
- dialog.Progress.Report(i);
76
- }
77
- });
78
-
79
- dialog.ShowDialog();
80
- }
81
- }
82
- }
83
- ```
84
-
85
- ```xaml
86
- <Window
87
- x:Class="Questions374184.ProgressDialog"
88
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
89
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
90
- Title="ProgressDialog"
91
- Width="380"
92
- ResizeMode="NoResize"
93
- SizeToContent="Height"
94
- WindowStartupLocation="CenterOwner">
95
- <StackPanel Margin="10">
96
- <TextBlock
97
- x:Name="instructionText"
98
- Margin="5"
99
- Foreground="#FF003399"
100
- Text="InstructionText" />
101
- <TextBlock
102
- x:Name="text"
103
- Margin="5"
104
- Text="Text" />
105
- <ProgressBar
106
- x:Name="progressBar"
107
- Height="20"
108
- Margin="5" />
109
- </StackPanel>
110
- </Window>
111
- ```
112
- ```C#
113
- using System;
114
- using System.ComponentModel;
115
- using System.Threading.Tasks;
116
- using System.Windows;
117
-
118
- namespace Questions374184
119
- {
120
- public partial class ProgressDialog : Window
121
- {
122
- public string Caption { get => Title; set => Title = value; }
123
- public string InstructionText { get => instructionText.Text; set => instructionText.Text = value; }
124
- public string Text { get => text.Text; set => text.Text = value; }
125
- public IProgress<int> Progress { get; }
126
-
127
- public ProgressDialog()
128
- {
129
- InitializeComponent();
130
-
131
- Progress = new Progress<int>(i =>
132
- {
133
- progressBar.Value = i;
134
- if (100 <= i) Close();
135
- });
136
- }
137
-
138
- protected override void OnClosing(CancelEventArgs e)
139
- {
140
- e.Cancel = progressBar.Value < 100;
141
- }
142
- }
143
- }
144
- ```
145
-
146
- 対比のために入れただけなので、WindowsAPICodePackを入れる必要はないです。
147
-
148
- わたしが使用したのは↓で、マニュフェストでコモンコントロールを有効にする必要があります。
149
- [NuGet Gallery | WindowsAPICodePack-Core 1.1.2](https://www.nuget.org/packages/WindowsAPICodePack-Core/1.1.2)
150
-
1
+ 閲覧者向けに雑実装^^;
2
+
3
+ 閉じるボタンは消していませんが、押しても閉じません。
4
+ コードからキャンセルとかアイコン表示とかいろいろあるでしょうが、わたしは使わないのでこれ以上凝る気はありません(ダイアログ嫌いなので^^;
5
+
6
+ ```xml
7
+ <Window
8
+ x:Class="Questions374184.MainWindow"
9
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
+ Width="800"
12
+ Height="450">
13
+ <StackPanel>
14
+ <Button Content="TaskDialog" Click="Button_Click" />
15
+ <Button Content="ProgressDialog" Click="Button_Click_1" />
16
+ </StackPanel>
17
+ </Window>
18
+ ```
19
+ ```cs
20
+ using System;
21
+ using System.Threading;
22
+ using System.Threading.Tasks;
23
+ using System.Windows;
24
+ using Microsoft.WindowsAPICodePack.Dialogs;
25
+
26
+ namespace Questions374184
27
+ {
28
+ public partial class MainWindow : Window
29
+ {
30
+ public MainWindow() => InitializeComponent();
31
+
32
+ private void Button_Click(object sender, RoutedEventArgs e)
33
+ {
34
+ var dialog = new TaskDialog
35
+ {
36
+ Caption = "Caption",
37
+ InstructionText = "InstructionText",
38
+ Text = "Text",
39
+ ProgressBar = new TaskDialogProgressBar(),
40
+ };
41
+
42
+ IProgress<int> progress = new Progress<int>(i =>
43
+ {
44
+ dialog.ProgressBar.Value = i;
45
+ if (100 <= i) dialog.Close();
46
+ });
47
+
48
+ Task.Run(() =>
49
+ {
50
+ for (var i = 0; i <= 100; i++)
51
+ {
52
+ Thread.Sleep(100);
53
+ progress.Report(i);
54
+ }
55
+ });
56
+
57
+ dialog.Show();
58
+ }
59
+
60
+ private void Button_Click_1(object sender, RoutedEventArgs e)
61
+ {
62
+ var dialog = new ProgressDialog
63
+ {
64
+ Caption = "Caption",
65
+ InstructionText = "InstructionText",
66
+ Text = "Text",
67
+ Owner = this,
68
+ };
69
+
70
+ Task.Run(() =>
71
+ {
72
+ for (var i = 0; i <= 100; i++)
73
+ {
74
+ Thread.Sleep(100);
75
+ dialog.Progress.Report(i);
76
+ }
77
+ });
78
+
79
+ dialog.ShowDialog();
80
+ }
81
+ }
82
+ }
83
+ ```
84
+
85
+ ```xml
86
+ <Window
87
+ x:Class="Questions374184.ProgressDialog"
88
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
89
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
90
+ Title="ProgressDialog"
91
+ Width="380"
92
+ ResizeMode="NoResize"
93
+ SizeToContent="Height"
94
+ WindowStartupLocation="CenterOwner">
95
+ <StackPanel Margin="10">
96
+ <TextBlock
97
+ x:Name="instructionText"
98
+ Margin="5"
99
+ Foreground="#FF003399"
100
+ Text="InstructionText" />
101
+ <TextBlock
102
+ x:Name="text"
103
+ Margin="5"
104
+ Text="Text" />
105
+ <ProgressBar
106
+ x:Name="progressBar"
107
+ Height="20"
108
+ Margin="5" />
109
+ </StackPanel>
110
+ </Window>
111
+ ```
112
+ ```cs
113
+ using System;
114
+ using System.ComponentModel;
115
+ using System.Threading.Tasks;
116
+ using System.Windows;
117
+
118
+ namespace Questions374184
119
+ {
120
+ public partial class ProgressDialog : Window
121
+ {
122
+ public string Caption { get => Title; set => Title = value; }
123
+ public string InstructionText { get => instructionText.Text; set => instructionText.Text = value; }
124
+ public string Text { get => text.Text; set => text.Text = value; }
125
+ public IProgress<int> Progress { get; }
126
+
127
+ public ProgressDialog()
128
+ {
129
+ InitializeComponent();
130
+
131
+ Progress = new Progress<int>(i =>
132
+ {
133
+ progressBar.Value = i;
134
+ if (100 <= i) Close();
135
+ });
136
+ }
137
+
138
+ protected override void OnClosing(CancelEventArgs e)
139
+ {
140
+ e.Cancel = progressBar.Value < 100;
141
+ }
142
+ }
143
+ }
144
+ ```
145
+
146
+ 対比のために入れただけなので、WindowsAPICodePackを入れる必要はないです。
147
+
148
+ わたしが使用したのは↓で、マニュフェストでコモンコントロールを有効にする必要があります。
149
+ [NuGet Gallery | WindowsAPICodePack-Core 1.1.2](https://www.nuget.org/packages/WindowsAPICodePack-Core/1.1.2)
150
+
151
151
  [視覚スタイルを有効にする - Win32 apps | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/win32/controls/cookbook-overview)