使用Win Ce6的屏幕截图 [英] Screenshot using Win CE6

查看:17
本文介绍了使用Win Ce6的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以用来截取Windows Ce6应用程序的屏幕截图吗?我有一个现有的应用程序,我想从我的应用程序截图。我需要从应用程序本身激活一个截图请求,并将截图保存到预定义的位置。我一直在网上寻找答案,但实际上什么也没有找到。如有任何帮助,我们将不胜感激。

我已经设法启动并运行了一些东西,但它不能给出完整的位图。相反,它只提供位图的下半部分。对于哪里出了问题,有什么建议吗?

截图编码

HWND DesktopHwnd = ::GetDesktopWindow();
   RECT DesktopParams;
   HDC DevC = ::GetDC(DesktopHwnd);
   ::GetWindowRect(DesktopHwnd,&DesktopParams);
   DWORD Width = DesktopParams.right - DesktopParams.left;
   DWORD Height = DesktopParams.bottom - DesktopParams.top;

   // get the device context of the screen
   HDC hScreenDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);     
   // and a device context to put it in
   HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

   int x = GetDeviceCaps(hScreenDC, HORZRES);
   int y = GetDeviceCaps(hScreenDC, VERTRES);

   // maybe worth checking these are positive values
   HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);

   // get a new bitmap
   HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);

   BitBlt(hMemoryDC, 0, 0, Width, Height, hScreenDC, 0, 0, SRCCOPY);
   hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);

   // now your image is held in hBitmap. You can save it or do whatever with it
   HWND hwnd = DesktopHwnd;
   PBITMAPINFO pbi; 
   HBITMAP hBMP = hBitmap;
   HDC hDC = DevC;
   HANDLE hf;                  // file handle  
   BITMAPFILEHEADER hdr;       // bitmap file-header  
   PBITMAPINFOHEADER pbih;     // bitmap info-header
   DWORD dwTotal;              // total count of bytes  
   DWORD cb;                   // incremental count of bytes  
   BYTE *hp;                   // byte pointer  
   DWORD dwTmp; 
   int ret = 0;

   pbi = CreateBitmapInfoStruct(NULL, hBMP);
   if(pbi == NULL)
   {
   }
   pbih = (PBITMAPINFOHEADER) pbi; 

   RGBQUAD *rgbq;
   rgbq = pbi->bmiColors;
   PALETTEENTRY pe[256];
   GetSystemPaletteEntries(hDC, 0, pbih->biClrUsed, pe);
   for(DWORD i = 0; i < pbih->biClrUsed; i++)
   {
     rgbq[i].rgbRed = pe[i].peRed;
     rgbq[i].rgbBlue = pe[i].peBlue;
     rgbq[i].rgbGreen = pe[i].peGreen;
     rgbq[i].rgbReserved = 0;
   }

   // CE5.0 + CE6.0
   HBITMAP h = CreateDIBSection(hScreenDC, pbi, DIB_RGB_COLORS, (void **)&hp, NULL, 0);
   if(h == NULL)
   {
     goto close_bmp;
   }
   SelectObject(hMemoryDC, h);
   BitBlt(hMemoryDC, 0, 0, Width, Height, hScreenDC, 0, 0, SRCCOPY);

   // Create the .BMP file.  
   hf = CreateFile(_T("\FlashDisk\image1.bmp"),GENERIC_READ | GENERIC_WRITE,(DWORD) 0, NULL, 
                  CREATE_ALWAYS, 
                FILE_ATTRIBUTE_NORMAL, 
                (HANDLE) NULL);
   if (hf == INVALID_HANDLE_VALUE) 
   {
     goto close_bmp;
   }
   hdr.bfType = 0x4D42;        // 0x42 = "B" 0x4d = "M"  
   // Compute the size of the entire file.  
   hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage); 
   hdr.bfReserved1 = 0; 
   hdr.bfReserved2 = 0; 

   // Compute the offset to the array of color indices.  
   hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                 pbih->biSize + pbih->biClrUsed 
                 * sizeof (RGBQUAD); 

   // Copy the BITMAPFILEHEADER into the .BMP file.  
   if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), 
     (LPDWORD) &dwTmp,  NULL)) 
   {
     goto close_bmp;
   }

   // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
   if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
               + pbih->biClrUsed * sizeof (RGBQUAD), 
               (LPDWORD) &dwTmp, ( NULL)))
   {
   }

   // Copy the array of color indices into the .BMP file.  
   dwTotal = cb = pbih->biSizeImage; 

   //hp = lpBits;     
   if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) 
   {
     goto close_bmp;
   }

   close_bmp:
   // Close the .BMP file.  
   if(hf != INVALID_HANDLE_VALUE)
   {
     if (!CloseHandle(hf)) 
     {
        int xyz = 2;
        xyz = 3;
     }
     else
     {
         ret = 1;
     }
   }

   if(h != NULL)
     DeleteObject(h);
   if(pbi != NULL)
   {
     free(pbi);
   }

   // clean up
   if(hMemoryDC != NULL)
      DeleteDC(hMemoryDC);
   DeleteDC(hScreenDC);

干杯。

推荐答案

我终于得到了一个屏幕截图。这可能会帮助其他人在未来从Win Ce6应用程序中截屏。希望这能帮助其他人。

void MyScreenshotFunction::Screenshot()
       {
          HWND DesktopHwnd = ::GetDesktopWindow();
          RECT DesktopParams;
          HDC DevC = ::GetDC(DesktopHwnd);
          ::GetWindowRect(DesktopHwnd,&DesktopParams);
          DWORD Width = DesktopParams.right - DesktopParams.left;
          DWORD Height = DesktopParams.bottom - DesktopParams.top;

          DWORD FileSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+(sizeof(RGBTRIPLE)+1*(Width*Height*4));
          char *BmpFileData = (char*)GlobalAlloc(0x0040,FileSize);

          PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
          PBITMAPINFOHEADER  BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData[sizeof(BITMAPFILEHEADER)];

          BFileHeader->bfType = 0x4D42; // BM
          BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
          BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);

          BITMAPINFO bmi;
          ZeroMemory(&bmi, sizeof(BITMAPINFO));
          bmi.bmiHeader.biSize            =   sizeof(BITMAPINFOHEADER);
          bmi.bmiHeader.biWidth           =   Width;
          bmi.bmiHeader.biHeight          =   Height;
          bmi.bmiHeader.biPlanes          =   1;
          bmi.bmiHeader.biBitCount        =   24;
          bmi.bmiHeader.biCompression     =   BI_RGB;
          bmi.bmiHeader.biSizeImage       =   bmi.bmiHeader.biWidth * abs(bmi.bmiHeader.biHeight) * 3;

          unsigned char *BitsRGB=0;
          HDC CaptureDC = CreateCompatibleDC(0);
          HBITMAP CaptureBitmap = CreateDIBSection(DevC, &bmi, DIB_RGB_COLORS, (void**)&BitsRGB, NULL, 0);
          SelectObject(CaptureDC,CaptureBitmap);
          BitBlt(CaptureDC, 0, 0, Width, Height, DevC, 0, 0, SRCCOPY);

          BITMAPFILEHEADER hdr;       // bitmap file-header
          hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"  
          // Compute the size of the entire file.  
          hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSize + bmi.bmiHeader.biClrUsed * sizeof(RGBQUAD) + bmi.bmiHeader.biSizeImage);
          // Compute the offset to the array of color indices.  
          hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSize + bmi.bmiHeader.biClrUsed * sizeof (RGBQUAD);
          hdr.bfReserved1 = 0; 
          hdr.bfReserved2 = 0;

          DWORD Junk;
          HANDLE FH = CreateFile(_T("\Destination.bmp"),GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,0,0);

          // Copy the BITMAPFILEHEADER into the .BMP file.  
          WriteFile(FH, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &Junk,  NULL);

          // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
          WriteFile(FH, (LPVOID) &(bmi.bmiHeader), sizeof(BITMAPINFOHEADER) + bmi.bmiHeader.biClrUsed * sizeof (RGBQUAD), 
             (LPDWORD) &Junk, ( NULL));

          // Copy the array of color indices into the .BMP file.  
          WriteFile(FH, (LPSTR) BitsRGB, (int) bmi.bmiHeader.biSizeImage, (LPDWORD) &Junk,NULL);
          CloseHandle(FH);
          ReleaseDC(DesktopHwnd, DevC);
          GlobalFree(BmpFileData);
       }

这篇关于使用Win Ce6的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆