0

I have become increasingly interested in artifical intelligence, but I am still learning the basics of the windows API. My current goal is to create a c++ process which calls SendInput to simulate PrtSc being pressed (print screen button). This is the only way I currently can think of being able to have a program be able to see one frame of the screen as human might.

As a simple example, lets say, for some reason, I wanted my program to "look and see for itself," what time the clock on Windows 7 says. So to make the task easy for the computer and for myself, lets say we know the toolbar is on the bottom of the screen. We already know the exact screen coordinates of the rectangular bitmap we want to examine. So we call SendInput to simulate pressing PrtSc and copy the screen display to the clipboard and we examine the clipboard at those coordinates and do some feature-detection to see what numbers there are, and the rest is downhill from there.

So my question then, is this a reasonable way to implement such a program? Or am I making this more difficult than it needs to be?

Leonardo
  • 1,452
  • 3
  • 15
  • 26
  • 2
    You can use GDI+ to directly read the screen without messing with the clipboard. – SLaks Mar 14 '13 at 20:38
  • @SLaks Thanks, do you happen to know what routine will allow me to get a bitmap of a given rectangular area of the display using this library? – Leonardo Mar 14 '13 at 20:43
  • 1
    http://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen – Jammerx2 Mar 14 '13 at 20:43
  • @SLaks: You can also use plain GDI to grab screenshots as well, you don't need GDI+ for it. – Remy Lebeau Mar 14 '13 at 20:49
  • Off-Topic: The task bar might not be in its default location (bottom), might be hidden (auto hide), or the clock might not be on (visible). You should also check for that. Also, to make your program more _intelligent_ you should have your own built-in timer/clock, and as soon as you get the time _once_ using your screen-capture method, you should _remember_ the time and keep track of it. Whenever you need to get the time again, after say 20 minutes, then you do a little math to get it internally, without screen-capturing it again. – Karim ElDeeb Mar 15 '13 at 07:47

1 Answers1

3

You don't need to simulate the PrtSc button at all in order to grab a screenshot. You can grab a screenshot directly from the screen and copy it into an in-memory bitmap, eg:

int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

HDC hScreenDC = GetDC(0);
HDC hMemDC = CreateCompatibleDC(hScreenDC);
HBITMAP hScreenBmp = CreateCompatibleBitmap(hMemDC, iScreenWidth, iScreenHeight);
HBITMAP hOldBmp = SelectObject(hMemDC, hScreenBmp);
BitBlt(hMemDC, 0, 0, iScreenWidth, iScreenHeight, hScreenDC, 0, 0, SRCCOPY);
ReleaseDC(0, hScreenDC);

// use hScreenBmp as needed...

SelectObject(hMemDC, hOldBmp);
DeleteObject(hScreenBmp);

For getting the clock value specifically, why bother with a screenshot at all? Just use the GetLocalTime() function instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Haha, yeah I know why bother right? I am just becoming fascinated with teaching my computer to become "visually" sentient. Thanks for that code snippet. Mostly I want to learn more about feature-recognition and this code is the first big step. – Leonardo Mar 14 '13 at 21:04