HWH....

导航

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Documents;
using System.Windows.Input;

namespace WindowTest
{

    public class ApplicationTest : Application
    {
        //当进行关机等操作的时候,会触发这人事件。
        protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
        {
            base.OnSessionEnding(e);

            MessageBoxResult result =
                   MessageBox.Show("Do you want to save your data?",
                                   MainWindow.Title,
                                   MessageBoxButton.YesNoCancel,
                                   MessageBoxImage.Question,
                                   MessageBoxResult.Yes);
            e.Cancel = (result == MessageBoxResult.Cancel);
        }

        //Application开始运行时执行。
        protected override void OnStartup(StartupEventArgs args)
        {
            base.OnStartup(args);
            Window win = new Window();
            win.Title = "Inherit the Application!";
            win.Show();
        }
    }

    class SimpleEllipse : FrameworkElement
    {
        /// <summary>
        /// OnRender 方法可以被重写以将其他图形元素(未事先在逻辑树中定义)添加
        /// 至所呈现的元素,如效果或装饰器。 DrawingContext 对象作为参数传递,
        /// 该参数可以为绘制形状、文本、图像或视频提供方法。
        /// Important
        /// 调用此方法时,不直接使用此元素的呈现指令,而是将其保留供布局和绘制在以后异步使用。
        /// </summary>
        /// <param name="dc"></param>
        protected override void OnRender(DrawingContext dc)
        {
            dc.DrawEllipse(Brushes.Blue,
                new Pen(Brushes.Red, 24),
                new Point(RenderSize.Width / 2, RenderSize.Height / 2),
                RenderSize.Width / 2,
                RenderSize.Height / 2);
        }
    }



    public class WindowTest : Window
    {
        [STAThread]
        public static void Main()
        {
            ApplicationTest app = new ApplicationTest();
            app.ShutdownMode = ShutdownMode.OnMainWindowClose;      //程序关闭模式
            app.Run(new WindowTest());
        }

        public WindowTest()
        {

            //Properties
            Title = "WPF Window class Test!";   //标题
            ShowInTaskbar = true;               //任务栏中是否显示
            Width = 500;                        //宽度
            Height = 400;                       //高度

            Width = SystemParameters.PrimaryScreenWidth;    //与屏幕一样宽
            Height = SystemParameters.PrimaryScreenHeight;  //与屏幕一样高
            //工作区的同大小,最大化。
            Width = SystemParameters.WorkArea.Width;        //与工作区一样宽
            Height = SystemParameters.WorkArea.Height;      //与工作区一样高

            Left = 100;     //位置左
            Top = 100;      //位置上
            //工作区的左上位 
            Left = SystemParameters.WorkArea.Left;
            Top = SystemParameters.WorkArea.Top;

            WindowStartupLocation = WindowStartupLocation.CenterScreen; //Window在工作区中间
            ResizeMode = ResizeMode.CanResizeWithGrip;                  //此选项的功能与 CanResize 相同,
            //但在窗口右下角添加了一个“大小调整手柄”。 

            WindowStyle = WindowStyle.SingleBorderWindow;               //Window的样式,为三
            WindowState = WindowState.Normal;                           //Window的状态。

            Topmost = false;    //总在最前否?                                

            Background = new LinearGradientBrush(Colors.Black, Colors.Green, new Point(0, 0), new Point(1, 1)); //背景,Brush类
            Foreground = Brushes.Red;                                                                           //前景,Brush类

            //RadialGradientBrush类运用
            RadialGradientBrush tempBorderBrush = new RadialGradientBrush();
            tempBorderBrush.Center = tempBorderBrush.GradientOrigin = new Point(0.5, 0.5);  //中心点,与渐变的原的位置为窗口中心
            tempBorderBrush.RadiusX = tempBorderBrush.RadiusY = 0.5;
            tempBorderBrush.GradientStops.Add(new GradientStop(Colors.Black, 0));           //添加色变点
            tempBorderBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.5));
            tempBorderBrush.GradientStops.Add(new GradientStop(Colors.Red, 1));
            tempBorderBrush.SpreadMethod = GradientSpreadMethod.Repeat;                     //散布方式,

            //边框
            BorderBrush = tempBorderBrush;                //边框,Brush类         
            BorderThickness = new Thickness(10);          //边框的厚度


            FontFamily = new FontFamily("Times New Roman"); //字体
            FontSize = 48;                                              //字体的太小48*(3/4)=36
            FontStyle = FontStyles.Normal;                              //字体的样式,FontStyle.Normal for default
            FontWeight = FontWeights.Normal;                            //字体的粗重
            FontStretch = FontStretches.UltraExpanded;

            //Content属性的处理
            AddSomeObjectIntoContent();

            SizeToContent = SizeToContent.Manual;    //有内容了,所以现在可以安内容来变窗口的大小了。
        }

        //Events.. 
        //鼠标的点下事件。
        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            Title = "WPF Window class Test! ChangeButton:" +
                e.ChangedButton.ToString() + "    " +       //哪个键被点了
                e.ClickCount.ToString() + "    " +          //点鼠标的次数
                e.OriginalSource + "   " +                  //源
                e.ButtonState + "  " +                      //鼠标的状态
                e.Timestamp + "     " +                     //时间
                e.GetPosition(this).ToString();             //点

            e.Handled = false;                              //不让MouseDown的事件处理方法执行


        }

        protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            base.OnTextInput(e);

            //InputStringBuilderToContent();

            //InpupStringToContent(e);

        }


        //Methods... 
        private void AddSomeObjectIntoContent()
        {
            //AddStringIntoContent();

            //AddStringBuilderIntoContent();

            //AddImageIntoContent();

            //AddEllipseIntoContent();

            //AddTextBlockIntoContent();

            //AddShapeIntoContent();

            Button btn = new Button();
            //btn.Background = Brushes.Beige;
            btn.Foreground = Brushes.Plum;
            btn.BorderBrush = Brushes.Purple;
            //btn.BorderThickness = new Thickness(50);      //No effect

            btn.Content = "_Click me!";                     //_ is very important char for Button...
            btn.Click += new RoutedEventHandler(btn_Click);
            btn.Focus();
            btn.IsDefault = true;                           //input Enter invoke the Click Event
            btn.IsCancel = true;                            //input Escape  invoke the Click Event
            btn.Margin = new Thickness(96);
            btn.HorizontalContentAlignment = HorizontalAlignment.Left;
            btn.VerticalContentAlignment = VerticalAlignment.Top;
            btn.Padding = new Thickness(96);
            btn.HorizontalAlignment = HorizontalAlignment.Center ; //Stretch for default
            btn.VerticalAlignment = VerticalAlignment.Center;     //Stretch for default
            //btn.Width = 96;                                     //The less explicitly you set the sizes of controls, the happier you'll be.
            //btn.Height = 96;
            btn.FontFamily = new FontFamily("Times New Roman");
            btn.FontSize = 96;

            Content = btn;

        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            btn.FontStyle = FontStyles.Italic;
            this.Close();
        }

        private void AddShapeIntoContent()
        {
            SimpleEllipse ellipse = new SimpleEllipse();
            ellipse.Width = ellipse.Height = 100;

            Content = ellipse;
        }

        private void AddTextBlockIntoContent()
        {
            TextBlock txt = new TextBlock();
            txt.FontSize = 32;              // 24 points
            txt.Inlines.Add("This is some ");                               //加入string
            txt.Inlines.Add(new Italic(new Run("italic")));                 //加入Inline类的子类。Intalic
            txt.Inlines.Add(" text, \nand this is some ");
            txt.Inlines.Add(new Bold(new Run("bold")));                     //加入Inline类的子类。Bold
            txt.Inlines.Add(" text, \nand let's cap it off with some ");
            //加入Inline类的子类:Bold,可以想像一下这里用的是什么设计模式?Decorator
            txt.Inlines.Add(new Hyperlink(new Underline(new Bold(new Italic(new Run("bold italic"))))));
            txt.Inlines.Add(" text.");
            txt.TextWrapping = TextWrapping.Wrap;                           //有边框
            txt.HorizontalAlignment = HorizontalAlignment.Center;           //中间
            txt.VerticalAlignment = VerticalAlignment.Center;

            Content = txt;

            string strQuote = "To be, or not to be, that is the question";
            string[] strWords = strQuote.Split();                           //字符串分解

            foreach (string str in strWords)
            {
                Run run = new Run(str);
                run.Background = Brushes.Gray;
                run.MouseDown += RunOnMouseDown;
                txt.Inlines.Add(run);
                txt.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;
        }

        private void AddEllipseIntoContent()
        {
            Width = 600;
            Height = 500;

            Ellipse ellipse = new Ellipse();
            ellipse.Fill = Brushes.Black;               //这个椭圆中间添黑色
            ellipse.StrokeThickness = 96;               //线厚为1英寸   
            ellipse.Stroke = new LinearGradientBrush(Colors.CadetBlue, Colors.Chocolate, new Point(1, 0), new Point(0, 1));  //线为线性示放射
            ellipse.Width = 400;                        //宽
            ellipse.Height = 400;                       //高
            ellipse.HorizontalAlignment = HorizontalAlignment.Stretch;  //位置
            ellipse.VerticalAlignment = VerticalAlignment.Stretch;
            Content = ellipse;
        }

        private void AddImageIntoContent()
        {
            Width = 600;
            Height = 400;
            Uri uri = new Uri("http://www.google.com.hk/intl/zh-CN/images/logo_cn.png");
            BitmapImage bitmap = new BitmapImage(uri);
            Image img = new Image();
            img.Source = bitmap;                                        //位图原
            img.Stretch = Stretch.Uniform;                              //Default,伸缩
            img.StretchDirection = StretchDirection.Both;               //全伸缩,(还小可缩,大可伸两个项)
            img.HorizontalAlignment = HorizontalAlignment.Center;       //水平位置为Center
            img.VerticalAlignment = VerticalAlignment.Center;           //垂直位置为Center
            img.Margin = new Thickness(10);                             //与Border的各个方向的距离是50
            img.Width = 600;                                            //img的宽,看到窗口Width与img的Width是相等了,所以图片显示
            //的区域是600-2*BorderThickness-2*img.Margin
            img.Height = 400;                                           //img的高,
            img.Opacity = 0.5;                                          //透明度
            img.LayoutTransform = new RotateTransform(45);
            Content = img;
        }

        private void AddStringBuilderIntoContent()
        {
            //StringBuilder.加入StringBuilder类型到Content中,注意OnTextInput方法中对这个Content的更新。
            StringBuilder stb = new StringBuilder();
            stb.Append("111111sdfjlasjdfl\n");
            stb.Append("22222222sdfasljd\n");
            Content = stb;
        }

        private void InputStringBuilderToContent()
        {
            //接收StringBuilder类型的Content。
            StringBuilder stb = new StringBuilder();
            stb = Content as StringBuilder;
            if (stb != null)
            {
                stb.Append("333333333ldjfsdlfkjsldfjals\n");
                Content = null;             //这一句是必须有的,如果没有的话,系统会认为内容没有变化,
                Content = stb;
            }
        }

        private object AddStringIntoContent()
        {
            return Content = "Hello World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
        }

        private void InpupStringToContent(System.Windows.Input.TextCompositionEventArgs e)
        {
            string str = Content as string;
            str += e.Text;
            Content = str;
        }

    }
}
posted on 2011-03-23 14:08  HWH....  阅读(993)  评论(0编辑  收藏  举报