WPF 里面的 Run .感觉这名称真没取好,application 里面有个 run, textblock 里面也有个....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Reflection;
using System.Windows.Input;

using System.Windows.Documents;


namespace WpfApplication1
{
    class Test : Window
    {

        string str=null;

        [STAThread]
        static void Main()
        {

            Application app = new Application();
            app.Run(new Test());
        }
        /// <summary>
        /// 有些夸张,关键是每个字都可以加事件。。Run 还是继承自 UElement;
        /// </summary>
        public Test()
        {
            Content = str;

            Title = "Format the Text";

            TextBlock txt = new TextBlock();

            txt.FontFamily = new System.Windows.Media.FontFamily("微软雅黑");
            txt.FontSize = 48;

            txt.HorizontalAlignment = HorizontalAlignment.Center;
            txt.VerticalAlignment = VerticalAlignment.Center;

            Content = txt;

            string strquote = "To be,or not to be, that is the question"+Environment.NewLine+"Click Me!!";
            string[] strwords = strquote.Split();

            foreach (var item in strwords)
            {
                Run run = new Run(item);
          
                run.MouseDown += new MouseButtonEventHandler(run_MouseDown);
                txt.Inlines.Add(run);
                txt.Inlines.Add(" ");
               
            }
            Content = txt;
        }
        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void run_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Run run = sender as Run;
            if (e.ChangedButton == MouseButton.Left)
            {
                run.FontStyle = run.FontStyle == FontStyles.Italic ? FontStyles.Normal : FontStyles.Italic;
            }
            if (e.ChangedButton == MouseButton.Right)
            {
                run.FontWeight= run.FontWeight == FontWeights.Bold ? FontWeights.Normal : FontWeights.Bold;
            }
        }
        /// <summary>
        /// 字符输入
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            base.OnTextInput(e);
           str = (string)Content;
            if (e.Text == "\b")
            {
                if (str.Length > 0)
                {
                    str = str.Substring(0, str.Length - 1);
                }

            }
            else
            {
                str += e.Text;
            }

            //Content = str;
           


        }

    }

}

转载于:https://www.cnblogs.com/fat_li/archive/2011/08/19/2145707.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值