The moment to create something like a game as NeoPets in WPF is coming, today I have made improvements using controls, here I show you how to add movement to the controls on runtime, I have add moving using the mouse and using the keyboard, download the source here
To do that, in the xaml file add to the button:
button keydown="OnKey" previewmousemove="OnMove"
And in the .cs add the next methods:
public void OnMove(object o, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
b = (Button)o;
SetZ();
Canvas.SetZIndex(b, 99);
b.Margin = new Thickness(e.MouseDevice.GetPosition(null).X - b.Width / 2, e.MouseDevice.GetPosition(null).Y - b.Height/2, b.Margin.Right, b.Margin.Bottom);
}
}
and for using the keyboard:
public void OnKey(object o, KeyEventArgs e)
{
int delta = 20;
b = (Button)o;
SetZ();
Canvas.SetZIndex(b, 99);
switch (e.Key)
{
case Key.W:
b.Margin = new Thickness(b.Margin.Left, b.Margin.Top - delta, b.Margin.Right, b.Margin.Bottom);
break;
case Key.S:
b.Margin = new Thickness(b.Margin.Left, b.Margin.Top + delta, b.Margin.Right, b.Margin.Bottom);
break;
case Key.A:
b.Margin = new Thickness(b.Margin.Left - delta, b.Margin.Top, b.Margin.Right, b.Margin.Bottom);
break;
case Key.D:
b.Margin = new Thickness(b.Margin.Left + delta, b.Margin.Top, b.Margin.Right, b.Margin.Bottom);
break;
default:
break;
}
}
Apparently is easy, the matter about all of this, is that there's only few docs, so I hope you find this useful and encourage you to improve your apps (uis).
1 comment:
The link to the source code doesn't work anymore, could you maybe upload it again?
Post a Comment