WPF程序设计指南第四章FormatTheButton例子效果

时间:2023-02-19 22:41:48

该例子能在鼠标移入按钮区域时使部分字符对象变色

代码如下:

 

//------------------------------------------------

// FormatTheButton.cs (c) 2006 by Charles Petzold

//------------------------------------------------

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

 

namespace Petzold.FormatTheButton

{

    public class FormatTheButton : Window

    {

        Run runButton;

 

        [STAThread]

        public static void Main()

        {

            Application app = new Application();

            app.Run(new FormatTheButton());

        }

        public FormatTheButton()

        {

            Title = "Format the Button";

 

            // Create the Button and set as window content.

            Button btn = new Button();

            btn.HorizontalAlignment = HorizontalAlignment.Center;

            btn.VerticalAlignment = VerticalAlignment.Center;

            btn.MouseEnter += ButtonOnMouseEnter;

            btn.MouseLeave += ButtonOnMouseLeave;

            Content = btn;

 

            // Create the TextBlock and set as button content.

            TextBlock txtblk = new TextBlock();

            txtblk.FontSize = 24;

            txtblk.TextAlignment = TextAlignment.Center;

            btn.Content = txtblk;

 

            // Add formatted text to the TextBlock.

            txtblk.Inlines.Add(new Italic(new Run("Click")));

            txtblk.Inlines.Add(" the ");

            txtblk.Inlines.Add(runButton = new Run("button"));

            txtblk.Inlines.Add(new LineBreak());

            txtblk.Inlines.Add("to launch the ");

            txtblk.Inlines.Add(new Bold(new Run("rocket")));

        }

        void ButtonOnMouseEnter(object sender, MouseEventArgs args)

        {

            runButton.Foreground = Brushes.Red;

        }

        void ButtonOnMouseLeave(object sender, MouseEventArgs args)

        {

            runButton.Foreground = SystemColors.ControlTextBrush;

        }

    }

}

WPF程序设计指南第四章FormatTheButton例子效果

WPF程序设计指南第四章FormatTheButton例子效果