简体   繁体   English

WPF C#问题与获取passwordBox(x,y)的位置

[英]WPF C# Issue with getting position of passwordBox (x,y)

Good afternoon, 下午好,

I need to get location of PasswordBox which is placed somewhere on my form called "LogInWindow.xaml", because I want to simulate psyhical/mouse click on that textBox. 我需要获取PasswordBox的位置,该位置位于我的窗体中名为“ LogInWindow.xaml”的某处,因为我想模拟心理/鼠标单击该textBox。

I allready have function which is accepting two parameters, and which is doing click and those parameters are x & y which should be location of target control, here is the function: 我已经准备好接受两个参数的函数,并执行点击操作,并且这些参数是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);
    }

so what I am missing here is xpos and ypos (coordinates of target control) and here is one of the ways I tried to get positions but its not working unfortunately: 所以我在这里缺少的是xposypos (目标控制的坐标),这是我尝试获取位置但不幸地无法正常工作的方法之一:

Point relativePoint = txtPassword.TransformToAncestor(Application.Current.MainWindow)
                       .Transform(new Point(0, 0));

I got error on txtPassword that says: Error 14 A field initializer cannot reference the non-static field, method, or property 'Main.LogInWindow.txtPassword' 我在txtPassword上遇到错误,提示:错误14字段初始化程序无法引用非静态字段,方法或属性'Main.LogInWindow.txtPassword'

I finally made this working by following @FrancisLord advice. 我终于按照@FrancisLord的建议进行了这项工作。

But now I am facing another issue , I wanted to test this on another computer/monitor, and I copied my .exe file and I saw that this is not working on another machine, on my developer machine I saw that my function LeftMouseClick is working fine because its triggering "button1" that I am looking for, and on another machine looks like its not simulating click, maybe it can't find position of button1 or whatever :// :/// Here is my code: 但是现在我面临另一个问题 ,我想在另一台计算机/显示器上进行测试,然后复制了.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
 }

It would look by the error that you are trying to assign the value of relativePoint through a filed initializer (that is when you set a value to a class level variable in it's declaration), which you cannot do if it calls methods or properties. 会出现以下错误,您尝试通过文件初始化器(即,在其声明中为类级别变量设置值)尝试分配relativePoint的值时,如果调用了方法或属性,则无法执行此操作。 What you should do here is declare that filed at the class level like right now, but assign it's s value in the constructor of your form's 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: sorry if the class name or inheritance don't make sense, I don't know what it's supposed to look like for WPF PS:对不起,如果类名或继承没有意义,我不知道WPF应该是什么样子

Use this line to get position from window 使用此行从窗口获取位置

Point locationFromWindow = txtPassword.TranslatePoint(new Point(0, 0), Application.Current.MainWindow);

add this line to get position from screen 添加此行以从屏幕获取位置

Point locationFromScreen = txtPassword.PointToScreen(locationFromWindow);

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

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