C++Builder
  Начало   Форум   Помощь Войти Регистрация  
Страниц: [1]   Вниз
  Печать  
Автор Тема: Print Screen  (Прочитано 16555 раз)
Varvar
Участник


Сказали спасибо: +0/-0
Offline

Сообщений: 10


Print Screen
« : 02 Апрель 2008, 12:38:28 »

Подскажите, как програмнмо реализовать функцию Alt+PrintScreen (изображение активного окна), и сохранить этот рисунок в файл?
Записан
Gedeon
Ы
Администратор


Сказали спасибо: +6/-0
Offline

Сообщений: 291


Re: Print Screen
« Ответ #1 : 17 Апрель 2008, 16:59:03 »

Недавно возился с близкой темой

Код
void __fastcall TForm1::GetPixelArrayOfWindow(int X1, int Y1, int X2, int Y2, vectorCOLORREF> &pixels)
{
   int sx= GetSystemMetrics(SM_CXSCREEN),sy = GetSystemMetrics(SM_CYSCREEN);
 
   BITMAPINFO bmi;
   ZeroMemory(&bmi, sizeof(BITMAPINFO));
 
   bmi.bmiHeader.biSize= sizeof(BITMAPINFOHEADER);
   bmi.bmiHeader.biWidth=sx;
   bmi.bmiHeader.biHeight=sy;
   bmi.bmiHeader.biPlanes=1;
   bmi.bmiHeader.biBitCount=32;
   bmi.bmiHeader.biCompression=BI_RGB;
   bmi.bmiHeader.biClrUsed=0;
   bmi.bmiHeader.biClrImportant=0;
   bmi.bmiHeader.biSizeImage = bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 4;
 
   HDC hDC = GetDC(GetDesktopWindow());
   HDC MyHDC = CreateCompatibleDC(hDC);
   HBITMAP hBMP = CreateCompatibleBitmap(hDC, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight);
   LOGBRUSH MyBrush;
   SelectObject(MyHDC, hBMP);
   MyBrush.lbStyle = BS_SOLID;
   MyBrush.lbColor = 0xFF0000;
   HBRUSH hBrush = CreateBrushIndirect(&MyBrush);
   RECT MyRect = {0, 0, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight};
   FillRect(MyHDC, &MyRect, hBrush);
   BitBlt(MyHDC, 0, 0, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, hDC, 0, 0, SRCCOPY);
 
   unsigned long *pBits = new unsigned long[bmi.bmiHeader.biSizeImage];
   GetDIBits(MyHDC,hBMP,0,bmi.bmiHeader.biHeight,pBits, &bmi,DIB_RGB_COLORS);
 
   long i,j;
   for (i=0; i (Y2-Y1); i++) // rows
    {  for (j=0; j (X2-X1); j++) // one row
        {  unsigned long  px = pBits[(i+Y1) * bmi.bmiHeader.biWidth + (j+X1)];
           unsigned char *cp = (unsigned char*)&px;
           int B,G,R;
           B = *(cp+0);
           G = *(cp+1);
           R = *(cp+2);
           pixels.push_back(RGB(R,G,B));
        }
    }
 
   DeleteDC(MyHDC);
   DeleteDC(hDC);
   DeleteObject(hBMP);
   delete[] pBits;
}
 

Код
void TForm1::CreateBMPFile(HWND hwnd, LPCSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
{
   HANDLE hf;                 // file handle
   BITMAPFILEHEADER hdr;       // bitmap file-header
   PBITMAPINFOHEADER pbih;     // bitmap info-header
   LPBYTE lpBits;              // memory pointer
   //DWORD dwTotal;              // total count of bytes
   DWORD cb;                   // incremental count of bytes
   BYTE *hp;                   // byte pointer
   DWORD dwTmp;
 
   pbih = (PBITMAPINFOHEADER) pbi;
   lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
 
   /*if (!lpBits)
   {
       errhandler("GlobalAlloc", hwnd);
   } */

 
   // Retrieve the color table (RGBQUAD array) and the bits
   // (array of palette indices) from the DIB.
   if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,
       DIB_RGB_COLORS))
   {
       ShowMessage("GetDIBits");
   }
 
   // Create the .BMP file.
   hf = CreateFile(pszFile,
                  GENERIC_READ | GENERIC_WRITE,
                  (DWORD) 0,
                   NULL,
                  CREATE_ALWAYS,
                  FILE_ATTRIBUTE_NORMAL,
                  (HANDLE) NULL);
   if (hf == INVALID_HANDLE_VALUE)
   {
       ShowMessage("CreateFile");
   }
   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))
   {
      ShowMessage("WriteFile");
   }
 
   // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
   if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
                 + pbih->biClrUsed * sizeof (RGBQUAD),
                 (LPDWORD) &dwTmp, ( NULL)) )
       ShowMessage("WriteFile");
 
   // 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))
          ShowMessage("WriteFile");
 
   // Close the .BMP file.
    if (!CloseHandle(hf))
          ShowMessage("CloseHandle");
 
   // Free memory.
   GlobalFree((HGLOBAL)lpBits);
}
 

Код
PBITMAPINFO TForm1::CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp) {
   BITMAP bmp;
 
   WORD    cClrBits;
 
   /* Retrieve the bitmap's color format, width, and height. */
 
   if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
       MessageBox(0,"GetObject","hwnd",0);
 
 
   /* Convert the color format to a count of bits. */
 
   cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
 
   if (cClrBits == 1)
       cClrBits = 1;
   else if (cClrBits 4)
 
       cClrBits = 4;
   else if (cClrBits 8)
       cClrBits = 8;
   else if (cClrBits 16)
       cClrBits = 16;
   else if (cClrBits 24)
       cClrBits = 24;
   else
       cClrBits = 32;
 
   /*
    * Allocate memory for the BITMAPINFO structure. (This structure
    * contains a BITMAPINFOHEADER structure and an array of RGBQUAD data
    * structures.)
    */

 
   if (cClrBits != 24)
        pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
 
                   sizeof(BITMAPINFOHEADER) +
                   sizeof(RGBQUAD) * (2^cClrBits));
 
   /*
    * There is no RGBQUAD array for the 24-bit-per-pixel format.
    */

 
   else
        pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
                   sizeof(BITMAPINFOHEADER));
 
 
 
   /* Initialize the fields in the BITMAPINFO structure. */
 
   pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
   pbmi->bmiHeader.biWidth = bmp.bmWidth;
 
   pbmi->bmiHeader.biHeight = bmp.bmHeight;
   pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
   pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
   if (cClrBits 24)
       pbmi->bmiHeader.biClrUsed = 2^cClrBits;
 
 
   /* If the bitmap is not compressed, set the BI_RGB flag. */
 
   pbmi->bmiHeader.biCompression = BI_RGB;
 
   /*
    * Compute the number of bytes in the array of color
    * indices and store the result in biSizeImage.
    */

 
 
   pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8
                                 * pbmi->bmiHeader.biHeight
                                 * cClrBits;
 
   /*
    * Set biClrImportant to 0, indicating that all of the
    * device colors are important.
    */

 
   pbmi->bmiHeader.biClrImportant = 0;
 
   return pbmi;
 
}
 

Код
void TForm1::SaveBitmap(HBITMAP bmp, LPCSTR file)
{
   HWND hwnd=Handle;
   PBITMAPINFO pbi=CreateBitmapInfoStruct(hwnd,bmp);
 
   HDC hdcImage=CreateCompatibleDC(NULL);
   CreateBMPFile(hwnd,file,pbi,bmp, hdcImage);
 
   DeleteDC(hdcImage);
 
LocalFree(pbi);
}
 

Спасибо за помощь AVC

Разберетесь?
Записан
oxotnik
Глобальный модератор


Сказали спасибо: +100/-15
Offline

Сообщений: 2425


« Ответ #2 : 17 Апрель 2008, 17:03:08 »

Чуток покороче нашел
Код
Как поместить вид рабочего стола в буфер
 
Напишите следующий код:
#include
void __fastcall TForm1::Button1Click(TObject *Sender)
{
unsigned DataHandle;
HPALETTE APalette;
Word Format;
Graphics::TBitmap *Bitmap=new Graphics::TBitmap;
HDC DC=GetDC(0);
try{
Bitmap->Height=Screen->Height;
Bitmap->Width=Screen->Width;
BitBlt(Bitmap->Canvas->Handle , 0, 0, Screen->Width,
Screen->Height,DC, 0, 0, SRCCOPY);
//Изображение экрана теперь в Bitmap, запишем его в бyфеp обмена
Bitmap->SaveToClipboardFormat(Format,DataHandle,APalette);
Clipboard()->SetAsHandle(Format,DataHandle);
}
__finally
{
delete Bitmap;
ReleaseDC(0,DC);
}
}
 

сам не проверял, но по идее должно работать
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  




....

Powered by SMF 1.1.21 | SMF © 2025, Simple Machines
Страница сгенерирована за 0.084 секунд. Запросов: 20.