WPF では、WinForm のように任意のタイミングで再描画や特定のコントロールに線など描画できないようですね。
なかなかグーグルでヒットしません。
下記のようなサンプルはありましたが、どうやら特定のコントロールを指定して書くことはできないようです。
C#
1DrawingVisual drawingVisual = new DrawingVisual(); 2 3// Retrieve the DrawingContext in order to create new drawing content. 4DrawingContext drawingContext = drawingVisual.RenderOpen(); 5 6// Create a rectangle and draw it in the DrawingContext. 7Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80)); 8drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect); 9 10// Persist the drawing content. 11drawingContext.Close();
実際には、下記のコードを呼んでRichTextBox に波線を引きたいのですが、どこから呼び出すようにしたらいいのかわかりません。
VisualTreeHelper より目的のコントロールを探すことも試しましたが、今のところヌルしか戻りません。
C#
1 private void DrawWave(System.Windows.Media.DrawingContext graphics, System.Windows.Point StartOfLine, System.Windows.Point EndOfLine) 2 { 3 System.Windows.Media.Pen newPen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Red, 1); 4 5 if ((EndOfLine.X - StartOfLine.X) > 4) 6 { 7 System.Collections.ArrayList pl = new System.Collections.ArrayList(); 8 for (int i = (int)StartOfLine.X; i <= ((int)EndOfLine.X - 2); i += 4) 9 { 10 pl.Add(new System.Windows.Point(i, StartOfLine.Y)); 11 pl.Add(new System.Windows.Point(i + 2, StartOfLine.Y + 2)); 12 } 13 14 Point[] p = (Point[])pl.ToArray(typeof(Point)); 15 for (int i = 0; i < p.Length - 1; ++i) 16 graphics.DrawLine(newPen, p[i], p[i + 1]); 17 } 18 else 19 { 20 graphics.DrawLine(newPen, StartOfLine, EndOfLine); 21 } 22 } 23
VS2015 C# WPF
RichTextBox に波線を引きたいのですか? その中のテキストにではなく?
テキストに波線を引きたいなら Document プロパティを使ってください。https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.richtextbox.document?view=netframework-4.8#System_Windows_Controls_RichTextBox_Document
お世話になります。
TextBox でも波線が引けるのですね。WinForm のイメージが強く RichTextBox しかできないものと思い込んでいました。
ただ、波線の範囲を区切って引く(例えば、協調したい言葉など)場合、どのプロパティに値をセットするのでしょうか?
Run です。
回答1件
あなたの回答
tips
プレビュー