大可山博客[十年一日, GDI+,WPF, .Net图形图像]

WPF,WinForms,asp.net开发,图形图像处理系统研究 (Johnson Blog) [信奉:凡事靠自己] MSN:a3news(at)hotmail.com http://www.brawdraw.com

用户操作
[即时聊天] [发私信] [加为好友]
朱继山ID:johnsuna
421978次访问,排名113好友60人,关注者101
深圳报业集团问工网,技术总监
johnsuna的文章
原创 258 篇
翻译 1 篇
转载 44 篇
评论 462 篇
大可山(Johnson)的公告

MSN:a3news(at)hotmail.com,从2007年8月8日起笔名改为:大可山(以前叫阿山Net)
Q:329325120
[这段时间忙,尽量少问我问题。见谅!]
引用本人原作,请注明出处。
最近评论
mkyj3d:老大,GMAIL可以用JMAIL组件来发送邮件吗?
lsbbox:请问Open/Close这类需要自己实现的Command和直接给菜单添加一个Click事件处理函数有什么区别呢?貌似后者写起来还简单很多
lsbbox:请问Open/Close这类需要自己实现的Command和直接给菜单添加一个Click事件处理函数有什么区别呢?貌似后者写起来还简单很多
cctvcomchn:好.
messageto:[这段时间忙,尽量少问我问题。见谅!]
引用本人原作,请注明出处。


------------------------
牛人
文章分类
收藏
相册
Chinaphotoshop.Net
报业大厦外摄
程序截图
美丽的深圳
世界之窗
软件开发
你的灯亮着吗?(RSS)
图书出版
大溪水的博客(RSS)
图形图像
C#新型报表工具 XDesigner(RSS)
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

转载 WPF中RichTextBox的使用小窍门(翻译、整理)收藏

新一篇: WPF控件按分类汇总 | 旧一篇: 深入WPF中的图像画刷(ImageBrush)之2——ImageBrush的铺设方式

原文在此:http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx 

这里仅整理出其中的知识点:
1. 取得已被选中的内容:
(1)使用 RichTextBox.Document.Selection属性
(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text

2. 在XAML中增加内容给RichTextBox:
<RichTextBox IsSpellCheckEnabled="True">
   <FlowDocument>
        <Paragraph>
<!-- 这里加上你的内容 -->
          This is a richTextBox. I can <Bold>Bold</Bold>, <Italic>Italicize</Italic>, <Hyperlink>Hyperlink stuff</Hyperlink> right in my document.
        </Paragraph>
   </FlowDocument>
</RichTextBox>

3. 缩短段间距,类似<BR>,而不是<P>
方法是使用Style定义段间距:
    <RichTextBox>
        <RichTextBox.Resources>
          <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="0"/>
          </Style>
        </RichTextBox.Resources>
        <FlowDocument>
          <Paragraph>
            This is my first paragraph... see how there is...
          </Paragraph>
          <Paragraph>
            a no space anymore between it and the second paragraph?
          </Paragraph>
        </FlowDocument>
      </RichTextBox>

4. 从文件中读出纯文本文件后放进RichTextBox或直接将文本放进RichTextBox中:
private void LoadTextFile(RichTextBox richTextBox, string filename)
{
    richTextBox.Document.Blocks.Clear();
    using (StreamReader streamReader = File.OpenText(filename)) {
           Paragraph paragraph = new Paragraph();
           paragraph.Text = streamReader.ReadToEnd();
           richTextBox.Document.Blocks.Add(paragraph);
    }
}

private void LoadText(RichTextBox richTextBox, string txtContent)
{
    richTextBox.Document.Blocks.Clear();
    Paragraph paragraph = new Paragraph();
    paragraph.Text = txtContent;
    richTextBox.Document.Blocks.Add(paragraph);
}
5. 取得指定RichTextBox的内容:
private string GetText(RichTextBox richTextBox)
{
        TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
        return textRange.Text;
}
6. 将RTF (rich text format)放到RichTextBox中:
        private static void LoadRTF(string rtf, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(rtf)) {
                throw new ArgumentNullException();
            }
            TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            using (MemoryStream rtfMemoryStream = new MemoryStream()) {
                using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream)) {
                    rtfStreamWriter.Write(rtf);
                    rtfStreamWriter.Flush();
                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);
                    //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                }
            }
        }
7. 将文件中的内容加载为RichTextBox的内容
        private static void LoadFile(string filename, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(filename)) {
                throw new ArgumentNullException();
            }
            if (!File.Exists(filename)) {
                throw new FileNotFoundException();
            }
            using (FileStream stream = File.OpenRead(filename)) {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml",true) == 0) {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0) {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Load(stream, dataFormat);
            }       
        }
8. 将RichTextBox的内容保存为文件:
        private static void SaveFile(string filename, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(filename)) {
                throw new ArgumentNullException();
            }
            using (FileStream stream = File.OpenWrite(filename)) {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml", true) == 0) {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0) {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Save(stream, dataFormat);
            }
        }
9. 做个简单的编辑器:
  <!-- Window1.xaml -->
  <DockPanel>
    <Menu DockPanel.Dock="Top">
      <MenuItem Header="_File">
        <MenuItem Header="_Open File" Click="OnOpenFile"/>
        <MenuItem Header="_Save" Click="OnSaveFile"/>
        <Separator/>
        <MenuItem Header="E_xit" Click="OnExit"/>
      </MenuItem>     
    </Menu>
    <RichTextBox Name="richTextBox1"></RichTextBox>    
  </DockPanel>
        // Window1.xaml.cs
        private void OnExit(object sender, EventArgs e) {
            this.Close();
        }
        private void OnOpenFile(object sender, EventArgs e) {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == true) {
                LoadFile(ofd.SafeFileName, richTextBox1);
            }
        }
        private void OnSaveFile(object sender, EventArgs e) {
            Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
            sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
            if (sfd.ShowDialog() == true) {
                SaveFile(sfd.SafeFileName, richTextBox1);
            }
        }

发表于 @ 2007年09月05日 19:07:00|评论(loading...)|编辑

新一篇: WPF控件按分类汇总 | 旧一篇: 深入WPF中的图像画刷(ImageBrush)之2——ImageBrush的铺设方式

评论

#majicool 发表于2008-04-28 15:59:02  IP: 218.106.60.*
谢谢啦,很有帮助
#f26511314 发表于2008-07-17 15:23:34  IP: 221.222.201.*
如何让水平滚动条自动显示?
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 大可山(Johnson)