SilkTest 技術情報(FAQ)


SilkTestでスクリプトから画面コピーを取得する


SilkTestスクリプトによる標準的な方法

SilkTestの.NET スクリプトでは、.CaptureBitmapメソッドで、画面コピーをファイルに保存できます。 たとえば、

 _desktop.Window("電卓").CaptureBitmap("C:\電卓.bmp")

などです。ほとんどの場合、この方法で十分な情報が取得できます。

画像をファイルに保存する前に加工したい、あるいは何らかの理由によりこの方法が動作しない等の場合、Windows の機能を使用して画面コピーを取得することもできます。


SilkTest Workbench から、Visual Basic.NET を使って画面コピーを取得

本ページ末尾、画面コピースクリプト(Visual Basic.NET)を、そのままテストスクリプトに張り付けます。最初のImportsが、スクリプトの先頭に来るように注意してください。 アプリケーションから、画面コピーを取得するには、ScreenCaptureのオブジェクトを作成しGetBitmapメソッドを呼び出します。メソッドの第一引数には、SilkTestのオブジェクトを指定してください。第二引数は、クライアント領域のみの画面コピーを取るかどうかのフラグです。

例1:画面コピーを取得し、変数に格納。その後、変数をビットマップとして保存。

Dim scrCap As ScreenCaputre = New ScreenCaputre()
:
Dim bmpTest as Bitmap 
 bmpTest = scrCap.GetBitmap(.PushButton("pushButton9"),False)
 bmpTest.Save("C:\テスト.bmp")

例2:デスクトップ全体の画面コピーを取得し、ファイルに保存

Dim scrCap As ScreenCaputre = New ScreenCaputre()
scrCap.GetBitmap("DESKTOP").Save("C:\デスクトップ.bmp")

Silk4NET から、C#を使用して画面コピーを取得

Silk4NETから使用する場合もほぼ同様ですが参照設定の追加が必要になります。プロジェクトの参照設定から、System.Drawingを追加してください。

本ページ末尾の画面コピースクリプト(C#)を、そのままテストスクリプトに張り付けます。Using 文の位置など、C#の文法に準拠するよう、注意してください。

使用例

ScreenCapture scrCap = new ScreenCapture();
Window 電卓 = _desktop.Window("@caption='電卓'");
scrCap.GetBitmap(電卓, true).Save("C:\\dtk.bmp");


画面コピースクリプト (Visual Basic)

Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing

Public Class ScreenCaputre
	Private Const SRCCOPY = 13369376
	<StructLayout(LayoutKind.Sequential)> _
	Private Structure RECT
		<FieldOffset(0)> Public left As Integer
   		<FieldOffset(4)> Public top As Integer
   		<FieldOffset(8)> Public right As Integer
   		<FieldOffset(12)> Public bottom As Integer
	End Structure
 
	Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Integer,<MarshalAs(UnmanagedType.Struct)> ByRef   lpRect As RECT) As Integer
	Private Declare Function GetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As Integer,<MarshalAs(UnmanagedType.Struct)> ByRef   lpRect As RECT) As Integer
	Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
	Private Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
	Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
	Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer
	Private Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
 	
	''' <summary>
	''' 指定されたウインドウハンドルの画面コピーを取得し、そのビットマップを返します。
	''' たとえば、SilkHelper.ScreenCapture(_desktop.Window("電卓").NativeHandle,True).Save("C:\b.bmp") などとすることで、
	''' 電卓ウインドウのクライアント領域を、C:\b.bmpなどに保存できます。
	''' 例:
	''' 
	''' </summary>
	''' <param name="objGUI">SilkTestのBaseGUIObjectを指定します。</param>
	''' <param name="isCaptureClient">クライアント領域のみを保存するか、ウインドウ全体を保存するかを指定します。</param>
	''' <returns>保存されたビットマップオブジェクトです。.Save メソッドなどを使用するとファイルに保存できます。</returns>	
	Public Function GetBitmap(hWnd As IntPtr, isCaptureClient As Boolean) As Bitmap
		Dim winRect As RECT = New RECT()
		Dim winDC  As IntPtr
		Dim Ret As Integer
		If isCaptureClient Then
        	Dim clientRect As RECT = New RECT()
			winDC = GetDC(hWnd)
			Ret = GetWindowRect(hWnd,  winRect)
            Ret = GetClientRect(hWnd,  clientRect)
            winRect.right = winRect.left + clientRect.right
            winRect.bottom = winRect.top + clientRect.bottom
		Else
			winDC = GetWindowDC(hWnd)
			GetWindowRect(hWnd,  winRect)
		End If
		Dim bmp As Bitmap = New Bitmap(winRect.right - winRect.left,winRect.bottom - winRect.top)
        Dim g As Graphics = Graphics.FromImage(bmp)
		
		Dim hDC As IntPtr = g.GetHdc()
 
		BitBlt(hDC, 0, 0, bmp.Width, bmp.Height,winDC, 0, 0, SRCCOPY)
 
		g.ReleaseHdc(hDC)
		g.Dispose()
		Return bmp
	End Function
 
	''' <summary>
	''' 指定されたオブジェクトの画面コピーを取得し、そのビットマップを返します。
	''' たとえば、SilkHelper.ScreenCapture(_desktop.Window("電卓"),True).Save("C:\b.bmp") などとすることで、
	''' 電卓ウインドウのクライアント領域を、C:\b.bmpなどに保存できます。
	''' 例:
	''' 
	''' </summary>
	''' <param name="objGUI">SilkTestのBaseGUIObjectを指定します。</param>
	''' <param name="isCaptureClient">クライアント領域のみを保存するか、ウインドウ全体を保存するかを指定します。</param>
	''' <returns>保存されたビットマップオブジェクトです。.Save メソッドなどを使用するとファイルに保存できます。</returns>
	Public Function GetBitmap(objGUI As SilkTest.Ntf.BaseGuiTestObject , isCaptureClient As Boolean) As Bitmap
		Dim hWnd As IntPtr
		hWnd = objGUI.NativeHandle
		Return GetBitmap(hWnd,isCaptureClient)
	End Function
	''' <summary>
	''' 特殊なウインドウの画面コピーを取得し、そのビットマップを返します。
	''' 第一引数で、ウインドウの種類を指定します。現在使用できるのは、"DESKTOP"のみです。
	''' 例:
	''' 
	''' </summary>
	''' <param name="objGUI">文字列です。"DESKTOP"だけが指定できます。</param>
	''' <returns>保存されたビットマップオブジェクトです。.Save メソッドなどを使用するとファイルに保存できます。</returns>
	Public Function GetBitmap(strSRC As String) As Bitmap
		If (strSRC = "DESKTOP") Then
			Return GetBitmap(GetDesktopWindow(),False)
		End If
		Dim bmpEmpty As Bitmap = New Bitmap(0,0)
		Return bmpEmpty
	End Function	
End Class


画面コピースクリプトC#

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SilkTest.Ntf;
using System.Runtime.InteropServices;
using System.Drawing;
 :
    class ScreenCapture {
        [StructLayout(LayoutKind.Sequential)]
        private struct RECT {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        private const int SRCCOPY = 13369376;
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref  RECT lpRect);
        [DllImport("user32.dll")]
        private static extern int GetClientRect(IntPtr hwnd, ref  RECT lpRect);
        [DllImport("gdi32.dll")]
        private static extern int BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern bool IsWindow(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern int GetDesktopWindow();
        /// <summary>
	    ///  指定されたウインドウハンドルの画面コピーを取得し、そのビットマップを返します。
	    /// たとえば、SilkHelper.ScreenCapture(_desktop.Window("電卓").NativeHandle,True).Save("C:\b.bmp") などとすることで、
	    /// 電卓ウインドウのクライアント領域を、C:\b.bmpなどに保存できます。
	    ///  例:
	    ///  
	    ///  </summary>
	    ///  <param name="objGUI">SilkTestのBaseGUIObjectを指定します。</param>
	    ///  <param name="isCaptureClient">クライアント領域のみを保存するか、ウインドウ全体を保存するかを指定します。</param>
        ///  <returns>保存されたビットマップオブジェクトです。.Save メソッドなどを使用するとファイルに保存できます。</returns>	
        public Bitmap GetBitmap(IntPtr hWnd, bool isCaptureClient)
        {
            IntPtr dC;
            RECT lpRect = new RECT();
            if (isCaptureClient) {
                RECT rect2 = new RECT();
                dC = (IntPtr)GetDC(hWnd);
                int windowRect = GetWindowRect(hWnd, ref lpRect);
                windowRect = GetClientRect(hWnd, ref rect2);
                lpRect.right = lpRect.left + rect2.right;
                lpRect.bottom = lpRect.top + rect2.bottom;
            } else {
                dC = (IntPtr)GetWindowDC(hWnd);
                GetWindowRect(hWnd, ref lpRect);
            }
            Bitmap image = new Bitmap(lpRect.right - lpRect.left, lpRect.bottom - lpRect.top);
            Graphics graphics = Graphics.FromImage(image);
            IntPtr hdc = graphics.GetHdc();
            BitBlt(hdc, 0, 0, image.Width, image.Height, dC, 0, 0, 0xcc0020);
            graphics.ReleaseHdc(hdc);
            graphics.Dispose();
            return image;
        }
        /// <summary>
        /// 特殊なウインドウの画面コピーを取得し、そのビットマップを返します。
        /// 第一引数で、ウインドウの種類を指定します。現在使用できるのは、"DESKTOP"のみです。
        /// 例:
        /// 
        /// </summary>
        /// <param name="objGUI">文字列です。"DESKTOP"だけが指定できます。</param>
        /// <returns>保存されたビットマップオブジェクトです。.Save メソッドなどを使用するとファイルに保存できます。</returns>
        public Bitmap GetBitmap(string strSRC)
        {
            if (strSRC == "DESKTOP") {
                return this.GetBitmap((IntPtr)GetDesktopWindow(), false);
            }
            return new Bitmap(0, 0);
        }
        /// <summary>
        /// 指定されたオブジェクトの画面コピーを取得し、そのビットマップを返します。
        /// たとえば、SilkHelper.ScreenCapture(_desktop.Window("電卓"),True).Save("C:\b.bmp") などとすることで、
        /// 電卓ウインドウのクライアント領域を、C:\b.bmpなどに保存できます。
        /// 例:
        /// 
        /// </summary>
        /// <param name="objGUI">SilkTestのBaseGUIObjectを指定します。</param>
        /// <param name="isCaptureClient">クライアント領域のみを保存するか、ウインドウ全体を保存するかを指定します。</param>
        /// <returns>保存されたビットマップオブジェクトです。.Save メソッドなどを使用するとファイルに保存できます。</returns>
        public Bitmap GetBitmap(BaseGuiTestObject objGUI, bool isCaptureClient)
        {
            IntPtr hWnd = (IntPtr)objGUI.NativeHandle;
            return this.GetBitmap(hWnd, isCaptureClient);
        }
    }

免責事項

ここで紹介したスクリプトは説明のためのサンプルであり、製品の一部ではございません。スクリプトが実際に動作するか、御社業務に適合するかなどに関しまして、一切の保証はございません。 スクリプト、説明、その他すべてについて、無謬性は保障されません。

ここで紹介するスクリプトの一部、もしくは全部について、弊社に断りなく、御社スクリプトの内部に組み込み、そのままご利用頂いても構いません。

本スクリプトの一部もしくは全部を二次的著作物に対して引用する場合、著作権法の精神に基づき、適切な扱いを行ってください。