61 wpf Toggle Bold & Itlic

来源:互联网 发布:ios ar源码 编辑:程序博客网 时间:2024/05/01 16:50

 

 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;


namespace SayHello
{
 public  class ToggleBoldAndItlic:Window{
  
       [STAThread]
     public static void Main()
     {
         Application app=new Application();
         app.Run(new ToggleBoldAndItlic());
     }
       public ToggleBoldAndItlic()
       {
           Title = "Toggle Bold & Itlic";
          TextBlock txt=new TextBlock();
           txt.FontSize = 32;
           txt.HorizontalAlignment=HorizontalAlignment.Center;
           txt.VerticalAlignment=VerticalAlignment.Center;
           Content = txt;

           string strQuote = " To be.or not to be,or not to be,that is the question";
           string[] strWords = strQuote.Split();
           foreach (var str in strWords)
           {
               Run run=new Run(str);
               run.MouseDown += RunOnMouseDown;
               txt.Inlines.Add(run);
               txt.Inlines.Add(" ");
           }
       }
     void RunOnMouseDown(object sender,MouseButtonEventArgs args)
     {
         Run run=new Run();
         if(args.ChangedButton==MouseButton.Left)
         {
             run.FontStyle = run.FontStyle == FontStyles.Italic
                                 ? FontStyles.Normal
                                 : FontStyles.Italic;

         }
         if (args.ChangedButton==MouseButton.Right)
         {
             run.FontWeight = run.FontWeight == FontWeights.Bold
                 ? FontWeights.Normal : FontWeights.Bold;
         }
     }
 }
}