Automated Testing & Windows CE
by Tom Pugh

Listing One
// Take a snapshot of the screen
CDC dc;
dc.CreateCompatibleDC(pDriverWindow->GetDC());

// Retrieve the dimensions of the application window
CRect rect;
pDriverWindow->GetWindowRect(&rect);
int nWidth = rect.Width();
int nHeight = rect.Height();

// Fill in the DIB structure
BITMAPINFO dibInfo;
dibInfo.bmiHeader.biBitCount = 24;
dibInfo.bmiHeader.biClrImportant = 0;
dibInfo.bmiHeader.biClrUsed = 0;
dibInfo.bmiHeader.biCompression = 0;
dibInfo.bmiHeader.biHeight = nHeight;
dibInfo.bmiHeader.biPlanes = 1;
dibInfo.bmiHeader.biSize = 40;
dibInfo.bmiHeader.biSizeImage = nWidth*nHeight*3;
dibInfo.bmiHeader.biWidth = nWidth;
dibInfo.bmiHeader.biXPelsPerMeter = 3780;
dibInfo.bmiHeader.biYPelsPerMeter = 3780;
dibInfo.bmiColors[0].rgbBlue = 0;
dibInfo.bmiColors[0].rgbGreen = 0;
dibInfo.bmiColors[0].rgbRed = 0;
dibInfo.bmiColors[0].rgbReserved = 0;

// Create a new device independent bitmap and retrieve a pointer to its bit 
storage -- this is the raw bitmap data that will be sent to the client
BGRBit *pBGR = NULL;
HBITMAP hNewBitmap = CreateDIBSection(dc.GetSafeHdc(), 
                (const BITMAPINFO*)&dibInfo, 
                DIB_RGB_COLORS, (void**)&pBGR, NULL, 0);
HBITMAP hOldBitmap = (HBITMAP) dc.SelectObject(hNewBitmap);

// Copy bitmap into new device context -- this also copies bitmap to DIB
dc.BitBlt(rect.left, rect.top, nWidth, nHeight, pDriverWindow->GetDC(), 
                                                   0, 0, SRCCOPY);
// Determine the buffer size of the data to be sent to the client
int bufSize = sizeof(*pBGR) * nWidth * nHeight;


Listing Two
// Now that we have received click event, we need to adjust positioning a 
// bit so that click is translated properly to correct location. Note that at 
// different resolutions hardcoded numbers might need adjusted (this has not 
// been tested at a resolution other than 640x480 at this time).
pt.x = _wtoi(tcXCoordinate) + 8;
pt.y = _wtoi(tcYCoordinate) + 3;

// Determine the window that is beneath the specified point.
// The mouse click event must be sent directly to this window.
pWindow = WindowFromPoint(pt);

// To simulate a mouse click, the pointer must be in the proper position.
SetCursorPos(pt.x, pt.y);

// Make sure the target window has focus
pDriverWindow->SetFocus();

//Map the screen coordinates to the client
pWindow->ScreenToClient(&pt);

// Send a message to the window indicating the button press
wParam = 0;
lParam = MAKELONG(pt.x, pt.y);
pWindow->PostMessage(WM_LBUTTONDOWN, wParam, lParam);
pWindow->PostMessage(WM_LBUTTONUP, wParam, lParam);

// If this delay does not exist between mouse clicks,
// the application misses some of the clicks.
Sleep(700);   // To give time interval b/n two mouse clicks

Listing Three
void CScreenDriverDlg::CopyBitmap(BGRBit *pBGR, int nWidth, int nHeight)
{
    // If there is a bitmap already stored in the module level
    // bitmap object, delete it
    if ((HBITMAP) bitmap != NULL)
        bitmap.DeleteObject();
    // Create a bitmap compatible with the screen device context
    bitmap.CreateCompatibleBitmap (this->GetDC(), nWidth, nHeight);
    // Fill out the bitmap info structure -- this should be 
    // identical to the server version
    BITMAPINFO dibInfo;
    dibInfo.bmiHeader.biBitCount = 24;
    dibInfo.bmiHeader.biClrImportant = 0;
    dibInfo.bmiHeader.biClrUsed = 0;
    dibInfo.bmiHeader.biCompression = 0;
    dibInfo.bmiHeader.biHeight = nHeight;
    dibInfo.bmiHeader.biPlanes = 1;
    dibInfo.bmiHeader.biSize = 40;
    dibInfo.bmiHeader.biSizeImage = nWidth*nHeight*3;
    dibInfo.bmiHeader.biWidth = nWidth;
    dibInfo.bmiHeader.biXPelsPerMeter = 3780;
    dibInfo.bmiHeader.biYPelsPerMeter = 3780;
    dibInfo.bmiColors[0].rgbBlue = 0;
    dibInfo.bmiColors[0].rgbGreen = 0;
    dibInfo.bmiColors[0].rgbRed = 0;
    dibInfo.bmiColors[0].rgbReserved = 0;
    // Create a temporary device context that will be used
    // to copy the bitmap
    CDC dc;
    dc.CreateCompatibleDC(this->GetDC());
    // Use the module level bitmap as the destination for the
    // device dependent bitmap
    CBitmap *pOldBitmap = dc.SelectObject(&bitmap);
    // Copy the DI bits into the device dependent bitmap
    // We use SetDIBits here instead of SetBitmapBits because it will
    // accept the data in BGR format instead of RGB format.
    SetDIBits(dc, bitmap, 0, nHeight, pBGR, &dibInfo, DIB_RGB_COLORS);
    // Select the old bitmap back into the device context to
    // avoid resource leaks
    dc.SelectObject(pOldBitmap);
    dc.DeleteDC();
    // Place the bitmap into the CStatic 
    HBITMAP hOldBitmap = m_BitmapArea.SetBitmap(bitmap);
}









1


