2009-08-30 63 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