-
get screenshot
I'm making a program that I want to simply get a screenshot of the desktop and then display that single image in the background of the window I create. I can get everything to work so far except to get the bitmap to be drawn in the window (I'm assuming it gets captured). This is my first time working with windows, so on a large scale I don't know what I'm doing. I'd really appreciate if someone could look at my file and see if you can tell me what I'm doing wrong.
-
Screen capture ( desktop capture ) questions arise periodically so I have modified your code to accomplish that and as a reference for future questions. Further examples can be found in the CodeGuru article various methods for capturing the screen.
Code:
/*screen capture example*/
#include <windows.h>
#define SCREENWIDTH GetSystemMetrics(SM_CXSCREEN)
#define SCREENHEIGHT GetSystemMetrics(SM_CYSCREEN)
HBITMAP g_hDeskBmp;
HDC g_hMemDC;
int g_nDCdata;
void CaptureDesktop()
{
/*capture desktop and store for later use*/
HDC hdcDesk;
hdcDesk=GetDC(HWND_DESKTOP); /*equivalent to hdcDesk=GetDC((HWND)0);*/
g_hMemDC=CreateCompatibleDC(0);
g_hDeskBmp=CreateCompatibleBitmap(hdcDesk, SCREENWIDTH, SCREENHEIGHT);
/*fill the bitmap with contents of desktop*/
g_nDCdata=SaveDC(g_hMemDC);
SelectObject(g_hMemDC,g_hDeskBmp);
BitBlt(g_hMemDC, 0, 0, SCREENWIDTH, SCREENHEIGHT,hdcDesk,0,0,SRCCOPY);
ReleaseDC(HWND_DESKTOP,hdcDesk);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
CaptureDesktop();
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
BitBlt(ps.hdc,0,0,SCREENWIDTH,SCREENHEIGHT,g_hMemDC,0,0, SRCCOPY);
EndPaint(hwnd,&ps);
break;
}
case WM_DESTROY:
/*restore memory dc to original state
and free up gdi resources*/
RestoreDC(g_hMemDC,g_nDCdata);
DeleteDC(g_hMemDC);
DeleteObject(g_hDeskBmp);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWindow;
MSG msg;
WNDCLASSEX wc;
/*registering the window class*/
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName= "windowclass";
wc.hIconSm = NULL;
/*make sure window class has registered*/
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Window Registration Failed","Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
/*create screensaver window*/
hWindow=CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"screensaver",
WS_OVERLAPPEDWINDOW,0,0,
SCREENWIDTH,SCREENHEIGHT,
NULL,NULL,hInstance,NULL);
ShowWindow(hWindow,nCmdShow);
UpdateWindow(hWindow);
if(hWindow == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
/*main message loop*/
while(GetMessage(&msg,NULL,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
-
I would suggest looking up the DIB library and making use of it. It has several very very useful functions. You can capture the entire screen or parts of a screen and draw bitmaps to windows, and save bitmaps, etc.
-
thanks guys. Ken, your code works great. I have one more question, which is how can I make the window so that it doesnt have any borders, especially the one at the toP?
-
>>how can I make the window so that it doesnt have any borders, especially the one at the toP?<<
Set the window style in your call to CreateWindowEx to WS_POPUP. Either set the extended style parameter of CreateWindowEx to zero or, for something like a screensaver, WS_EX_TOPMOST may be a good idea.
For more information see extended window styles and window styles.