用c#打开进程Windows文本框

本文关键字:Windows 文本 进程 | 更新日期: 2023-09-27 18:12:07

我需要找到打开进程或应用程序文本框并更改其值。但我想用c#的方式来做。如果有人知道,你能告诉我吗?或者我必须使用c++以及如何使用?

谢谢你的建议。

用c#打开进程Windows文本框

就像另一个人说的,UIAutomation是前进的方向。http://msdn.microsoft.com/en-us/library/ms753107.aspx

下面的代码将打开Notepad.exe,打开它的文件对话框,然后在文件名字段中键入一些文本。

        Process notepad = Process.Start("notepad");
        Thread.Sleep(5000);
        SendKeys.SendWait("^o"); //ctrl+o to open the File Dialog
        var notepadTextBox = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, 
            new PropertyCondition(AutomationElement.AutomationIdProperty, "1148"));
        object valuePattern = null;
        if (notepadTextBox.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern))
        {
            ((ValuePattern)valuePattern).SetValue("THERE YOU GO"); // this text will be entered in the textbox
        }
        else 
        {
            //ERROR
        }

所以这实际上是发送按键来控制UI的组合(打开文件对话框)&UIAutomation,但是如果你需要的话,你可以像用户一样改变它来驱动菜单。

另外,您可能想知道神奇的字符串"1148"来自哪里-这是记事本中文件名输入字段的"自动化Id"。我使用inspect.exe(包含在Windows SDK中)来查找自动化Id,您将需要该应用程序来查看其自动化Id,如果它有任何。

如果应用程序在使用库和包装器方面超出了您的控制,则可以这样做:

Process[] Procs = Process.GetProcessesByName("NameofProcess");

将给出所讨论的过程。现在,这将是一个棘手的地方,取决于你到底需要做什么。

你最终需要找到字符串存储在内存中的位置,你可以使用内存分析器来做这件事,或者像CheatEngine这样的东西来查找值,不打算进入你使用CheatEngine的目的或你如何使用它,但它只是一种查找内存位置的简单方法。

你可以这样读/写内存位置:

    [DllImport("kernel32.dll")]
    public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
    public static byte[] ReadMem(IntPtr MemAddy, uint bytestoread, Process Proc)
    {
        //
        //Create new Memory buffer and pointer to that buffer
        //
        byte[] buffer = new byte[bytestoread];
        IntPtr bufferptr;
        //
        //Read Process Memory and output to buffer
        //
        ReadProcessMemory(Proc.Handle, MemAddy, buffer, bytestoread, out bufferptr);
        //
        //Return the buffer
        //
        return buffer;
    }
    public static bool WriteMem(IntPtr MemAddy, byte[] buffer, Process Proc)
    {
        int NumWriten;
        WriteProcessMemory(Proc.Handle, MemAddy, buffer, (uint)buffer.Length, out NumWriten);
        if (NumWriten != buffer.Length)
        {
            return false;
        }
        else return true;
    }

这两个函数允许你对任意进程的内存位置进行读写。

如果你想要这个窗口,你可以使用:

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

一样:

    IntPtr HWND = FindWindow(null, "WinName");

它将为您提供所讨论的窗口的句柄。

另一种方法是找到窗口,然后将一些事件传递给它,例如使窗口聚焦,然后以编程方式通过文本框进行制表。然而,如果没有更多的信息,你到底想做什么,我不知道在这里还能说什么。

您正在寻找的工具是UI自动化。它可以让你看到其他程序的控件,并向这些控件发送文本。我曾经这样做过,在过去,我不得不从一个损坏的数据库导出数据,我不得不在一个对话框上点击确定,每次它碰到一个损坏的记录。

这个主题太复杂了,无法在SO答案的空间中深入探讨如何做到这一点,但这里有一个我在CodePlex上找到的教程,介绍了如何做到这一点。

也有第三方包装库,使它更容易做到。我个人最喜欢的是白色。