BitCount Help

Discussion in 'C++' started by Kodiak, Nov 28, 2006.

  1. Kodiak

    Kodiak Guest

    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;
    }
     
    Kodiak, Nov 28, 2006
    #1
    1. Advertising

  2. Kodiak

    BobR Guest

    Kodiak wrote in message
    >


    I removed all the parts that this NG does not cover.

    >int main(){
    > long length;
    > for(int i=0; i<length; i++){ }
    > return 0;
    >}


    Hmm, not much left. What was your C++ question on that?

    Oh, you *should* initialise 'length'.

    --
    Bob R
    POVrookie
     
    BobR, Nov 29, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    813
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    790
    Harvey
    Jul 16, 2004
  3. Adam Chapman

    newbie: bitcount for unsigned interegers

    Adam Chapman, Oct 27, 2008, in forum: C Programming
    Replies:
    2
    Views:
    1,316
    Lew Pitcher
    Oct 27, 2008
  4. Replies:
    10
    Views:
    784
    Flash Gordon
    Nov 8, 2008
  5. MrGeek

    bitcount

    MrGeek, Jun 17, 2009, in forum: C Programming
    Replies:
    1
    Views:
    473
    MrGeek
    Jun 17, 2009
Loading...

Share This Page