5

I am trying to make a program to work on top of an existing GUI to annotate it and provide extra calculations and statistical information. I want to do this using image recognition, as I have learned a fair amount about this in University using Matlab and similar things. I can get a handle to the window I want to perform image recognition on, but I don't know how to turn that handle into an image of that window, and all its visible child windows. I suppose I am looking for something like the screenshot function, but restricted to a single window.

How would I go about doing this? I suppose I'd need something like a .bmp to mess about with. Also, it would have to be efficient enough that I could call it several times a second without my PC grinding to a halt.

Hopefully this isn't an obvious question, I typed some things into google but didn't get anything related.

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
Oliver
  • 11,297
  • 18
  • 71
  • 121

2 Answers2

4

I think CImage class will be helpful.

void CreateImage(HWND hwnd)
{
    CImage img;
    img.m_hDC = ::GetWindowDC(hwnd);
    img.Save(strFileName);
}
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
Ashish
  • 8,441
  • 12
  • 55
  • 92
3

One simple way is using the PrintWindow API (which is an automated Alt + Print basically). The following example takes a screenshot of the calculator, but you would just have to replace the handles.

void CScreenShotDlg::OnPaint()
{
    // device context for painting
    CPaintDC dc(this);

    // Get the window handle of calculator application.
    HWND hWnd = ::FindWindow( 0, _T( "Calculator" ));

    // Take screenshot.
    PrintWindow( hWnd,
                 dc.GetSafeHdc(),
                 0 );
}

(see https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-printwindow )

z0rberg's
  • 674
  • 5
  • 10
Leo
  • 37,640
  • 8
  • 75
  • 100