MainPage.xaml
<UserControl x:Class="MovingTheSquare.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Image Height="400" Name="image1" Stretch="None" Width="600"/>
<TextBlock x:Name="text1" Text="Click on the square and drag it."
Height="25" Margin="260,-350,0,0"></TextBlock>
</Grid>
</UserControl>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging; // for WriteableBitmap
using System.Windows.Threading; // for DispatcherTimer
namespace MovingTheSquare
{
public partial class MainPage : UserControl
{
WriteableBitmap m_bitmap;
DateTime m_LastUpdate;
int x = 300, y = 200; // initial position of the box
int width = 600;
int height = 400;
int isClicked = 0; // isClicked = 1 means being clicked
int x_old, y_old;
int a = 10; // half of the square line
public MainPage()
{
InitializeComponent();
this.MouseLeftButtonDown += new MouseButtonEventHandler(MainPage_MouseLeftButtonDown);
this.MouseLeftButtonUp += new MouseButtonEventHandler(MainPage_MouseLeftButtonUp);
this.MouseMove += new MouseEventHandler(MainPage_MouseMove);
m_bitmap = new WriteableBitmap(width, height);
image1.Source = m_bitmap;
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new System.TimeSpan(0, 0, 0, 0, 1);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
m_LastUpdate = DateTime.Now;
}
void timer_Tick(object sender, EventArgs e)
{
UpdateScene();
}
private void UpdateScene()
{
DateTime now = DateTime.Now;
float deltaTime = (float)now.Subtract(m_LastUpdate).TotalSeconds;
m_LastUpdate = now;
// setting the background
for (int i = 0; i < width * height; i++)
{
m_bitmap.Pixels[i] = RGBA(0,200,0,255);
}
DrawTheSquare(x, y);
m_bitmap.Invalidate();
}
private int RGBA(byte byRed, byte byGreen, byte byBlue, byte alpha)
{
return (alpha * 16777216) + (byRed * 65536) + (byGreen * 256) + byBlue;
}
private void DrawTheSquare(int x, int y)
{
for (int j = (y - a); j < (y + a); j++)
{
for (int i = (x - a); i < (x + a); i++)
{
m_bitmap.Pixels[j*width + i] = RGBA(0, 0, 200, 255);
}
}
}
private void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
x_old = (int)(e.GetPosition(image1).X);
y_old = (int)(e.GetPosition(image1).Y);
if ((x - a) < x_old && x_old < (x + a))
{
if ((y - a) < y_old && y_old < (y + a))
{
isClicked = 1;
}
}
}
private void MainPage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isClicked = 0;
}
private void MainPage_MouseMove(object sender, MouseEventArgs e)
{
//http://www.dotnetcurry.com/ShowArticle.aspx?ID=198
int x_now, y_now;
if (isClicked == 1)
{
x_now = (int)(e.GetPosition(image1).X);
y_now = (int)(e.GetPosition(image1).Y);
x += x_now - x_old;
y += y_now - y_old;
x_old = x_now;
y_old = y_now;
}
}
}
}