zoukankan      html  css  js  c++  java
  • 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;
               


            }

        }

    }

  • 相关阅读:
    js验证表单大全
    JavaScript验证表单大全
    ASP.NET(c#)操作cookie、session、cache工具类
    AIX查看硬件配置
    SAP* DDIC密码丢失如何处理
    如何学好SAP BASIS
    SAP STMS 传输系统配置
    BOM展开实例
    免安装Oracle客户端使用PL/SQL连接Oracle
    入门培训SAP操作手册 之前台操作
  • 原文地址:https://www.cnblogs.com/fat_li/p/2145707.html
Copyright © 2011-2022 走看看