Windows API是什么?
Windows API(Application Programming Interface)是一组函数、数据结构、常量和类,用于在Microsoft Windows操作系统上开发软件。它提供了许多功能,包括图形用户界面(GUI)、文件和输入/输出操作、网络通信、多线程处理、注册表操作等。
通过调用Windows API,开发人员可以与操作系统进行交互,并使用其提供的各种功能来创建各种类型的应用程序。开发人员可以使用不同的编程语言,如C++、C#、VB.NET等,来调用Windows API。
Windows API 可以用于许多不同类型的应用程序开发,包括桌面应用程序、控制台应用程序、Windows服务、设备驱动程序等。通过使用Windows API,开发人员可以利用操作系统的强大功能,并实现各种复杂的操作和交互。
——chatGPT
怎么做?
个人认为最简单的方法是先使用函数从屏幕上拷贝像素到内存位图中,然后通过自定义的函数保存位图为文件。
代码呢?
#include <windows.h>
#include <bits/stdc++.h>
bool SaveBitmapToFile(HBITMAP hBitmap, const char* fileName);
int main()
{
HDC hScreenDC = GetDC(NULL);
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, screenWidth, screenHeight);
SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, screenWidth, screenHeight, hScreenDC, 0, 0, SRCCOPY);
if (SaveBitmapToFile(hBitmap, "screenshot.bmp"))
std::cout << "截图已保存为screenshot.bmp" << std::endl;
else
std::cout << "保存截图失败" << std::endl;
DeleteDC(hMemoryDC);
ReleaseDC(NULL, hScreenDC);
DeleteObject(hBitmap);
return 0;
}
bool SaveBitmapToFile(HBITMAP hBitmap, const char* fileName)
{
BITMAP bmp;
HANDLE file;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
DWORD dwBytesWritten;
GetObject(hBitmap, sizeof(BITMAP), &bmp);
fileHeader.bfType = 0x4D42;
fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bmp.bmWidthBytes * bmp.bmHeight;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
infoHeader.biSize = sizeof(BITMAPINFOHEADER);
infoHeader.biWidth = bmp.bmWidth;
infoHeader.biHeight = bmp.bmHeight;
infoHeader.biPlanes = 1;
infoHeader.biBitCount = bmp.bmBitsPixel;
infoHeader.biCompression = BI_RGB;
infoHeader.biSizeImage = 0;
infoHeader.biXPelsPerMeter = 0;
infoHeader.biYPelsPerMeter = 0;
infoHeader.biClrUsed = 0;
infoHeader.biClrImportant = 0;
file = CreateFileA(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
return false;
WriteFile(file, &fileHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(file, &infoHeader, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
LPVOID lpBits = malloc(bmp.bmWidthBytes * bmp.bmHeight);
GetBitmapBits(hBitmap, bmp.bmWidthBytes * bmp.bmHeight, lpBits);
int bytesPerRow = bmp.bmWidthBytes;
int height = bmp.bmHeight;
BYTE* pPixels = (BYTE*)lpBits;
BYTE* pRowBuffer = new BYTE[bytesPerRow];
for (int y = 0; y < height / 2; y++)
{
memcpy(pRowBuffer, pPixels + y * bytesPerRow, bytesPerRow);
memcpy(pPixels + y * bytesPerRow, pPixels + (height - y - 1) * bytesPerRow, bytesPerRow);
memcpy(pPixels + (height - y - 1) * bytesPerRow, pRowBuffer, bytesPerRow);
}
delete[] pRowBuffer;
WriteFile(file, lpBits, bmp.bmWidthBytes * bmp.bmHeight, &dwBytesWritten, NULL);
free(lpBits);
CloseHandle(file);
return true;
}
好啦,这期内容结束啦,再见!感谢csdn对我的支持。