【发布时间】:2015-09-17 12:30:00
【问题描述】:
我写了一个小程序,它应该在鼠标的确切位置显示一个椭圆。问题是,我现在这样做的方式,鼠标和椭圆位置只在屏幕的中心。如果我把鼠标放在离窗口边框更远的地方,它们会越来越远。
我使用 MouseOver 元素来更新鼠标位置。
这是我的代码:
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Main_Grid.Children.Clear();
MousePos_Ellipse = new Ellipse();
Point MousePos_Point = new Point();
MousePos_Point = Mouse.GetPosition(Main_Grid);
Main_Grid.Children.Remove(MousePos_Ellipse);
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(55, 255, 255, 0);
MousePos_Ellipse.Fill = mySolidColorBrush;
MousePos_Ellipse.StrokeThickness = 2;
MousePos_Ellipse.Stroke = Brushes.Black;
// Set the width and height of the Ellipse.
MousePos_Ellipse.Width = 15;
MousePos_Ellipse.Height = 15;
// At this Point I do my Positioning
MousePos_Ellipse.Margin = new Thickness(left: MousePos_Point.X - ( Main_Grid.ActualWidth / 2) , top: MousePos_Point.Y - ( Main_Grid.ActualHeight / 2 ), right: 0 , bottom: 0);
//base.AddVisualChild(_circle);
// Add the Ellipse to the Grid
Main_Grid.Children.Add(MousePos_Ellipse);
}
【问题讨论】:
-
使用画布而不是网格
-
画布控件是您要用于显式定位子元素的控件。您将在其他实现特定布局样式的控件中遇到问题。