Routed Input Events(2) 译

代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApplication2
{
    
/// <summary>
    
/// App.xaml 的交互逻辑
    
/// </summary>
    public partial class App : Application
    {
        
static readonly FontFamily fontfam = new FontFamily("Lucida Console");
        
const string strFormat = "{0,-30} {1,-15} {2,-15} {3,-15}";
        StackPanel stackOutput;
        DateTime dtlast
=DateTime.Now;
        [STAThread]
        
public static void main()
        {
            App app 
= new App();
            app.Run();
        }
        
protected override void OnStartup(StartupEventArgs e)
        {
            
base.OnStartup(e);

            Window win 
= new Window();
            win.Title 
= "Examine Routed Enents";

            Grid grid 
= new Grid();
            win.Content 
= grid;

            RowDefinition rowdef 
= new RowDefinition();
            rowdef.Height 
= GridLength.Auto;
            grid.RowDefinitions.Add(rowdef);

            rowdef 
= new RowDefinition();
            rowdef.Height 
= GridLength.Auto;
            grid.RowDefinitions.Add(rowdef);

            rowdef 
= new RowDefinition();
            rowdef.Height 
= new GridLength(100, GridUnitType.Star);
            grid.RowDefinitions.Add(rowdef);

            Button btn 
= new Button();
            
            btn.HorizontalAlignment 
= HorizontalAlignment.Center;
            btn.Margin 
= new Thickness(24);
            btn.Padding 
= new Thickness(24);
            grid.Children.Add(btn);


            TextBlock text 
= new TextBlock();
            text.FontSize 
= 24;
            text.Text 
= win.Title;

            btn.Content 
= text;

            TextBlock textHeadings 
= new TextBlock();
            textHeadings.FontFamily 
= fontfam;
            textHeadings.Inlines.Add(
new Underline(new Run(String.Format(strFormat,"Routed Event","sender","Source","OriginalSource"))));

            grid.Children.Add(textHeadings);
            Grid.SetRow(textHeadings,
1);

            ScrollViewer scroll 
= new ScrollViewer();
            grid.Children.Add(scroll);

            Grid.SetRow(scroll,
2);

            stackOutput 
= new StackPanel();
            scroll.Content 
= stackOutput;

            UIElement[] els 
= { win,grid,btn,text};
            
foreach(UIElement el in els )
            {
                
//键盘

                el.PreviewKeyDown 
+= AllPurposeEventHandler;
                el.PreviewKeyUp 
+= AllPurposeEventHandler;
                el.PreviewTextInput 
+= AllPurposeEventHandler;
                el.KeyDown 
+= AllPurposeEventHandler;
                el.KeyUp 
+= AllPurposeEventHandler;
                el.TextInput 
+= AllPurposeEventHandler; 
                
//鼠标
                el.MouseDown += AllPurposeEventHandler;
                el.MouseUp 
+= AllPurposeEventHandler;
                el.PreviewMouseDown 
+= AllPurposeEventHandler;
                el.PreviewMouseUp 
+= AllPurposeEventHandler;
                
//click
                el.AddHandler(Button.ClickEvent,new RoutedEventHandler(AllPurposeEventHandler));
            }
            win.Show();
        }
        
void AllPurposeEventHandler(object sender, RoutedEventArgs args)
        {
            DateTime dtnow 
= DateTime.Now;
            
if (dtnow - dtlast > TimeSpan.FromMilliseconds(1000))
            {
                stackOutput.Children.Add(
new TextBlock(new Run(" ")));}


                dtlast 
= dtnow;
                TextBlock text 
= new TextBlock();
                text.FontFamily 
= fontfam;
                text.Text 
= string.Format(strFormat,args.RoutedEvent.Name,TypeWithoutNamespace(sender),TypeWithoutNamespace(args.Source),TypeWithoutNamespace(args.OriginalSource));
                stackOutput.Children.Add(text);
                (stackOutput.Parent 
as ScrollViewer).ScrollToBottom();
            
        }
        
string TypeWithoutNamespace(object obj)
        {
            
string[] astr = obj.GetType().ToString().Split('.');
            
return astr[astr.Length-1];
        }

    }
}

 

posted on 2010-05-26 17:16  孟凡龙  阅读(239)  评论(0)    收藏  举报