Advertisement
Guest User

Untitled

a guest
Sep 10th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <ole2.h>
  4. #include <olectl.h>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. bool saveBitmap(LPCSTR filename, HBITMAP bmp, HPALETTE pal)
  10. {
  11.     bool result = false;
  12.     PICTDESC pd;
  13.  
  14.     pd.cbSizeofstruct   = sizeof(PICTDESC);
  15.     pd.picType      = PICTYPE_BITMAP;
  16.     pd.bmp.hbitmap  = bmp;
  17.     pd.bmp.hpal     = pal;
  18.  
  19.     LPPICTURE picture;
  20.     HRESULT res = OleCreatePictureIndirect(&pd, IID_IPicture, false,
  21.                        reinterpret_cast<void**>(&picture));
  22.  
  23.     if (!SUCCEEDED(res))
  24.     return false;
  25.  
  26.     LPSTREAM stream;
  27.     res = CreateStreamOnHGlobal(0, true, &stream);
  28.  
  29.     if (!SUCCEEDED(res))
  30.     {
  31.     picture->Release();
  32.     return false;
  33.     }
  34.  
  35.     LONG bytes_streamed;
  36.     res = picture->SaveAsFile(stream, true, &bytes_streamed);
  37.  
  38.     HANDLE file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0,
  39.                  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  40.  
  41.     if (!SUCCEEDED(res) || !file)
  42.     {
  43.     stream->Release();
  44.     picture->Release();
  45.     return false;
  46.     }
  47.  
  48.     HGLOBAL mem = 0;
  49.     GetHGlobalFromStream(stream, &mem);
  50.     LPVOID data = GlobalLock(mem);
  51.  
  52.     DWORD bytes_written;
  53.  
  54.     result   = !!WriteFile(file, data, bytes_streamed, &bytes_written, 0);
  55.     result  &= (bytes_written == static_cast<DWORD>(bytes_streamed));
  56.  
  57.     GlobalUnlock(mem);
  58.     CloseHandle(file);
  59.  
  60.     stream->Release();
  61.     picture->Release();
  62.  
  63.     return result;
  64. }
  65.  
  66. bool screenCapturePart(int x, int y, int w, int h, LPCSTR fname){
  67.     HDC hdcSource = GetDC(NULL);
  68.     HDC hdcMemory = CreateCompatibleDC(hdcSource);
  69.  
  70.     int capX = GetDeviceCaps(hdcSource, HORZRES);
  71.     int capY = GetDeviceCaps(hdcSource, VERTRES);
  72.  
  73.     HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, w, h);
  74.     HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
  75.  
  76.     BitBlt(hdcMemory, 0, 0, w, h, hdcSource, x, x, SRCCOPY);
  77.     hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);
  78.  
  79.     DeleteDC(hdcSource);
  80.     DeleteDC(hdcMemory);
  81.  
  82.     HPALETTE hpal = NULL;
  83.     if(saveBitmap(fname, hBitmap, hpal)) return true;
  84.     return false;
  85. }
  86.  
  87. BOOL CALLBACK EnumDeskProc(LPTSTR lpszDesktop,LPARAM lParam)
  88. {
  89.     HDESK hDesk;
  90.     string capfilename;
  91.     RECT r;
  92.     HWND hwndDesk;
  93.     cout<<"\t"<<lpszDesktop<<endl;
  94.    
  95.     hDesk = OpenDesktop(lpszDesktop,0,false,READ_CONTROL | DESKTOP_SWITCHDESKTOP);
  96.  
  97.     if(hDesk != NULL)
  98.     {
  99.         SwitchDesktop(hDesk);
  100.  
  101.         hwndDesk = GetDesktopWindow();
  102.         GetWindowRect(hwndDesk,&r);
  103.  
  104.         capfilename = (LPTSTR)lParam;
  105.         capfilename += ".";
  106.         capfilename += lpszDesktop;
  107.         capfilename += ".bmp";
  108.         screenCapturePart(0,0,r.right,r.bottom,capfilename.c_str());
  109.         CloseDesktop(hDesk);
  110.     }
  111.  
  112.     return TRUE;
  113. }
  114.  
  115. BOOL CALLBACK EnumWinStProc(LPTSTR lpszWinSt, LPARAM lParam)
  116. {
  117.     HWINSTA hWinSt;
  118.     cout<<lpszWinSt<<endl;
  119.  
  120.     hWinSt = OpenWindowStation(lpszWinSt,false,READ_CONTROL | WINSTA_ENUMDESKTOPS);
  121.    
  122.     if(hWinSt != NULL)
  123.     {
  124.         EnumDesktops(hWinSt,EnumDeskProc,(LPARAM)lpszWinSt);
  125.         CloseWindowStation(hWinSt);
  126.     }
  127.  
  128.     return TRUE;
  129. }
  130.  
  131. void main(void)
  132. {
  133.     EnumWindowStations(EnumWinStProc,0);
  134.     return;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement