Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

how do I feed hwnd to screencapture?

this is by using windows sdk and I am calling its functions and objects.

I have my handle from this line



HWND hwnd = FindWindow(NULL,"Notepad");


how do I feed it to this function?


note that this function is from a demonstration of how to capture screen.


Void CaptureScreen()
{
    int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    HWND hDesktopWnd = GetDesktopWindow();
    HDC hDesktopDC = GetDC(hDesktopWnd);
    HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
    HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
                            nScreenWidth, nScreenHeight);
    SelectObject(hCaptureDC,hCaptureBitmap);
    BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
           hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
    SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code
                                //here to save the captured image to disk
    ReleaseDC(hDesktopWnd,hDesktopDC);
    DeleteDC(hCaptureDC);
    DeleteObject(hCaptureBitmap);
}





and then eventually I want to use this function to get pixel color data in RGB form,  how do I feed hdc from above to this function?


COLORREF GetPixel(
  HDC hdc,    // handle to DC
  int nXPos,  // x-coordinate of pixel
  int nYPos   // y-coordinate of pixel
);



what does this function return?


thanks for reading
Avatar of Troudeloup
Troudeloup

ASKER

Void CaptureScreen()
{
    int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    HWND hDesktopWnd = GetDesktopWindow();
    HDC hDesktopDC = GetDC(hDesktopWnd);
    HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
    HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
                            nScreenWidth, nScreenHeight);
    SelectObject(hCaptureDC,hCaptureBitmap);

    //  I run this to prepare to capture screen, and after this I can run


    // I run this to capture. but I can't make it out the part to pass it to the getpixel function

    BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
           hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);



    SaveCapturedBitmap(hCaptureBitmap);
    //Place holder - Put your code
    //here to save the captured image to disk

    // what does this do?



    ReleaseDC(hDesktopWnd,hDesktopDC);
    DeleteDC(hCaptureDC);
    DeleteObject(hCaptureBitmap);
   
    // I run these 3 only when I am done capturing right?
}
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in your code example,

return hBitmap



is this what I feed to getpixel?
a problem, I don't get your code is doing.


    BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
           hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);



where is the equivalent of this one?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
void CaptureWindow(HWND hWnd)             //     maybe I need to split this into 3 functions
                                          //     see below
{
     RECT rect;
     if  (!GetWindowRect(hWnd, &rect))
          return NULL;
     HDC hWindowDC = ::GetDC(hWnd);
     if (!hWindowDC)
          return NULL;
     HDC hCaptureDC = CreateCompatibleDC(hWindowDC);
     if (!hCaptureDC)
          return NULL;
     HBITMAP hBitmap = CreateCompatibleBitmap(hWindowDC, rect.right-rect.left+1, rect.top-rect.bottom+1));
     if (!hBitmap) {
          DeleteDC(hCaptureDC);
          return NULL;
     }


     HBITMAP oldBmp = (HBITMAP)SelectObject(hCaptureDC, hBitmap);
     int result = PrintWindow(hWnd, hCaptureDC, 0);
 
   
     //     does it help speed to keep the same DC and then reuse it by capturing onto it?
     //     if it helps, i would do
     //    
     //     //  a function to create a empty DC, return it
     //     //  capture onto this DC,            return it
     //     //  finishe with the DC,             run below
     //     //  to use this DC, see bottom


     // cleaning tasks before returning
     SelectObject(hCaptureDC, oldBmp);
     DeleteDC(hCaptureDC);
     DeleteObject(hBitmap);
 }








// you can use getpixel right here, passing hCaptureDC
//I need to access a lot of points with a function like this
//so I need to return hCaptureDC from above function somehow


getpixel ( ?type,   hCaptureDC,  x, y)
{
  //  place holder
  return color;
}






does this make sense?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah that's one i planned to use.


would it help it to run faster if I keep the same dc?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how about doing this in 3 functions?

1st to return dc with capture on it
2nd given dc return getpixel
3rd  release dc.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i am, not sure how to do it, you mean


HDC hWindowDC = ::GetDC(hWnd);

byte bytecolor = getpixel ( hWindowDC, x, y );


?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that't ALL I need ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HDC hWindowDC = ::GetDC(hWnd);
(?)          COLORREF GetPixel(   HDC hWindowDC, int x, int y );



I have problem with the (?) part.

I didn't get the type of variable I need to store the values

also, how do I convert those values into

int red
int green
int blue

?
oh party begins with that :p