要通过文本框前面的标签(Label)为不同的文本框填写不同的内容,你可以通过查找标签和文本框的句柄,然后使用 SendMessage 函数向文本框发送消息来设置其文本内容。

以下是一个示例代码,演示如何根据标签的文本内容为不同的文本框填写不同的内容:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

class Program
{
    // 导入 Windows API 函数
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);

    // 常量定义
    private const int WM_SETTEXT = 0x000C;
    private const int WM_GETTEXT = 0x000D;
    private const int WM_GETTEXTLENGTH = 0x000E;

    static void Main(string[] args)
    {
        // 延迟一段时间,以便能够切换到目标窗口
        Thread.Sleep(2000);

        // 获取父窗口的句柄
        IntPtr parentWindowHandle = FindWindow(null, "父窗口标题"); // 替换为父窗口的标题

        if (parentWindowHandle != IntPtr.Zero)
        {
            // 获取标签和文本框的句柄
            IntPtr label1Handle = FindWindowEx(parentWindowHandle, IntPtr.Zero, "Static", "标签1"); // 替换为标签1的文本内容
            IntPtr textBox1Handle = FindWindowEx(parentWindowHandle, IntPtr.Zero, "Edit", null); // 替换为文本框1的类名

            IntPtr label2Handle = FindWindowEx(parentWindowHandle, IntPtr.Zero, "Static", "标签2"); // 替换为标签2的文本内容
            IntPtr textBox2Handle = FindWindowEx(parentWindowHandle, IntPtr.Zero, "Edit", null); // 替换为文本框2的类名

            if (label1Handle != IntPtr.Zero && textBox1Handle != IntPtr.Zero &&
                label2Handle != IntPtr.Zero && textBox2Handle != IntPtr.Zero)
            {
                // 获取标签1的文本内容
                string label1Text = GetControlText(label1Handle);

                // 获取标签2的文本内容
                string label2Text = GetControlText(label2Handle);

                // 填写文本框1的内容
                if (label1Text == "标签1")
                {
                    SetControlText(textBox1Handle, "文本框1的内容");
                }

                // 填写文本框2的内容
                if (label2Text == "标签2")
                {
                    SetControlText(textBox2Handle, "文本框2的内容");
                }
            }
            else
            {
                Console.WriteLine("未找到标签和文本框!");
            }
        }
        else
        {
            Console.WriteLine("未找到父窗口!");
        }
    }

    static string GetControlText(IntPtr controlHandle)
    {
        // 获取文本长度
        int length = SendMessage(controlHandle, WM_GETTEXTLENGTH, IntPtr.Zero, null);

        // 创建缓冲区
        StringBuilder sb = new StringBuilder(length + 1);

        // 获取文本内容
        SendMessage(controlHandle, WM_GETTEXT, (IntPtr)sb.Capacity, sb);

        return sb.ToString();
    }

    static void SetControlText(IntPtr controlHandle, string text)
    {
        SendMessage(controlHandle, WM_SETTEXT, IntPtr.Zero, new StringBuilder(text));
    }
}

在示例代码中,我们首先使用 FindWindow 函数获取父窗口的句柄。然后,通过调用 FindWindowEx 函数,我们获取了标签和文本框的句柄。

接下来,我们使用 GetControlText 方法获取每个标签的文本内容,然后根据标签的文本内容来判断要填写的文本框。最后,我们使用 SetControlText 方法向文本框发送 WM_SETTEXT 消息来设置其文本内容。

请将示例代码中的 "父窗口标题" 替换为你要找到的父窗口的实际标题,"标签1"、"标签2" 替换为你要查找的标签的实际文本内容,"文本框1的内容"、"文本框2的内容" 替换为你要填写的文本框的实际内容。确保你的应用程序具有足够的权限来操作目标窗口、标签和文本框。

注意: 由于该代码使用 Windows API 函数,它可能与特定版本的 Windows 或应用程序的具体实现存在兼容性问题。在使用该代码之前,请务必进行测试并根据实际情况进行调整。

希望以上内容能对你有所帮助!

通过标签为不同文本框填写内容 - C# 代码示例

原文地址: https://www.cveoy.top/t/topic/PWD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录