简体   繁体   English

将UI元素位置设置为鼠标位置

[英]Set UI Element Position to Mouse Position

I wrote a little Program that should display a Ellipse at the exact mouse position. 我写了一个小程序,应该在鼠标的确切位置显示一个椭圆。 The Problem is that, the way Iam doing it right now , The Mouse and Ellipse Position are only exact at the center of the Screen. 问题是,我现在做的方式是,“鼠标”和“椭圆位置”仅精确位于屏幕中央。 If I put the mouse further away to the windowborder they drift further and further away. 如果我将鼠标移到窗口边框的更远处,它们会漂移得越来越远。

I use the MouseOver Element to Update the Mouse Position. 我使用MouseOver元素更新鼠标位置。

在此处输入图片说明

Here is my code: 这是我的代码:

         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);

    }

I propose to use a Canvas instead of a grid. 我建议使用画布而不是网格。

With a canvas you can simply set the ellipse position like that: 使用画布,您可以像这样简单地设置椭圆位置:

   Canvas.SetLeft(MousePos_Ellipse, MousePos_Point.X);
   Canvas.SetTop(MousePos_Ellipse, MousePos_Point.Y);

The Grid control will do automatic positioning and sizing of child elements and is therefore not so suitable for your goal. 网格控件会自动定位和调整子元素的大小,因此不太适合您的目标。

Disclaimer; 免责声明; while the answers above will fix your issue, the actual question is not properly resolved. 尽管上述答案可以解决您的问题,但实际问题并未得到正确解决。 The problem you are facing derives from your interpretation of the issue compared to how the computer sees it. 您所面对的问题源于您对问题的理解(与计算机的看法相比)。

The position of your cursor relative to the grid is not the same as the position relative to your screen (ie different resolutions return different values). 光标相对于网格的位置与相对于屏幕的位置不同(即不同的分辨率返回不同的值)。 This is why our x and y values will be further off the further you get off center. 这就是为什么我们的x和y值离您离中心越远的原因。 You could fix this by defining that you want your X and Y position relative to the form , f.ex, like so: 您可以通过定义要相对于表单 f.ex的X和Y位置解决此问题,如下所示:

    var relativePoint = this.PointToClient(Cursor.Position);

The noticable difference here is that here, we Point to the client, and therefore get the Cursor's relative position within the form. 这里值得注意的区别是,这里我们指向客户,因此获得了光标在表单中的相对位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM