質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

3382閲覧

【WPF】RichTextBoxのText要素に角丸枠線を付けたい

akihiro_h

総合スコア2

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

1クリップ

投稿2021/08/28 16:09

前提・実現したいこと

WPFのRichTextBoxにコードで追加するTextを枠線で囲もうとしています。この枠線の角を丸くしたいのですが可能でしょうか。

該当のソースコード

XAML

1 <RichTextBox x:Name="RtbMain" BorderThickness="0"/> 2

C#

1 Run run = new Run 2 { 3 Text = "Hogehoge" 4 }; 5 Paragraph par = new Paragraph 6 { 7 BorderThickness = new Thickness(1), 8 BorderBrush = Brushes.Aqua, 9 TextAlignment = TextAlignment.Right, 10 Margin = new Thickness(150, 5, 5, 5) 11 }; 12 par.Inlines.Add(run); 13 RtbMain.Document.Blocks.Add(par); 14 15

補足情報(FW/ツールのバージョンなど)

vs2019 wpf C# .net core3.1

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

やりたいことは単純なうえ需要もありそうなのに、やってみると非常に難しいですね^^;
長々書いていますが、残念ながら満足いく解決法は見つかりませんでした。。。

普通に検索するとこの辺りがすぐ出てきます。
c# - How can I make a paragraph corners round? - Stack Overflow

How do I surround Block content with Borders that have rounded corners?

しかしBlockUIContainerにくるむと、コレジャナイ感がすごいです(これでいいなら話は早いです)

2つ目のリンクで言及があるAdornerも↓をベースに検討しましたが、位置の更新以前にParagraphのサイズ取得法が見つけられませんでした。
Create custom FrameworkContentElement to add diagonal line over text in WPF - Stack Overflow

Paragraphを改造?するような方法も探りましたが、internalまみれでどうしようもなさそうでした。

発想を変えてBackgroundVisualBrushBorderを突っ込むという雑な方法でそれっぽくはなりましたが、背景なので内側に描画になるのとWidthHeightを実際のサイズに近い数字にしないと残念なことになります。

面倒なのでxamlで書きましたが、C#コードにするのは簡単でしょう。

xml

1<Window 2 x:Class="Questions356645.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 Width="800" 6 Height="450"> 7 <Grid> 8 <RichTextBox Margin="10" BorderThickness="0"> 9 <FlowDocument> 10 <Paragraph 11 Margin="150,5,5,5" 12 BorderBrush="Aqua" 13 BorderThickness="1" 14 TextAlignment="Right"> 15 <Run Text="normal" /> 16 </Paragraph> 17 18 <BlockUIContainer> 19 <Border 20 Margin="150,5,5,5" 21 BorderBrush="Aqua" 22 BorderThickness="1" 23 CornerRadius="5"> 24 <RichTextBox BorderThickness="0"> 25 <FlowDocument> 26 <Paragraph TextAlignment="Right"> 27 <Run Text="BlockUIContainer" /> 28 </Paragraph> 29 </FlowDocument> 30 </RichTextBox> 31 </Border> 32 </BlockUIContainer> 33 34 <Paragraph Margin="150,5,5,5" TextAlignment="Right"> 35 <Paragraph.Background> 36 <VisualBrush> 37 <VisualBrush.Visual> 38 <Border 39 Width="600" 40 Height="20" 41 BorderBrush="Aqua" 42 BorderThickness="1" 43 CornerRadius="5" /> 44 </VisualBrush.Visual> 45 </VisualBrush> 46 </Paragraph.Background> 47 <Run Text="VisualBrush" /> 48 </Paragraph> 49 50 <Paragraph 51 Margin="150,5,5,5" 52 BorderBrush="Aqua" 53 BorderThickness="5" 54 TextAlignment="Right"> 55 <Run Text="normal" /> 56 </Paragraph> 57 58 <BlockUIContainer> 59 <Border 60 Margin="150,5,5,5" 61 BorderBrush="Aqua" 62 BorderThickness="5" 63 CornerRadius="5"> 64 <RichTextBox BorderThickness="0"> 65 <FlowDocument> 66 <Paragraph TextAlignment="Right"> 67 <Run Text="BlockUIContainer" /> 68 </Paragraph> 69 </FlowDocument> 70 </RichTextBox> 71 </Border> 72 </BlockUIContainer> 73 74 <Paragraph Margin="150,5,5,5" TextAlignment="Right"> 75 <Paragraph.Background> 76 <VisualBrush> 77 <VisualBrush.Visual> 78 <Border 79 Width="600" 80 Height="20" 81 BorderBrush="Aqua" 82 BorderThickness="5" 83 CornerRadius="5" /> 84 </VisualBrush.Visual> 85 </VisualBrush> 86 </Paragraph.Background> 87 <Run Text="VisualBrush" /> 88 </Paragraph> 89 90 <Paragraph Margin="150,5,5,5" TextAlignment="Right"> 91 <Paragraph.Background> 92 <VisualBrush> 93 <VisualBrush.Visual> 94 <Border 95 Width="100" 96 Height="100" 97 BorderBrush="Aqua" 98 BorderThickness="1" 99 CornerRadius="5" /> 100 </VisualBrush.Visual> 101 </VisualBrush> 102 </Paragraph.Background> 103 <Run Text="VisualBrush" /> 104 </Paragraph> 105 </FlowDocument> 106 </RichTextBox> 107 </Grid> 108</Window>

アプリ画像


もう「htmlかなんかでやったほうがマシなんじゃ?」という気はします^^;

投稿2021/08/29 09:51

編集2023/07/28 17:13
TN8001

総合スコア9401

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

akihiro_h

2021/08/29 13:58

ありがとうございます。 いろいろ上げていただいた中からVisualBrushの方法でコードを書いてみます。試しに書いたら面白いもの(塗り潰しの漫画の吹き出し風)になってしまい、まあこれはこれでという感じなのですがもう少し勉強します。
guest

0

最終的に次のようなコードで実現出来ました。

C#

1 2 Run run = new Run 3 { 4 Text = "Hogehoge" 5 }; 6 Paragraph par = new Paragraph 7 { 8 TextAlignment = TextAlignment.Right, 9 Margin = new Thickness(150, 5, 5, 5) 10 }; 11 Border bd = new Border 12 { 13 Height = 10, 14 Width = 80, 15 BorderBrush = Brushes.Aqua, 16 BorderThickness = new Thickness(1), 17 CornerRadius = new CornerRadius(1, 2, 3, 10) 18 }; 19 VisualBrush vb = new VisualBrush(bd); 20 par.Inlines.Add(run); 21 par.Background = vb; 22 RtbMain.Document.Blocks.Add(par); 23

最初BoderのHeightやWidthを指定しなかったら塗りつぶしで表示されました(おそらく枠線の一部だけが拡大・切り取られて表示される?)。HeightとWidthを正しく設定しないとですが取れない・・・。とりあえず
イメージ説明

投稿2021/08/30 03:55

akihiro_h

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問