2009-08-30 66 views
1

我期待做一个简单的应用程序,它将每隔几毫秒从Skype窗口捕获视频。如何找到外国窗口并跟踪其位置?

我的问题是:

  • 如何自动定位的窗口句柄?也就是说,哪个是最好的策略(按名称或?),以及如何通过编程实现?
  • 我如何随着用户调整大小/移动它来跟踪窗口的位置?

回答

2

找到它,如果Skype是活动窗口,您可以使用GetForegroundWindow。如果不是,使用进程名称是一个不错的选择。你也可以将它们组合起来,看看你是否得到相同的窗口句柄。

以下代码将从窗口句柄中抓取图像并将其作为图像保存到磁盘。如果你经常调用这个代码并修改代码来创建独特的图像,你会得到像你所要求的几个图像。 GetWindowRect是获取窗口坐标的关键函数。当然,你可以用任何你喜欢的格式保存图像。用于保存图像的WPF函数比此处使用的Winform代码更快。

public void GrabActiveWindow() 
{ 
    IntPtr handle = GetForegroundWindow(); // window handle of active application 
    IntPtr handle = WindowsHandleFromProcess("skype.exe"); 

    RECT screenRect = new RECT(); 
    GetWindowRect(handle, out screenRect); 
    long width = screenRect.Right - screenRect.Left; 
    long height = screenRect.Bottom - screenRect.Top; 

    Bitmap bmpScreenshot = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb); 
    Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot); 
    Size size = new Size((int)width, (int)height); 
    gfxScreenshot.CopyFromScreen(screenRect.Left, screenRect.Top, 0, 0, size, CopyPixelOperation.SourceCopy); 
    string path = @"c:\savepath.tiff"; 
    bmpScreenshot.Save(path, ImageFormat.Tiff); 
} 

public IntPtr WindowsHandleFromProcess(string processName) 
{ 
    foreach (var process in Process.GetProcessesByName(processName)) 
    { 
     return process.MainWindowHandle; 
    } 
    return IntPtr.Zero; 
} 

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); 

[DllImport("user32.dll")] 
public static extern IntPtr GetForegroundWindow(); 
+0

这将与硬件覆盖层一起工作吗?我发现了一些与设备上下文类似的代码,但它给了我一个黑屏的视频。 如何将其转换为捕捉覆盖的视频? – georgiosd 2009-08-30 20:10:54

+0

它的工作原理,谢谢! – georgiosd 2009-08-30 20:27:00

+0

我不确定它是否可以在覆盖层上工作..今天早上想到了这个,但显然它确实如此;) – 2009-08-31 07:56:21

1

我从来没有使用它,但可能Skype4COM会帮助。

Skype4COM是 表示的Skype API为对象, 具有属性,命令和事件 和通知的接口。使用Skype4COM中的 任何ActiveX环境(如 Visual Studio或Delphi),并使用 熟悉的脚本语言,如 Visual Basic,PHP或Javascript。

你会

https://developer.skype.com/Docs/Skype4COM

+0

+1 - 我之前使用过Skype4COM,它的功能就像一个魅力。在您的内置应用程序访问Skype之前,它会向用户显示一些确认信息。 – 2009-08-31 02:21:18