现在的位置: 首页 > 综合 > 正文

WPF学习笔记 一个小示例

2012年12月01日 ⁄ 综合 ⁄ 共 1786字 ⁄ 字号 评论关闭

using System;
using System.Windows;
using System.Windows.Input;

namespace xiaohai.TextInput
{
    class Example : Application 
    {
        [STAThread]
        public static void Main()
        {
        
            Example app = new Example();
            app.ShutdownMode = ShutdownMode.OnMainWindowClose;
            app.Run();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Window winMain = new Window();
            winMain.Title = "Main Window";
           
            for (int i = 0; i < 2; i++)
            {
                Window win = new Window();
                win.Width = win.Height = 300;
                win.Title = "Window "+ i.ToString ();
                win.KeyDown += WindowOnKeyDown;
                win.TextInput += WindowOnTextInput;
                win.Show();
            }
            winMain.Show();
        }

        static void WindowOnTextInput(object sender, TextCompositionEventArgs e)
        {
            Window w=sender as Window ;
            if (e.Text == "/b" && w.Content.ToString().Length > 0)
                w.Content = e.Text.Substring(0, w.Content.ToString().Length - 1);
            else if (e.Text.Length > 0 && !char.IsControl(e.Text[0]))
                w.Content += e.Text;a
 
        }
        static void WindowOnKeyDown(object sender, KeyEventArgs e)
        {
            Window w = sender as Window;
            if (e.Key == Key.Up)
            {
                w.Top -= 0.05 * w.Height;

            }
            else if (e.Key == Key.Down)

                w.Top += 0.05 * w.Height;
            else if (e.Key == Key.Left)
                w.Left -= 0.1 * w.Width;
            else if (e.Key == Key.Right)
                w.Left += 0.1 * w.Width;

            else if (e.Key == Key.Enter)
            {
                w.Left = SystemParameters.WorkArea.Width/2 - w.Width/2;
                w.Top = SystemParameters.WorkArea .Height /2 - w.Height/2;
            }
           
        }
     
    }
}

【上篇】
【下篇】

抱歉!评论已关闭.