如何以编程方式捕获子窗口

问题描述

创建应用程序以自动执行 UI 操作(记录和回放)。在这种情况下,如果它的单个窗口/表单应用程序,我可以成功捕获应用程序窗口。如果父窗口之上有子窗口,则总是以父窗口的大小捕获子窗口。

我尝试了本机方法(来自 User32.dll),也尝试过以编程方式调用“Alt+PrtScr”键。但是没用。

enter image description here

enter image description here

输出(捕获的图像):

enter image description here

代码片段:

Process[] p = Process.GetProcessesByName(fileName);
IntPtr handle = p[0].MainWindowHandle; **//It always gives parent window handle; Can't get child window handle.**
User32.SetForegroundWindow(handle);
BitmapSource bitMapSource = TakeScreenSnapshot(true);
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(bitMapSource));
    enc.Save(outStream);
    bitmap = new System.Drawing.Bitmap(outStream);
    bitmap.Save(imagePath,ImageFormat.Png);
}

public static BitmapSource TakeScreenSnapshot(bool activeWindowOnly)
{
    Clipboard.Clear();
    // PrtSc = Print Screen Key
    string keys = "{PrtSc}";

    if (activeWindowOnly)
        keys = "%" + keys; // % = Alt

    System.Windows.Forms.SendKeys.SendWait(keys);
    while (!Clipboard.ContainsImage())
    {
        Thread.Sleep(500);
    }
    return Clipboard.Getimage();
}

分享有关如何获取子窗口句柄或捕获上述子/当前活动窗口的任何建议?

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)