WPF C# 获取 passwordBox (x,y) 位置的问题
WPF C# Issue with getting position of passwordBox (x,y)
下午好,
我需要获取 PasswordBox 的位置,它位于我名为 "LogInWindow.xaml" 的表单中的某处,因为我想模拟 psyhical/mouse 单击该文本框。
我已经有了接受两个参数的函数,它正在点击,这些参数是 x 和 y,它应该是目标控件的位置,这里是函数:
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
所以我在这里缺少的是 xpos
和 ypos
(目标控制的坐标),这是我试图获得位置但不幸的是它不起作用的方法之一:
Point relativePoint = txtPassword.TransformToAncestor(Application.Current.MainWindow)
.Transform(new Point(0, 0));
我在 txtPassword 上收到错误提示:
错误 14 字段初始值设定项无法引用非静态字段、方法或 属性 'Main.LogInWindow.txtPassword'
我终于按照@FrancisLord 的建议完成了这项工作。
但现在我面临另一个问题,我想在另一个computer/monitor上测试这个,我复制了我的.exe文件,我发现这不是在另一台机器上工作,在我的开发机器上我看到我的函数 LeftMouseClick
工作正常,因为它触发了我正在寻找的 "button1",而在另一台机器上看起来它没有模拟点击,也许它可以找不到 button1 或其他任何位置 :// :///
这是我的代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
relativePoint = this.button1.TransformToAncestor(this)
.Transform(new Point(0, 0));
LeftMouseClick((int)relativePoint.X, (int)relativePoint.Y);
//restOFcode
}
从错误中可以看出,您正试图通过一个已提交的初始值设定项分配 relativePoint
的值(也就是说,当您在其声明中将一个值设置为 class 级别变量时) ,如果它调用方法或属性,则不能执行此操作。您在这里应该做的是像现在一样在 class 级别声明该文件,但在表单的 class :
的构造函数中为其分配值
public class MyForm : Window
{
Point relativePoint;
public MyForm()
{
//other code already in the constructor
relativePoint = txtPassword.TransformToAncestor(Application.Current.MainWindow)
.Transform(new Point(0, 0));
}
}
PS:抱歉,如果 class 名称或继承没有意义,我不知道 WPF
应该是什么样子
使用此行从 window
获取位置
Point locationFromWindow = txtPassword.TranslatePoint(new Point(0, 0), Application.Current.MainWindow);
添加此行以从屏幕获取位置
Point locationFromScreen = txtPassword.PointToScreen(locationFromWindow);
下午好,
我需要获取 PasswordBox 的位置,它位于我名为 "LogInWindow.xaml" 的表单中的某处,因为我想模拟 psyhical/mouse 单击该文本框。
我已经有了接受两个参数的函数,它正在点击,这些参数是 x 和 y,它应该是目标控件的位置,这里是函数:
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
所以我在这里缺少的是 xpos
和 ypos
(目标控制的坐标),这是我试图获得位置但不幸的是它不起作用的方法之一:
Point relativePoint = txtPassword.TransformToAncestor(Application.Current.MainWindow)
.Transform(new Point(0, 0));
我在 txtPassword 上收到错误提示: 错误 14 字段初始值设定项无法引用非静态字段、方法或 属性 'Main.LogInWindow.txtPassword'
我终于按照@FrancisLord 的建议完成了这项工作。
但现在我面临另一个问题,我想在另一个computer/monitor上测试这个,我复制了我的.exe文件,我发现这不是在另一台机器上工作,在我的开发机器上我看到我的函数 LeftMouseClick
工作正常,因为它触发了我正在寻找的 "button1",而在另一台机器上看起来它没有模拟点击,也许它可以找不到 button1 或其他任何位置 :// :///
这是我的代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
relativePoint = this.button1.TransformToAncestor(this)
.Transform(new Point(0, 0));
LeftMouseClick((int)relativePoint.X, (int)relativePoint.Y);
//restOFcode
}
从错误中可以看出,您正试图通过一个已提交的初始值设定项分配 relativePoint
的值(也就是说,当您在其声明中将一个值设置为 class 级别变量时) ,如果它调用方法或属性,则不能执行此操作。您在这里应该做的是像现在一样在 class 级别声明该文件,但在表单的 class :
public class MyForm : Window
{
Point relativePoint;
public MyForm()
{
//other code already in the constructor
relativePoint = txtPassword.TransformToAncestor(Application.Current.MainWindow)
.Transform(new Point(0, 0));
}
}
PS:抱歉,如果 class 名称或继承没有意义,我不知道 WPF
应该是什么样子使用此行从 window
获取位置Point locationFromWindow = txtPassword.TranslatePoint(new Point(0, 0), Application.Current.MainWindow);
添加此行以从屏幕获取位置
Point locationFromScreen = txtPassword.PointToScreen(locationFromWindow);