【问题标题】:How to change the button text of another programs window如何更改另一个程序窗口的按钮文本
【发布时间】:2012-07-31 20:58:19
【问题描述】:

我的任务是更改窗口中按钮的文本。我没有也不能访问源代码,因为它由我们付费订阅的公司所有。

如何在没有源代码的情况下更改按钮文本?我正在尝试使用 pInvoke 并遇到问题。窗口标题会根据您与谁一起工作而变化:

“订单输入表 - LASTNAME, FIRSTNAME”

所以窗口标题在 win32 调用中可能对我不可用

FindWindow(string lpClassName, string lpWindowName);

我知道这两个参数都是可选的。我正在使用 Spy++,但我不确定 lpClassName 使用什么。我看到的类名是#32770 (Dialog)。我试了一下,返回0。

IntPtr windowHandle = FindWindow("#32770 (Dialog)", null);

如何从另一个进程更改按钮文本?

更新

根据 MSDN,我应该可以通过 SetWindowText 实现这一目标。

更改指定窗口标题栏的文本(如果有的话)。 如果指定的窗口是控件,则控件的文本为 改变了。但是,SetWindowText 不能更改控件的文本 另一个应用程序。

我不能使用 SetWindowText 来做我想做的事。可以用别的吗?

【问题讨论】:

标签: c# winforms


【解决方案1】:
    [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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多