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;
}
}
}
}