K
Kodiak
I have this code snippet below and what I am trying to due is return
the sum of the byte returned from the GetDIBits() method. What I am
trying to due is if you open up notepad and leave the text area empty
you should get the sum of the bits returned in the byte data. If you
type some text into the notepad area and run the program again the sum
of the bits should be different from the first sum. Everytime I run
the program I get the same result every time. I do not know if there
is something wrong with my logic, but if someone can help me with this
problem I would appreciate it.
int main()
{
HWND hWnd;
RECT rcWnd;
hWnd = FindWindow("Notepad", "Untitled - Notepad");
GetWindowRect(hWnd, &rcWnd);
int x = rcWnd.right - rcWnd.left;
int y = rcWnd.bottom - rcWnd.top;
int left = rcWnd.left;
int top = rcWnd.top;
int width = x - left;
int height = y - top;
HDC hDC;
HDC hBitmapDC;
HBITMAP hBitmap;
HGDIOBJ hobj;
byte* bitmap;
byte* data;
DWORD sum, result;
long length;
LPBITMAPINFOHEADER lpbih;
sum = NULL;
hDC = GetDC(hWnd);
bitmap = new byte[sizeof(BITMAPINFOHEADER)+x*y*4];
if(bitmap)
{
length = x*y*4;
memset(bitmap,0,sizeof(BITMAPINFOHEADER)+x*y*4);
hBitmapDC = CreateCompatibleDC(hDC);
hBitmap=CreateCompatibleBitmap(hDC,x,y);
hobj = SelectObject(hBitmapDC,hBitmap);
BitBlt(hBitmapDC,0,0,x,y,hDC,rcWnd.left,rcWnd.top,SRCCOPY);
hobj = SelectObject(hBitmapDC,hobj);
lpbih = LPBITMAPINFOHEADER(bitmap);
lpbih->biSize = sizeof(BITMAPINFOHEADER);
lpbih->biWidth = x;
lpbih->biHeight = y;
lpbih->biBitCount = 32; //say our desired format is 32bpp
BI_RGB
lpbih->biPlanes = 1;
lpbih->biCompression = BI_RGB;
data = bitmap+sizeof(BITMAPINFOHEADER);
if(GetDIBits(hBitmapDC,hBitmap,0,0,data,LPBITMAPINFO(lpbih),DIB_RGB_COLORS))
{
for(int i=0; i<length; i++)
{
sum += (DWORD)data;
}
result = sum;
}
}
ReleaseDC((HWND)hWnd, hDC);
DeleteDC(hDC);
DeleteDC(hBitmapDC);
DeleteObject(hBitmap);
return 0;
}
the sum of the byte returned from the GetDIBits() method. What I am
trying to due is if you open up notepad and leave the text area empty
you should get the sum of the bits returned in the byte data. If you
type some text into the notepad area and run the program again the sum
of the bits should be different from the first sum. Everytime I run
the program I get the same result every time. I do not know if there
is something wrong with my logic, but if someone can help me with this
problem I would appreciate it.
int main()
{
HWND hWnd;
RECT rcWnd;
hWnd = FindWindow("Notepad", "Untitled - Notepad");
GetWindowRect(hWnd, &rcWnd);
int x = rcWnd.right - rcWnd.left;
int y = rcWnd.bottom - rcWnd.top;
int left = rcWnd.left;
int top = rcWnd.top;
int width = x - left;
int height = y - top;
HDC hDC;
HDC hBitmapDC;
HBITMAP hBitmap;
HGDIOBJ hobj;
byte* bitmap;
byte* data;
DWORD sum, result;
long length;
LPBITMAPINFOHEADER lpbih;
sum = NULL;
hDC = GetDC(hWnd);
bitmap = new byte[sizeof(BITMAPINFOHEADER)+x*y*4];
if(bitmap)
{
length = x*y*4;
memset(bitmap,0,sizeof(BITMAPINFOHEADER)+x*y*4);
hBitmapDC = CreateCompatibleDC(hDC);
hBitmap=CreateCompatibleBitmap(hDC,x,y);
hobj = SelectObject(hBitmapDC,hBitmap);
BitBlt(hBitmapDC,0,0,x,y,hDC,rcWnd.left,rcWnd.top,SRCCOPY);
hobj = SelectObject(hBitmapDC,hobj);
lpbih = LPBITMAPINFOHEADER(bitmap);
lpbih->biSize = sizeof(BITMAPINFOHEADER);
lpbih->biWidth = x;
lpbih->biHeight = y;
lpbih->biBitCount = 32; //say our desired format is 32bpp
BI_RGB
lpbih->biPlanes = 1;
lpbih->biCompression = BI_RGB;
data = bitmap+sizeof(BITMAPINFOHEADER);
if(GetDIBits(hBitmapDC,hBitmap,0,0,data,LPBITMAPINFO(lpbih),DIB_RGB_COLORS))
{
for(int i=0; i<length; i++)
{
sum += (DWORD)data;
}
result = sum;
}
}
ReleaseDC((HWND)hWnd, hDC);
DeleteDC(hDC);
DeleteDC(hBitmapDC);
DeleteObject(hBitmap);
return 0;
}