【问题标题】:Position of curson in clickevent点击事件中光标的位置
【发布时间】:2019-01-17 23:08:51
【问题描述】:

我想获取执行点击事件的 Control 的位置

为此我尝试了以下代码

Xpos = Cursor.Position.X;
Ypos = Cursor.Position.Y;

但这给出了光标的当前位置。然后我尝试了以下代码

MouseEventArgs m = (MouseEventArgs)e;
Xpos=m.X;
Ypos=m.Y;

但是这个位置不是相对于整个屏幕的。

如何获得执行点击事件的控制位置?

编辑

由于为重复提供的链接,,,,它提供了执行点击动作的点的位置,,,,它不提供执行点击的控件的位置。

【问题讨论】:

标签: c# winforms


【解决方案1】:

如果你的项目在WindowsFormApplicaiton 那么

如果您的控件是button,就像屏幕截图中一样

然后你也可以访问它的位置到它的主屏幕并形成屏幕

下面是代码

private void button1_Click(object sender, EventArgs e)
{
    //This code gives you the location of button1 wrt your primary working screen
    Point location = this.PointToScreen(button1.Location);
    int x1 = location.X;
    int y1 = location.Y;

    MessageBox.Show($"X: {x1}, Y: {y1}");


    //This code gives you the location of button1 wrt your forms upper-left corner        
    Point relativeLoc = new Point(location.X - this.Location.X, location.Y - this.Location.Y);
    int x2 = relativeLoc.X;
    int y2 = relativeLoc.Y;

    MessageBox.Show($"X: {x2}, Y: {y2}");


    //This code gives you the location of button1 wrt your forms client area
    Point relativeLoc1 = new Point(button1.Location.X, button1.Location.Y);
    int x3 = relativeLoc1.X;
    int y3 = relativeLoc1.Y;

    MessageBox.Show($"X: {x3}, Y: {y3}");
}

在上面的代码中我使用了this,您可以根据需要使用任何forms 对象

编辑:

如果您不知道您的控件将被点击的位置,那么您必须为表单中的所有控件注册一个事件,例如

在下面的代码中,我使用了MouseHover 事件,但您可以根据需要使用任何事件

首先为Form1_Load 中的所有控件注册MouseHover 事件

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
        c.MouseHover += myMouseHoverEvent;
}

那么你的自定义MouseHover事件就是

private void myMouseHoverEvent(object sender, EventArgs e)
{
    Control control = sender as Control;

    int x = control.Location.X;
    int y = control.Location.Y;

    MessageBox.Show($"X: {x}, Y: {y}");
}

试一次可能对你有帮助

【讨论】:

  • @vk2s,请查看答案可能对您有帮助:)
  • 这里我们知道button1被点击了,,,它可以在表单的任何部分被点击...button1.Location
  • 表示您需要知道鼠标悬停在表单上的任何控件的位置吗?
  • 我想知道执行点击的控件的位置,,,,以及执行点击的控件我不知道
  • vk2s,请查看答案中的编辑部分可能对您有帮助:)
【解决方案2】:

你可以试试这个吗?它应该返回您所期望的(您单击的控件的左上点)。

XAML:

<TextBlock Text="Sample" Width="100" Height="50" 
           MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>

XAML.cs

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var control = sender as FrameworkElement;
    var positionToScreen = control.PointToScreen(new Point(0, 0));
}

【讨论】:

  • 它们是表单中的一个按钮,用于关闭表单它在 var positionToScreen 行中抛出 execption "System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Button'.'" in line var positionToScreen
  • @vk2s 您是否在单击按钮时关闭窗口?在此之前,是WindowsForms 还是WPF 应用程序?
  • WindowsForms 和按钮点击关闭
  • 尝试在窗口关闭之前访问位置。如果您可以分享您的 C# 代码部分,将更容易获得正确答案。
【解决方案3】:

如果您让此事件处理程序处理您感兴趣的所有控件的 MouseClick 事件,此代码应该可以解决您的问题。 如果要在单击的控件内包含位置。然后添加鼠标事件args中包含的位置(代码中Se注释)。

private void control_MouseClick(object sender, MouseEventArgs e)
{
        Control control = sender as Control;
        int posX = control.Location.X;
        int posY = control.Location.Y;
        //Add e.X respectivly e.Y if you want to add the mouse position within the control that generated the event.

        while (control.Parent != null)
        {
            control = control.Parent;
            posX += control.Location.X;
            posY += control.Location.Y;
        }

        ((Label)sender).Text = "X: " + posX + " Y: " + posY;
}

【讨论】:

  • 只有代码 sn-ps 可以回答这个问题。但是在 SO 中总是欢迎提供解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多