WPFはRichTextBoxの画像挿入と行間調整を実現


WPFにはWinformと同じ形式のものが多いが,コントロールの使用には驚くことが多い.RichTextBoxは一例です.はい、WPFではこのコントロールに対してWinformが難しい効果をたくさん作ることができます.
例えばRichTextBoxに画像を挿入すると、winform時代にはクリップボードをコピーして貼り付けるという下手な方法のほか、RichTextBoxコントロールを書き直してカスタマイズすることになりました.これには優れたプログラミング能力が必要です.しかし、WPFでは、いくつかのコードを追加するだけでできます.
XAMLにRichTextBoxに画像を追加すると、以下のようになります.

 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> ​ ​ ​ ​ ​ ​ ​ < R​i​c​h​T​e​x​t​B​o​x​ H​o​r​i​z​o​n​t​a​l​A​l​i​g​n​m​e​n​t =​"​L​e​f​t​" ​M​a​r​g​i​n =​"​9​0​,​1​2​,​0​,​0​" ​N​a​m​e =​"​r​i​c​h​T​e​x​t​B​o​x​1​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​i​c​h​T​e​x​t​B​o​x​.​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < F​l​o​w​D​o​c​u​m​e​n​t​ F​o​c​u​s​a​b​l​e =​"​T​r​u​e​" ​L​i​n​e​H​e​i​g​h​t =​"​5​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < P​a​r​a​g​r​a​p​h​ x​:​N​a​m​e =​"​g​a​r​a​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < I​m​a​g​e​ S​o​u​r​c​e =​"​D​:​\​1​3​4​2​8​9​2​_​1​0​.​j​p​g​" ​F​o​c​u​s​a​b​l​e =​"​T​r​u​e​" ​H​e​i​g​h​t =​"​5​0​" ​S​t​r​e​t​c​h =​"​U​n​i​f​o​r​m​" /​> ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​ ​ ​ ​ ​ ​ ​ ​ ​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​ ​ ​ ​ ​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ P​a​r​a​g​r​a​p​h > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < P​a​r​a​g​r​a​p​h​ x​:​N​a​m​e =​"​g​a​r​a​1​" > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​ ​ ​ ​ ​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ < R​u​n​ T​e​x​t =​"​ ​ ​ ​ ​" >​<​/ R​u​n > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ P​a​r​a​g​r​a​p​h > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ F​l​o​w​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ <​/ R​i​c​h​T​e​x​t​B​o​x​.​D​o​c​u​m​e​n​t > ​ ​ ​ ​ ​ ​ ​ <​/ R​i​c​h​T​e​x​t​B​o​x >

これでコントロールに画像を追加しました.
メモ:FlowDocumentのLineHeightプロパティは、テキスト段落の間隔です.デフォルトの間隔が大きいので、ここで調整します!
もちろん、プログラムが実行されている間にボタンをクリックして画像を選択して追加する必要があるため、必ずしも要求を完全に満たすことはできません.コードは次のとおりです.

 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> p​r​i​v​a​t​e v​o​i​d ​A​d​d​J​P​G​_​C​l​i​c​k​( o​b​j​e​c​t ​s​e​n​d​e​r​,​ ​R​o​u​t​e​d​E​v​e​n​t​A​r​g​s​ ​e​)​ ​ ​ ​ ​ ​ ​ ​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ s​t​r​i​n​g ​f​i​l​e​p​a​t​h​ = "​" ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ s​t​r​i​n​g ​f​i​l​e​n​a​m​e​ = "​" ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​O​p​e​n​F​i​l​e​D​i​a​l​o​g​ ​o​p​e​n​f​i​l​e​j​p​g​ = n​e​w ​O​p​e​n​F​i​l​e​D​i​a​l​o​g​(​)​;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​e​n​f​i​l​e​j​p​g​.​F​i​l​t​e​r​ = " j​p​g​ ​ ​(​*​.​j​p​g​)​|​*​.​j​p​g​|​g​i​f​ ​ ​(​*​.​g​i​f​)​|​*​.​g​i​f " ;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​p​e​n​f​i​l​e​j​p​g​.​F​i​l​t​e​r​I​n​d​e​x​ = 0 ; openfilejpg.RestoreDirectory = true ; openfilejpg.Multiselect = false ; if (openfilejpg.ShowDialog() == true ) { filepath = openfilejpg.FileName; Image img = new Image(); BitmapImage bImg = new BitmapImage(); img.IsEnabled = true ; bImg.BeginInit(); bImg.UriSource = new Uri(filepath, UriKind.Relative); bImg.EndInit(); img.Source = bImg; // MessageBox.Show(bImg.Width.ToString() + "," + bImg.Height.ToString()); /* if (bImg.Height > 100 || bImg.Width > 100) { img.Height = bImg.Height * 0.2; img.Width = bImg.Width * 0.2; } */ img.Stretch = Stretch.Uniform; // new InlineUIContainer(img, richTextBox1.Selection.Start); // } }