8,734
社区成员
发帖
与我相关
我的任务
分享
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
#region API方式取鼠标坐标
[StructLayout(LayoutKind.Sequential)]
public struct MyPoint
{
public int X;
public int Y;
public MyPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out MyPoint pt);
#endregion
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
MyPoint myPoint = new MyPoint();
GetCursorPos(out myPoint);
Point userPoint = this.PointFromScreen(new Point(myPoint.X, myPoint.Y));
PointCollection points = new PointCollection();
points.Add(new Point(0, userPoint.Y));
points.Add(new Point(MyGd.ActualWidth, userPoint.Y));
points.Add(new Point(userPoint.X, userPoint.Y));
points.Add(new Point(userPoint.X, 0));
points.Add(new Point(userPoint.X, MyGd.ActualHeight));
Pl1.Points = points;
Pl1.StrokeThickness = 1;
Pl1.Stroke = new SolidColorBrush(Colors.Black);
}
}
<Window x:Class="简单橡皮筋.MainWindow"
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"
xmlns:local="clr-namespace:简单橡皮筋"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="MyGd" MouseMove="Grid_MouseMove">
<Polyline x:Name="Pl1"/>
<TextBlock />
</Grid>
</Window>