WPF之鼠标事件

前台默认即可

后台代码:

  private void Window_Loaded(object sender, RoutedEventArgs e)         {             TextBlock text = new TextBlock();             text.FontSize = 32;             text.HorizontalAlignment = HorizontalAlignment.Center;             text.VerticalAlignment = VerticalAlignment.Center;             Content = text;

            string strQuote = "to be ,is not to be ,that is the question ";             string[] Words = strQuote.Split();             foreach (string str in Words)             {                 Run run = new Run(str);                 run.MouseDown += RunOnMouseDown;                 text.Inlines.Add(run);                 text.Inlines.Add(" ");

            }                    }

        void RunOnMouseDown(object sender, MouseButtonEventArgs args)         {

            Run run = sender as 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;
            }
        }

你可能感兴趣的:(编程墨法)