Simulating print Screen functions
Phoenix_256

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

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.

Jakub Wasilewski

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.

Dustin Dettmer

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

BAF

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

Andrei Ellman
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.

Thread #590017. Printed from Allegro.cc