関連記事| WPFって何なんだぜ☆(^~^)?
「 フォームの初期化のイベントハンドラーを使うには
↓MainWindow とかいてあるところをクリックして Loaded をダブルクリックすればいいのかだぜ☆?」
「 ↑リッチ・テキスト・ボックスのAPIが さっぱり 見たことのない体系だぜ☆」
private void Window_Loaded(object sender, RoutedEventArgs e)
{
StackPanel myStackPanel = new StackPanel();
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Create a Run of plain text and some bold text.
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
// Add the paragraph to the FlowDocument.
myFlowDoc.Blocks.Add(myParagraph);
// Add initial content to the RichTextBox.
richTextBox.Document = myFlowDoc;
// すでに接続済み。何のこっちゃ☆(^~^)
// myStackPanel.Children.Add(richTextBox);
// this.Content = myStackPanel;
}
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
「 ↑Run
がプレーンなテキストで、Bold
でラッピングすると太字になるみたいだな☆」
「 なんで プレーンなテキストは Run
なの? なんで HTML の Span
タグ に合わせないの?」
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
「 ↑Paragraph
というのは段落だな☆ Inlines
というのは長方形じゃなくて画面端で改行する直線みたいなやつだろ☆
そこにRun
を Add していけばいいみたいだぜ☆」
FlowDocument myFlowDoc = new FlowDocument();
myFlowDoc.Blocks.Add(myParagraph);
「 ↑Document
というのは文書全体のことだな☆ Blocks
というのは長方形か何かだろ☆ そこにParagraph
を追加しようぜ☆?」
richTextBox.Document = myFlowDoc;
「 ↑そのDocument
を RichTextBox にセットしろだぜ☆」
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
{
// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
{
// Create a Run of plain text and some bold text.
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
}
// Add the paragraph to the FlowDocument.
myFlowDoc.Blocks.Add(myParagraph);
}
// Add initial content to the RichTextBox.
richTextBox.Document = myFlowDoc;
}
System.Windows.Documents Namespace
「 ↑この名前空間から探せばいいと思うんだが、画像が欲しいんだぜ☆」
var hyperl = new Hyperlink(new Run("Link Text."));
hyperl.NavigateUri = new Uri("http://search.msn.com");
myParagraph.Inlines.Add(hyperl);
「 ↑ハイパーリンクは有名だろ☆ 使ってみようぜ☆?
あれ☆? ハイパーテキスト みたいな見た目をしているが クリックしても ブラウザが出てくるわけでもない……☆
なんかもっと設定がいるのか……☆」
var myItalic = new Italic(new Run("Italic"));
myParagraph.Inlines.Add(myItalic);
myParagraph.Inlines.Add(new LineBreak());
var underline = new Underline(new Run("Hello!"));
myParagraph.Inlines.Add(underline);
wpf – FlowDocument内の波状の下線
Painting with Images, Drawings, and Visuals
var highlight = new Run("Good morning!");
highlight.Foreground = Brushes.White;
highlight.Background = Brushes.Blue;
myParagraph.Inlines.Add(highlight);
「 ↑色を変えるだけなら ブラシを設定するだけだぜ☆
選択範囲の機能が欲しいなら Selection か何かを使うんだろ☆」
var large = new Run("Yes.");
large.FontSize += 8.0;
myParagraph.Inlines.Add(large);
「 ?
リッチ・テキスト・ボックスには .Text
プロパティが無いんだけど?
テキストの全文って どうやって 引っこ抜くの?」
RichTextBox (WPF) does not have string property “Text”
「 昔のWindowsアプリは マウス操作のようにして ウィジェットからプロパティを取れだぜ☆
↓例えば マウスで先頭から最後尾まで引っ張ると 文字を取れる☆」
string plainText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
Trace.WriteLine(plainText);
「 ↑方眼紙に入れたように 文字の桁を揃えたいんだけど どうやんの?」
「 むしろ MS-DOS の頃は全部 きれいな 等幅(とうふく)フォントだったんだけどな☆
現代では 良い方法は無くなってしまったが、 MS ゴシック
は 等幅になってくれる時期がある☆
勝手に Microsoft に更新されて 空白がずれる時期もあるが……☆」
// なるべく等幅なフォント指定
richTextBox.FontFamily = new FontFamily("MS ゴシック");
「 何だぜ、ウィンドウを広げたら リッチ・テキスト・ボックス は自動的にリサイズしてくれるんじゃ ないのかだぜ☆?」
「 クリックしたら クリップみたいなマークが くっついた……☆」
「 これで枠と くっつく のかだぜ☆ ボタンにも設定しないといけないな☆」
「 テキストを パースして こんなタグやらDOMみたいなのを いちいち設定していくの?」
<書きかけ>
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント