[Turn] C # capture dialog text EnumChildWindows

How to find the error on the desktop window, regardless of the parent window or child window, and get it wrong message? 

Mainly using API functions: 

Copy  save
[DllImport("user32.dll")]
public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,
    string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);
[DllImport("user32.dll")]
public static extern int GetLastActivePopup(int hWnd);
[DllImport("user32.dll")]
public static extern int AnyPopup();
[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);


The key is understanding the nature of the windows operating system windows using Spy ++ tool to find the window you can find, in fact, for a given dialog window, any controls, such as icons, text, OK, Cancel buttons are all its sub-window, or window essence, the only difference, when the top-level parent window to find, use FindWindow function, but with FindWindowEx when looking for child windows. 

Also useful is EnumWindows, you can traverse all of the top-level parent window, and EnumChildWindows is traversing its child windows. After testing, EnumThreadWindows callback function can not call, do not know what the reason, experts advise hope. 

So the idea to solve the problem is to use EnumWindows through all the top-level parent window, use EnumChildWindows through it all the controls for each top-level parent window, each control is actually a window, get the handle of the control, you can call to get GetWindowText the text information. 

Specific implementation, first need to define more callbacks proxy API functions: 

Copy  save
/// <summary>
/// 回调函数代理
/// </summary>
public delegate bool CallBack(int hwnd, int lParam);


Then, for each instance of the function must be defined proxy API function: 

Copy  save
/// <summary>
/// 进程回调处理函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static bool ThreadWindowProcess(int hwnd, int lParam)
{
    EnumChildWindows(hwnd, callBackEnumChildWindows, 0);
    return true;
}
/// <summary>
/// 窗口回调处理函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static bool WindowProcess(int hwnd, int lParam)
{
    EnumChildWindows(hwnd, callBackEnumChildWindows, 0);
    return true;
}
/// <summary>
/// 子窗口回调处理函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static bool ChildWindowProcess(int hwnd, int lParam)
{
    StringBuilder title = new StringBuilder(200);
    int len;
    len = GetWindowText(hwnd, title, 200);
    if (len > 0)
    {
        if (title.ToString().IndexOf(GlobalManager.ErrorMessage) != -1)
        {
            FindError = true;
        }
    }
    return true;
}


Finally, define the callback proxy instance 

Copy  save
/// <summary>
/// 进程窗口回调函数代理
/// </summary>
public static CallBack callBackEnumThreadWindows = new CallBack(ThreadWindowProcess);
/// <summary>
/// 窗口回调函数代理
/// </summary>
public static CallBack callBackEnumWindows = new CallBack(WindowProcess);
/// <summary>
/// 子窗口回调函数代理
/// </summary>
public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);


Examples of use: 

Copy  save
/// <summary>
/// 客户端是否弹出对话框
/// </summary>
/// <returns></returns>
public bool IsClientPopupWindows()
{
    bool FindError = false;
    EnumWindows(callBackEnumWindows, 0);
    return FindError;
}

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/08/08/2628836.html

Guess you like

Origin blog.csdn.net/weixin_34072637/article/details/93495149