Run in WPF. I think this name is really not good. There is a run in the application and a textblock in it.

Source: Internet
Author: User

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>
/// Exaggerated. The key is that events can be added to each word .. Run or inherited from 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 = verticalignment. 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>
/// Click an event
/// </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>
/// Character Input
/// </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;

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.