Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Simulating print Screen functions

This thread is locked; no one can reply to it. rss feed Print
Simulating print Screen functions
Phoenix_256
Member #8,316
February 2007

Heya,

I want to write a program that startup takes a screenshot of what ever is on the screen to use as a background within the program.

I've already found the fantastic code to get a bitmap from the clipboard, so I thought that using 'simulate_key(KEY_PRTSCR);' would copy it to the clipboard, but Windows doesn't accept the key press (it ignores it) I guess that simulate_key simply sets that keys flag to true, but what I'd like to find out is; is there another way to capture the screen image?

I've included the code below.

Cheers

Phoenix

1#include <allegro.h>
2#include <winalleg.h>
3 
4BITMAP* getBitmapFromClipboard() {
5 BITMAP *result = NULL;
6
7 if (!IsClipboardFormatAvailable(CF_BITMAP)) {
8 return NULL;
9 }
10
11 if (OpenClipboard(win_get_window())) {
12 HBITMAP hbmp;
13
14 hbmp = GetClipboardData(CF_BITMAP);
15 if (hbmp != NULL) {
16 result = convert_hbitmap_to_bitmap(hbmp);
17 }
18 CloseClipboard();
19 }
20 return result;
21}
22 
23 
24 
25int main(int argc, char** argv) {
26 BITMAP *bmp;
27
28 allegro_init();
29 install_timer();
30 install_mouse();
31 install_keyboard();
32 simulate_keypress(KEY_PRTSCR);
33 rest(1000);
34 set_color_depth(32);
35 set_gfx_mode(1,1024,768, 0, 0);
36
37 bmp = getBitmapFromClipboard();
38
39 if (bmp) {
40 // Draw a rectangle on it just to test!!
41 rectfill(bmp,100,100,200,200,makecol(255,0,255));
42 blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
43 }
44 clear_keybuf();
45 readkey();
46 
47 destroy_bitmap(bmp);
48 
49 allegro_exit();
50}
51END_OF_MAIN()

Andrei Ellman
Member #3,434
April 2003

Allegro does not provide a way to grab an image of the desktop. Each operating system has a different means of accessing the desktop image. For Windows, you might want to look at the sourcecode for ChromaPlas to see how it's done on Windows.

AE.

--
Don't let the illegitimates turn you into carbon.

Jakub Wasilewski
Member #3,653
June 2003
avatar

Basically, you would use GetDC(NULL) to get a HDC for the entire screen.

Then, you can use Windows specific Allegro functions, described here in the manual. Specifically, you want blit_from_hdc.

If you want to use that, be sure to include winalleg.h.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

Dustin Dettmer
Member #3,935
October 2003
avatar

Grabbing a picture of the desktop is no longer possible for screen savers as of XP.

BAF
Member #2,981
December 2002
avatar

But you can grab a screenshot in a screen saver, right?

Andrei Ellman
Member #3,434
April 2003

Dustin Dettmer said:

Grabbing a picture of the desktop is no longer possible for screen savers as of XP.

Is that only as of XP SP2? I could have sworn I've gotten ChromaPlas in desptop-image mode to work on an XP-based machine.

Even if it were true, it would be a pity, as it would prevent the many screensavers that maipulate the desktop image from working.

AE.

--
Don't let the illegitimates turn you into carbon.

Go to: