winapi实现一个截屏程序:创建一个截图按钮,点击后等待2秒截图,然后通过保存文件对话框保存(编译环境:vs2022桌面应用程序)
时间: 2024-04-09 22:28:26 浏览: 117
您可以使用WinAPI来实现一个截屏程序。下面是一个简单的示例代码:
```cpp
#include <Windows.h>
// 回调函数,用于捕获屏幕截图
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
// 获取屏幕尺寸
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// 创建设备上下文
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, screenWidth, screenHeight);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
// 将屏幕内容复制到设备上下文中
BitBlt(hdcMem, 0, 0, screenWidth, screenHeight, hdcScreen, 0, 0, SRCCOPY);
// 保存截图到文件
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = "bmp";
if (GetSaveFileName(&ofn))
{
// 将截图保存为位图文件
HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFOHEADER bmpInfoHeader;
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biWidth = screenWidth;
bmpInfoHeader.biHeight = -screenHeight;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 24;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biSizeImage = 0;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
DWORD dwBytesWritten;
bmpFileHeader.bfType = 0x4D42; // 'BM'
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + screenWidth * screenHeight * 3;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
WriteFile(hFile, &bmpFileHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, &bmpInfoHeader, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, hBitmap, screenWidth * screenHeight * 3, &dwBytesWritten, NULL);
CloseHandle(hFile);
}
// 清理资源
SelectObject(hdcMem, hOldBitmap);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
// 截图完成,停止枚举窗口
return FALSE;
}
// 点击按钮触发的事件处理函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
if (LOWORD(wParam) == 1) // 按钮ID为1
{
// 延迟2秒后截图
Sleep(2000);
EnumWindows(EnumWindowsProc, 0);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = "ScreenCapture";
if (!RegisterClass(&wc))
return 1;
// 创建窗口
HWND hwnd = CreateWindow("ScreenCapture", "Screen Capture", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, hInstance, NULL);
if (!hwnd)
return 1;
// 创建按钮
CreateWindow("BUTTON", "Capture", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 10, 100, 30, hwnd, (HMENU)1, hInstance, NULL);
// 显示窗口
ShowWindow(hwnd, nCmdShow);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
```
这段代码创建了一个简单的窗口应用程序,其中包含一个名为"Capture"的按钮。当点击按钮时,程序会等待2秒后进行屏幕截图,并通过保存文件对话框保存截图为位图文件(.bmp)。请将该代码保存为一个.cpp文件,并在VS2022中进行编译。
阅读全文