Solved

how do I feed hwnd to screencapture?

Posted on 2007-11-26
21
474 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:Troudeloup
  • 11
  • 7
  • 3
21 Comments
 

Author Comment

by:Troudeloup
Comment Utility
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?
}
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
>>what does this function return?

COLORREF is a 32-bit value used to specify an RGB color and has the following hexadecimal form:

0x00bbggrr
 
The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The high-order byte must be zero. The maximum value for a single byte is 0xFF.

0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
Comment Utility
Should be something like:

HBITMAP CaptureWindow(HWND hWnd)

{

     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);

     SelectObject(hCaptureDC, oldBmp);
 

     DeleteDC(hCaptureDC);

     DeleteObject(hBitmap);
 

     if (!return)

          return NULL;

     else

          return hBitmap;

}

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
IOW, to get the single colors ot of a COLORREF, you need to use some bit operations, e.g.

#define RED(x) (x & 0x000000ff)
#define GREEN(x) (x & 0x0000ff00)
#define BLUE(x) (x & 0x00ff0000)
0
 

Author Comment

by:Troudeloup
Comment Utility
in your code example,

return hBitmap



is this what I feed to getpixel?
0
 

Author Comment

by:Troudeloup
Comment Utility
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?
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
Comment Utility
Oh, wait, use my function this way:

void CaptureWindow(HWND hWnd)
{
     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);
 

     // you can use getpixel right here, passing hCaptureDC
 

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

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
Comment Utility
Retouched version:


void CaptureWindow(HWND hWnd)

{

     RECT rect;

     if  (!GetWindowRect(hWnd, &rect))

          return;

     HDC hWindowDC = ::GetDC(hWnd);

     if (!hWindowDC)

          return;

     HDC hCaptureDC = CreateCompatibleDC(hWindowDC);

     if (!hCaptureDC)

          return;

     HBITMAP hBitmap = CreateCompatibleBitmap(hWindowDC, rect.right-rect.left+1, rect.top-rect.bottom+1));

     if (!hBitmap) {

          DeleteDC(hCaptureDC);

          return;

     }

     HBITMAP oldBmp = (HBITMAP)SelectObject(hCaptureDC, hBitmap);

     int result = PrintWindow(hWnd, hCaptureDC, 0);

  
 

     // you can use getpixel right here, passing hCaptureDC

 
 

     // cleaning tasks before returning

     SelectObject(hCaptureDC, oldBmp);

     DeleteDC(hCaptureDC);

     DeleteObject(hBitmap);

 }

Open in new window

0
 

Author Comment

by:Troudeloup
Comment Utility
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?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
Actually, there is already a 'GetPixel()' (http://msdn2.microsoft.com/en-us/library/ms532282.aspx) that you can use:

#define RED(x) (x & 0x000000ff)
#define GREEN(x) (x & 0x0000ff00)
#define BLUE(x) (x & 0x00ff0000)

COLORREF cr = GetPixel(hCaptureDC,x,y);

BYTE red = RED(cr);
BYTE green = GREEN(cr);
BYTE blue = BLUE(cr);
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Troudeloup
Comment Utility
yeah that's one i planned to use.


would it help it to run faster if I keep the same dc?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
Well, which DC do you mean? You can read the pixel values directly from the Window DC, bu that would not make a big difference in speed, just a small one.
0
 

Author Comment

by:Troudeloup
Comment Utility
how about doing this in 3 functions?

1st to return dc with capture on it
2nd given dc return getpixel
3rd  release dc.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
Um, you can directly call 'GetPixel()' on

 HDC hWindowDC = ::GetDC(hWnd);

given that the contents do not change during the time you need.
0
 

Author Comment

by:Troudeloup
Comment Utility
i am, not sure how to do it, you mean


HDC hWindowDC = ::GetDC(hWnd);

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


?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
Yes, that would work, however it will fail if the window repaints. I'd actually use the function above "as is", looks good to me.
0
 

Author Comment

by:Troudeloup
Comment Utility
that't ALL I need ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
Well, as a first step towards OCR, it is. Once you have a copy of the DC, you can start analyzing it.
0
 

Author Comment

by:Troudeloup
Comment Utility
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

?
0
 

Author Comment

by:Troudeloup
Comment Utility
oh party begins with that :p
0
 

Author Comment

by:Troudeloup
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now