Displaying a screenshot on a static

Hello everyone,

I have been driving myself crazy trying to do something that to me seems like it should be relatively straight forward. In my program (Win32 using Dev-C++ 4.9.9.2) I am trying to take a screenshot of the entire desktop, save it to a HBITMAP and apply it to a static control. This is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
HWND StaticControl=CreateWindowEx(
0,"Static","",WS_VISIBLE|WS_CHILD|SS_BITMAP,
10,10,800,600,ParentWindow,NULL,hInstance,NULL);//create standard static control

screenDC=GetDC(NULL);//global screen DC
hBitmap=CreateCompatibleBitmap(screenDC,Screen.x,Screen.y);//global HBITMAP

if(hBitmap!=NULL){//check if bitmap is NULL(never is)
SendMessage(StaticControl,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
}else{
MessageBox(ParentWindow,"bitmap is NULL...","Oh Noes:",MB_OK);
}


When my window opens it looks like a normal window (title bar,caption buttons,etc...) and the static control is there too, but it is completely black. Also the variable hBitmap is never NULL.

I have no idea what is going wrong here. I have been able to apply file bitmaps (.bmp's) to a static before using almost the exact same code. I have also tried to paint the hBitmap using GDI+, and have achieved similear (all black) results. I would prefer to do this without GDI+ so that I can add button controls (and the like) and not have to deal with painting .

I do not know where to go from here so if anyone has any suggesstions, it would be much appreciated.

Thanks again for all the help


Sigma
http://www.apitalk.com/document.php?id=1184209000_1

Irrelevant.
Bitblt() is even not called in original code (!)
so?

my point being that doing a google search would probably answer his question faster. OTOH your post seems quite redundant.
Thanks for the help, but I was able to figure it out on my own. If anyone wants it, I wrapped it into a function that returns a HBITMAP directly.


1
2
3
4
5
6
7
8
9
10
11
12
13
HBITMAP TakeScreenshot(HWND hwnd,DWORD dwWindowX,DWORD dwWindowY){

HDC hScreenDC=GetDC(hwnd);//global screen DC
HDC hCompatibleDC=CreateCompatibleDC(hScreenDC);
HBITMAP screenshot=CreateCompatibleBitmap(hScreenDC,dwWindowX,dwWindowY);
SelectObject(hCompatibleDC,screenshot);

BitBlt(hCompatibleDC,0,0,dwWindowX,dwWindowY,hScreenDC,0,0,SRCCOPY);
ReleaseDC(NULL,hScreenDC);
DeleteDC(hCompatibleDC);

return screenshot;
}


Thanks again

Sigma
Topic archived. No new replies allowed.