如何更改另一个程序窗口中按钮的文本

3
我被指定更改一个窗口中按钮的文本。由于该公司是我们付费订阅的公司,因此我没有也无法访问源代码。我该如何在没有源代码的情况下更改按钮文本?我正在尝试使用pInvoke并遇到问题。窗口标题会根据你正在与谁一起工作而更改: "Order Entry Sheet - LASTNAME,FIRSTNAME",因此窗口标题可能无法在win32调用内使用。我知道两个参数都是可选的。我正在使用Spy ++,不确定要使用什么lpClassName。我看到列出的类名是#32770(Dialog)。我尝试了一下并得到了0的返回值。如何从另一个进程更改按钮文本?
根据MSDN的说法,我应该能够通过SetWindowText来实现这个功能。

更改指定窗口标题栏(如果有)的文本。如果指定的窗口是控件,则更改控件的文本。但是,SetWindowText无法更改另一个应用程序中控件的文本。

我不能使用SetWindowText来完成我想要的事情。是否有其他办法可以使用?

将文本插入到另一个应用程序的文本框中。 - John Arlen
1个回答

2
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32", SetLastError = true)]
    public static extern int EnumWindows(CallBack x, int y);

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumChildWindows(IntPtr hwndParent, CallBack lpEnumFunc, IntPtr lParam);

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

    public const uint WM_SETTEXT = 0x000C;

    public delegate bool CallBack(int hwnd, int lParam);

    public static void Main()
    {
        CallBack windowsCallback = new CallBack(IterateWindows);
        EnumWindows(windowsCallback, 0);        
    }

    public static bool IterateChildren(int hwnd, int lParam)
    {
        string newButtonText = "Some text";
        bool continueIteratingChildren = true;
        //Console.WriteLine("Child handle: " + hwnd);

        int length = GetWindowTextLength((IntPtr)hwnd);
        StringBuilder sb = new StringBuilder(length + 1);
        GetWindowText((IntPtr)hwnd, sb, sb.Capacity);
        //Console.WriteLine(sb);

        if (sb.ToString().StartsWith("My Button Text "))
        {
            HandleRef hrefHWndTarget = new HandleRef(null, (IntPtr)hwnd);
            SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, newButtonText);
            continueIteratingChildren = false;
        }
        return continueIteratingChildren;
    }

    public static bool IterateWindows(int hwnd, int lParam)
    {
        bool continueIteratingWindows = true;
        int windowTextLength = GetWindowTextLength((IntPtr)hwnd);
        StringBuilder sb = new StringBuilder(windowTextLength + 1);
        GetWindowText((IntPtr)hwnd, sb, sb.Capacity);

        if (sb.ToString().StartsWith("My Window Caption"))
        {
            //Console.Write("Window handle is ");
            //Console.WriteLine(hwnd);
            //Console.WriteLine(sb);
            //Console.WriteLine(Marshal.GetLastWin32Error());
            var childrenCallback = new CallBack(IterateChildren);
            EnumChildWindows((IntPtr)hwnd, childrenCallback, IntPtr.Zero);
            continueIteratingWindows = false;
        }
        return continueIteratingWindows;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接