2016-07-25 9 views
0

非アクティブな外部アプリケーション(たとえば、TeamSpeakやSkype)のスクリーンショットを撮る必要があります。非アクティブな外部アプリケーション

私は検索しましたが、私はあまり見つけられませんでした。私は最小化されたアプリケーションをスクリーンショットすることはできませんが、非アクティブなアプリケーションをスクリーンショットすることができるはずだと思います。

PS:私はちょうどアプリケーションをスクリーンショットしたいので、別のアプリケーションが私が望むものの上にある場合、それは問題になりますか?

私は今何のコードを持っていない、私は私がやりたいことができUSER32のAPIを発見したが、私は..助けを

おかげで名前を忘れてしまいました。

答えて

1

あなたは後にしているAPIはPrintWindowです:

void Example() 
{ 
    IntPtr hwnd = FindWindow(null, "Example.txt - Notepad2"); 
    CaptureWindow(hwnd); 
} 

[DllImport("User32.dll", SetLastError = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); 

[DllImport("user32.dll")] 
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect); 

[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

public void CaptureWindow(IntPtr handle) 
{ 
    // Get the size of the window to capture 
    Rectangle rect = new Rectangle(); 
    GetWindowRect(handle, ref rect); 

    // GetWindowRect returns Top/Left and Bottom/Right, so fix it 
    rect.Width = rect.Width - rect.X; 
    rect.Height = rect.Height - rect.Y; 

    // Create a bitmap to draw the capture into 
    using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) 
    { 
     // Use PrintWindow to draw the window into our bitmap 
     using (Graphics g = Graphics.FromImage(bitmap)) 
     { 
      IntPtr hdc = g.GetHdc(); 
      if (!PrintWindow(handle, hdc, 0)) 
      { 
       int error = Marshal.GetLastWin32Error(); 
       var exception = new System.ComponentModel.Win32Exception(error); 
       Debug.WriteLine("ERROR: " + error + ": " + exception.Message); 
       // TODO: Throw the exception? 
      } 
      g.ReleaseHdc(hdc); 
     } 

     // Save it as a .png just to demo this 
     bitmap.Save("Example.png"); 
    } 
} 
+0

PrintWindowはfalseを返すので、それはうまくいきませんか? – Haytam

+0

ええ、それを供給しているHWNDが無効であるか、ターゲットウィンドウがPrintWindowのサポートを無効にしています。 'Marshal.GetLastWin32Error'には失敗の詳細が含まれているかもしれません。 –

+0

私はGetWindowRectが完全に機能するので、私が得るHWNDが有効だと思います。 Marshal.GetLastWin32Errorはどのように機能しますか? – Haytam

1

GetWindowRectとのuser32 APIを組み合わせることで、この機能を実装する必要があります。 PrintWindowは、特定のアプリケーションがその上にある別のウィンドウによって隠されていても、そのアプリケーションの内容を適切に取得します。

これは、DirectXウィンドウの内容をキャプチャするためには機能しない可能性があることに注意してください。

+0

おかげで、これら2つのAPIでの作業それを得ました。 – Haytam

関連する問題