Created
September 10, 2011 01:47
-
-
Save 7shi/1207814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Input; | |
using System.Windows.Shapes; | |
using System.Windows.Media; | |
class Test | |
{ | |
static void move(UIElement sh, double x, double y) | |
{ | |
Canvas.SetLeft(sh, x); | |
Canvas.SetTop(sh, y); | |
} | |
static Canvas canvas; | |
static UIElement elem; | |
static Point click = new Point(); | |
static Point orig = new Point(); | |
static void down(UIElement el, MouseButtonEventArgs e) | |
{ | |
elem = el; | |
orig = new Point(Canvas.GetLeft(el), Canvas.GetTop(el)); | |
click = e.GetPosition(canvas); | |
canvas.CaptureMouse(); | |
} | |
[STAThread] | |
static void Main() | |
{ | |
var w = new Window { Title = "Test", Width = 256, Height = 256 }; | |
w.Show(); | |
canvas = new Canvas(); | |
w.Content = canvas; | |
var r = new Rectangle { Width = 40, Height = 40 }; | |
move(r, 20, 60); | |
r.Stroke = new SolidColorBrush(Colors.Black); | |
r.Fill = new SolidColorBrush(Colors.Yellow); | |
canvas.Children.Add(r); | |
var tb = new TextBlock { Text = "abcdefg", FontSize = 24 }; | |
move(tb, 20, 30); | |
canvas.Children.Add(tb); | |
r.MouseDown += (sender, e) => down(r, e); | |
tb.MouseDown += (sender, e) => down(tb, e); | |
canvas.MouseMove += (sender, e) => | |
{ | |
if (elem != null) | |
{ | |
var p = e.GetPosition(canvas); | |
move(elem, orig.X + p.X - click.X, orig.Y + p.Y - click.Y); | |
} | |
}; | |
canvas.MouseUp += (sender, e) => | |
{ | |
elem = null; | |
canvas.ReleaseMouseCapture(); | |
}; | |
new Application().Run(w); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment