0

我查看了几个尝试在 stackoverflow 上列出的示例,但我没有找到任何列出如何为 .NET 编码执行此操作的内容。我还需要它来检测弹出窗口,一旦找到弹出窗口,然后拉窗口的标题和弹出窗口内的文本。

我尝试使用帖子: how-can-i-change-text-on-a-win32-window

但我没有运气试图找到如何“获取文本”只是如何查看弹出窗口是否存在。我试图从中获取文本的窗口是标准的 Windows 错误窗口 (#32770),也就是 WIN32 关键弹出窗口(带有红色 X 的窗口)。我知道错误窗口的标题总是“TLP2011”

我只需要能够阅读标题、文本,然后单击错误按钮上的“确定”即可。

原因?有一个我运行 IT 的软件,为了减少呼入电话成本,我想编写一个程序,在错误弹出时检测错误,然后一旦检测到错误,关闭错误窗口并告诉用户错误将被自动修复。(我的新程序必须能够从错误窗口中读取文本,以确定如何自动修复错误,这样该错误就不会再次出现)

----------已编辑以显示最新发现------------

感谢 GX 发布以下链接如何在我的 C# 应用程序中获得类似于 Spy++ 的功能?

现在我已经确定我需要以下内容:

Dim pProcess() As Process = Process.GetProcessesByName("TLP2011")

    For Each p As Process In pProcess
        For Each threadInfo As ProcessThread In p.Threads

            ' uncomment to dump thread handles
            'Console.WriteLine("\tthread {0:x}", threadInfo.Id);
            Dim windows As IntPtr() = GetWindowHandlesForThread(threadInfo.Id)
            If windows IsNot Nothing AndAlso windows.Length > 0 Then
                For Each hWnd As IntPtr In windows
                    Console.WriteLine(vbTab & "window {0:x} text:{1} caption:{2}", hWnd.ToInt32(), GetText(hWnd), GetEditText(hWnd))
                Next
            End If
        Next
    Next

现在这列出了实际程序本身的所有内容。每一段文字和一切。任何人都知道只挑出弹出框的技巧吗?

以下是显示内容的示例

window 3d21aa text:Select Package caption:
window 551176 text: caption:
window e8147c text:Do Not Auto Show  caption:Do Not Auto Show 
window 8e21c6 text:&Select All caption:&Select All
window 2300e10 text:&Help caption:&Help
window 762164 text:Cancel caption:Cancel
window 330f0e text:OK caption:OK
window e8147c text:Do Not Auto Show  caption:Do Not Auto Show 
window 8e21c6 text:&Select All caption:&Select All
window 2300e10 text:&Help caption:&Help
window 762164 text:Cancel caption:Cancel
window 330f0e text:OK caption:OK
window 10f20a6 text: caption:
window 5f122ce text: caption:
window 1824e2 text: caption:
window 1824e2 text: caption:
window 5f122ce text: caption:
window 1824e2 text: caption:
window 1824e2 text: caption:
window 6e1be4 text: caption:
window 342162 text: caption:
window 1c1b92 text: caption:
window 65105a text: caption:Admin
window 291f72 text: caption:
window 375104e text: caption:
window 375104e text: caption:
window 1c1b92 text: caption:
window 65105a text: caption:Admin
window 291f72 text: caption:
window 375104e text: caption:
window 375104e text: caption:
window 242308 text:Form Filter caption:
window 9ee294e text: caption:
window 8ff0f54 text: caption:All Status
window 468273c text: caption:
window 1d110c text: caption:
window 1d110c text: caption:
window 8ff0f54 text: caption:All Status
window 468273c text: caption:
window 1d110c text: caption:
window 1d110c text: caption:
window b90bea text:cbEngine caption:
window 5b17b0 text: caption:
window ee24c6 text: caption:
window ee24c6 text: caption:
window fc1ae0 text: caption:
window 141cfc text:TLP2011 caption:
window d51928 text:OK caption:OK
window 2c1223c text: caption:
window 100f08 text:Access violation at address 6CABC667 in module 'tlpcore.dll'. Write of address F8FFFF86. caption:Access violation at address 6CABC667 in module 'tlpcore.dll'. Write of address F8FFFF86.
window b2a214e text: caption:

有谁知道更好的方法来尝试找到错误框?我不认为搜索“TLP2011”然后使用接下来的 3 行来尝试确定 msgbox 文本是一种非常“可靠”的方法。有谁知道如何将它单挑出来?

----增加了更多的想法----

每隔 10 秒对所有不同的线程进行一次检查似乎会给系统带来巨大的消耗。这真的是最合乎逻辑的方法吗?

4

1 回答 1

0

加埃塔诺的回答是正确的

static void Main(string[] args)
{
    foreach (Process procesInfo in Process.GetProcesses())
    {
        Console.WriteLine("process {0} {1:x}", procesInfo.ProcessName, procesInfo.Id);
        foreach (ProcessThread threadInfo in procesInfo.Threads)
        {
            // uncomment to dump thread handles
            //Console.WriteLine("\tthread {0:x}", threadInfo.Id);
            IntPtr[] windows = GetWindowHandlesForThread(threadInfo.Id);
            if (windows != null && windows.Length > 0)
                foreach (IntPtr hWnd in windows)
                    Console.WriteLine("\twindow {0:x} text:{1} caption:{2}",
                        hWnd.ToInt32(), GetText(hWnd), GetEditText(hWnd));
        }
    }
    Console.ReadLine();
}

private static IntPtr[] GetWindowHandlesForThread(int threadHandle)
{
    _results.Clear();
    EnumWindows(WindowEnum, threadHandle);
    return _results.ToArray();
}

// enum windows

private delegate int EnumWindowsProc(IntPtr hwnd, int lParam);

[DllImport("user32.Dll")]
private static extern int EnumWindows(EnumWindowsProc x, int y);
[DllImport("user32")]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowsProc callback, int lParam);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

private static List<IntPtr> _results = new List<IntPtr>();

private static int WindowEnum(IntPtr hWnd, int lParam)
{
    int processID = 0;
    int threadID = GetWindowThreadProcessId(hWnd, out processID);
    if (threadID == lParam)
    {
        _results.Add(hWnd);
        EnumChildWindows(hWnd, WindowEnum, threadID);
    }
    return 1;
}

// get window text

[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);

private static string GetText(IntPtr hWnd)
{
    int length = GetWindowTextLength(hWnd);
    StringBuilder sb = new StringBuilder(length + 1);
    GetWindowText(hWnd, sb, sb.Capacity);
    return sb.ToString();
}

// get richedit text 

public const int GWL_ID = -12;
public const int WM_GETTEXT = 0x000D;

[DllImport("User32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int index);
[DllImport("User32.dll")]
public static extern IntPtr SendDlgItemMessage(IntPtr hWnd, int IDDlgItem, int uMsg, int nMaxCount, StringBuilder lpString);
[DllImport("User32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);

private static StringBuilder GetEditText(IntPtr hWnd)
{
    Int32 dwID = GetWindowLong(hWnd, GWL_ID);
    IntPtr hWndParent = GetParent(hWnd);
    StringBuilder title = new StringBuilder(128);
    SendDlgItemMessage(hWndParent, dwID, WM_GETTEXT, 128, title);
    return title;
}
于 2014-12-12T04:44:24.083 回答