回答編集履歴

4

見直しキャンペーン中

2023/07/29 06:18

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -96,3 +96,5 @@
96
96
  > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
97
97
 
98
98
  雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)
99
+
100
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/6f285c71-035c-4670-8118-db6ba680f867.gif)

3

見直しキャンペーン中

2023/07/29 06:07

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,195 +1,98 @@
1
1
  ぶっちゃけなにもわかってません^^;
2
2
 
3
-
4
-
5
3
  [TextPointer.GetPositionAtOffset メソッド (System.Windows.Documents) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.documents.textpointer.getpositionatoffset)
6
-
7
4
  は「シンボル単位」となっており、「文字単位」ではないようです。
8
-
9
5
  そのため`TextPointer`を、うまく送れてないのではないでしょうか?
10
6
 
11
-
12
-
13
7
  適当にggって見つけたコードで置き換えたところ、それっぽく動いている気はします。
14
-
15
8
  [c# - Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically - Stack Overflow](https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically/12399115#12399115)
16
9
 
17
-
18
-
19
- ```xaml
10
+ ```xml
20
-
21
11
  <Window
22
-
23
12
  x:Class="Questions361934.MainWindow"
24
-
25
13
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
-
27
14
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
-
29
15
  Width="800"
30
-
31
16
  Height="450">
32
-
33
17
  <Grid>
34
-
35
18
  <Grid.RowDefinitions>
36
-
37
19
  <RowDefinition />
38
-
39
20
  <RowDefinition />
40
-
41
21
  <RowDefinition />
42
-
43
22
  </Grid.RowDefinitions>
44
-
45
23
  <RichTextBox x:Name="rtb">
46
-
47
24
  <FlowDocument>
48
-
49
25
  <Paragraph>
50
-
51
26
  tonarinokyakuhayokukakikuukyakuda
52
-
53
27
  </Paragraph>
54
-
55
28
  </FlowDocument>
56
-
57
29
  </RichTextBox>
58
30
 
59
-
60
-
61
31
  <TextBlock Grid.Row="1" Text="tonarinokyakuhayokukakikuukyakuda" />
62
-
63
32
  <TextBlock
64
-
65
33
  x:Name="textBlock"
66
-
67
34
  Grid.Row="1"
68
-
69
35
  Foreground="Red" />
70
36
 
71
-
72
-
73
37
  <TextBox
74
-
75
38
  x:Name="tb"
76
-
77
39
  Grid.Row="2"
78
-
79
40
  TextChanged="TextChanged" />
80
-
81
41
  </Grid>
82
-
83
42
  </Window>
84
-
85
43
  ```
86
44
 
87
-
88
-
89
- ```C#
45
+ ```cs
90
-
91
46
  using System.Windows;
92
-
93
47
  using System.Windows.Controls;
94
-
95
48
  using System.Windows.Documents;
96
-
97
49
  using System.Windows.Media;
98
50
 
99
-
100
-
101
51
  namespace Questions361934
102
-
103
52
  {
104
-
105
53
  public partial class MainWindow : Window
106
-
107
54
  {
108
-
109
55
  public MainWindow() => InitializeComponent();
110
56
 
111
-
112
-
113
57
  private void TextChanged(object sender, TextChangedEventArgs e)
114
-
115
58
  {
116
-
117
59
  var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
118
-
119
60
  textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
120
-
121
61
 
122
-
123
62
  textBlock.Text = "";
124
63
 
125
-
126
-
127
64
  var kaitou = "tonarinokyakuhayokukakikuukyakuda";
128
-
129
65
  var ans = tb.Text;
130
-
131
66
  if (kaitou.StartsWith(ans))
132
-
133
67
  {
134
-
135
68
  var p = GetPoint(rtb.Document.ContentStart, ans.Length);
136
-
137
69
  textRange = new TextRange(rtb.Document.ContentStart, p);
138
-
139
70
  textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
140
71
 
141
-
142
-
143
72
  textBlock.Text = ans;
144
-
145
73
  }
146
-
147
74
  }
148
75
 
149
-
150
-
151
76
  // [c# - Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically - Stack Overflow](https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically/12399115#12399115)
152
-
153
77
  private static TextPointer GetPoint(TextPointer start, int x)
154
-
155
78
  {
156
-
157
79
  var ret = start;
158
-
159
80
  var i = 0;
160
-
161
81
  while (ret != null)
162
-
163
82
  {
164
-
165
83
  var stringSoFar = new TextRange(ret, ret.GetPositionAtOffset(i, LogicalDirection.Forward)).Text;
166
-
167
84
  if (stringSoFar.Length == x) break;
168
85
 
169
-
170
-
171
86
  i++;
172
-
173
87
  if (ret.GetPositionAtOffset(i, LogicalDirection.Forward) == null)
174
-
175
88
  return ret.GetPositionAtOffset(i - 1, LogicalDirection.Forward);
176
-
177
89
  }
178
-
179
90
  return ret.GetPositionAtOffset(i, LogicalDirection.Forward);
180
-
181
91
  }
182
-
183
92
  }
184
-
185
93
  }
186
-
187
94
  ```
188
-
189
-
190
95
 
191
96
  > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
192
97
 
193
-
194
-
195
98
  雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)

2

簡単な方法

2021/09/29 21:18

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -38,6 +38,8 @@
38
38
 
39
39
  <RowDefinition />
40
40
 
41
+ <RowDefinition />
42
+
41
43
  </Grid.RowDefinitions>
42
44
 
43
45
  <RichTextBox x:Name="rtb">
@@ -54,11 +56,25 @@
54
56
 
55
57
  </RichTextBox>
56
58
 
59
+
60
+
61
+ <TextBlock Grid.Row="1" Text="tonarinokyakuhayokukakikuukyakuda" />
62
+
63
+ <TextBlock
64
+
65
+ x:Name="textBlock"
66
+
67
+ Grid.Row="1"
68
+
69
+ Foreground="Red" />
70
+
71
+
72
+
57
73
  <TextBox
58
74
 
59
75
  x:Name="tb"
60
76
 
61
- Grid.Row="1"
77
+ Grid.Row="2"
62
78
 
63
79
  TextChanged="TextChanged" />
64
80
 
@@ -102,6 +118,10 @@
102
118
 
103
119
  textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
104
120
 
121
+
122
+
123
+ textBlock.Text = "";
124
+
105
125
 
106
126
 
107
127
  var kaitou = "tonarinokyakuhayokukakikuukyakuda";
@@ -117,6 +137,10 @@
117
137
  textRange = new TextRange(rtb.Document.ContentStart, p);
118
138
 
119
139
  textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
140
+
141
+
142
+
143
+ textBlock.Text = ans;
120
144
 
121
145
  }
122
146
 
@@ -160,6 +184,12 @@
160
184
 
161
185
  }
162
186
 
187
+ ```
163
188
 
164
189
 
190
+
191
+ > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
192
+
193
+
194
+
165
- ```
195
+ 雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)

1

()

2021/09/29 21:18

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [TextPointer.GetPositionAtOffset メソッド (System.Windows.Documents) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.documents.textpointer.getpositionatoffset)
6
6
 
7
- は「(シンボル単位)」となっており、「文字単位」ではないようです。
7
+ は「シンボル単位」となっており、「文字単位」ではないようです。
8
8
 
9
9
  そのため`TextPointer`を、うまく送れてないのではないでしょうか?
10
10