To fix the error and correctly use the TakeScreenshot function from your Utils class, there must be an instance of the Utils class to call the member function. Additionally, the inclusion of headers and namespace should be managed properly. Here's a revised version of your code:
Utils.h
#pragma once
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include <AclAPI.h>
#include <string>
#include <string>
#include "Sddl.h"
class Utils {
public:
void HideConsole();
void Autoload(const std::string& Name, const std::string& Path);
std::string FindPath();
LPCWSTR Path();
std::string FindDir();
bool TakeScreenshot(int x, int y, int width, int height, const char* filename, HWND hwnd);
};
Utils.cpp
Here is a minimal implementation of the TakeScreenshot method. You should replace this with the full implementation as needed.
#include "Utils.h"
#include <Windows.h>
bool Utils::TakeScreenshot(int x, int y, int width, int height, const char* filename, HWND hwnd) {
HDC hdcScreen = GetDC(NULL); // retrieve the handle to the device context (DC) for the screen
HDC hdcMemory = CreateCompatibleDC(hdcScreen); // create a compatible DC in memory
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, width, height); // create a bitmap compatible with the screen DC
SelectObject(hdcMemory, hBitmap);
BitBlt(hdcMemory, x, y, width, height, hdcScreen, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height; // negative to ensure top-down bitmap
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((width * bi.biBitCount + 31) / 32) * 4 * height;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char* lpbitmap = (char*)GlobalLock(hDIB);
GetDIBits(hdcScreen, hBitmap, 0, (UINT)height, lpbitmap, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return false;
}
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
ReleaseDC(NULL, hdcScreen);
DeleteDC(hdcMemory);
DeleteObject(hBitmap);
return true;
}
Main.cpp
#include "Utils.h"
int main() {
Utils utils;
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
char filename[] = "screenshot.bmp";
HWND hwnd = NULL; // capture entire screen
if (utils.TakeScreenshot(0, 0, screenWidth, screenHeight, filename, hwnd)) {
printf("Screenshot saved to %s\n", filename);
}
else {
printf("Failed to take screenshot\n");
}
return 0;
}
In this setup:
- The
Utils class is correctly defined with the TakeScreenshot method as a non-static member function.
- An instance of the
Utils class (utils) is created in main(), and the TakeScreenshot method is called on this instance.
const char* and const std::string& are used for constant character array and string parameters to avoid unnecessary copies and allow flexibility with string literals.