elliottso
December 19th, 2005, 04:28 AM
Hi,
I am trying to implement a screen capture program which will simply output the picture to a window.
I try to modify the following code originally posted here by golanshahar.
RECT rc;
HWND hWnd = ::FindWindow(NULL, "未命名 - 記事本"); // find handle using caption which is only a notepad
::GetWindowRect (hWnd, &rc);
HDC hDC = ::GetDC(0);
HDC memDC = ::CreateCompatibleDC ( hDC );
HBITMAP memBM = ::CreateCompatibleBitmap ( hDC, rc.right-rc.left, rc.bottom-rc.top );
HBITMAP OldBM = (HBITMAP)::SelectObject(memDC, memBM );
::BitBlt( memDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, hDC, rc.left, rc.top , SRCCOPY );
int size = 3 * ( (rc.right-rc.left) * (rc.bottom-rc.top) );
BYTE *lpBits = new BYTE[size];
::GetBitmapBits( memBM, size, lpBits );
BITMAPINFOHEADER bitmapInfo;
::ZeroMemory(&bitmapInfo,sizeof(BITMAPINFOHEADER));
bitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.biWidth = rc.right-rc.left; //enter Width
bitmapInfo.biHeight = rc.bottom-rc.top; //enter Height
bitmapInfo.biPlanes = 1;
bitmapInfo.biBitCount = 24; //bit per pixel 24,32
bitmapInfo.biCompression = 0; //RGB
bitmapInfo.biSizeImage = bitmapInfo.biWidth*bitmapInfo.biHeight*bitmapInfo.biBitCount/8;
HDC hDC2 = ::GetDC((this->GetDlgItem(IDC_PICTURE))->m_hWnd);
::SetDIBitsToDevice(hDC2,
0,0,
rc.right-rc.left,rc.bottom-rc.top,
0,0,
0,rc.bottom-rc.top,
lpBits,
(LPBITMAPINFO)&bitmapInfo,DIB_RGB_COLORS);
delete [] lpBits;
::SelectObject(hDC, OldBM);
::DeleteObject(memBM);
::DeleteObject(memDC);
::ReleaseDC( 0, hDC );
However, when the code is executed, only a bar-code like picture is shown. (the file size is too large to upload)
I would like to ask if there is any problem on the above code?
Thank you.
I am trying to implement a screen capture program which will simply output the picture to a window.
I try to modify the following code originally posted here by golanshahar.
RECT rc;
HWND hWnd = ::FindWindow(NULL, "未命名 - 記事本"); // find handle using caption which is only a notepad
::GetWindowRect (hWnd, &rc);
HDC hDC = ::GetDC(0);
HDC memDC = ::CreateCompatibleDC ( hDC );
HBITMAP memBM = ::CreateCompatibleBitmap ( hDC, rc.right-rc.left, rc.bottom-rc.top );
HBITMAP OldBM = (HBITMAP)::SelectObject(memDC, memBM );
::BitBlt( memDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, hDC, rc.left, rc.top , SRCCOPY );
int size = 3 * ( (rc.right-rc.left) * (rc.bottom-rc.top) );
BYTE *lpBits = new BYTE[size];
::GetBitmapBits( memBM, size, lpBits );
BITMAPINFOHEADER bitmapInfo;
::ZeroMemory(&bitmapInfo,sizeof(BITMAPINFOHEADER));
bitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.biWidth = rc.right-rc.left; //enter Width
bitmapInfo.biHeight = rc.bottom-rc.top; //enter Height
bitmapInfo.biPlanes = 1;
bitmapInfo.biBitCount = 24; //bit per pixel 24,32
bitmapInfo.biCompression = 0; //RGB
bitmapInfo.biSizeImage = bitmapInfo.biWidth*bitmapInfo.biHeight*bitmapInfo.biBitCount/8;
HDC hDC2 = ::GetDC((this->GetDlgItem(IDC_PICTURE))->m_hWnd);
::SetDIBitsToDevice(hDC2,
0,0,
rc.right-rc.left,rc.bottom-rc.top,
0,0,
0,rc.bottom-rc.top,
lpBits,
(LPBITMAPINFO)&bitmapInfo,DIB_RGB_COLORS);
delete [] lpBits;
::SelectObject(hDC, OldBM);
::DeleteObject(memBM);
::DeleteObject(memDC);
::ReleaseDC( 0, hDC );
However, when the code is executed, only a bar-code like picture is shown. (the file size is too large to upload)
I would like to ask if there is any problem on the above code?
Thank you.