Para compilar usar:
Código: Seleccionar todo
g++ main.cpp -lgdiplus -luser32 -lgdi32 -mwindowsCódigo: Seleccionar todo
#include <windows.h>
#include <gdiplus.h>
#include <shlobj.h>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
std::string savePath = "C:\\ScreenshotsPro";
int hotkey = 'S'; // CTRL + SHIFT + S
// ---------------- CONFIG ----------------
void saveConfig() {
std::ofstream f("config.txt");
f << savePath << "\n" << hotkey;
}
void loadConfig() {
std::ifstream f("config.txt");
if (f.is_open()) {
std::getline(f, savePath);
f >> hotkey;
}
}
// ---------------- CREATE FOLDER ----------------
void ensureFolder() {
CreateDirectoryA(savePath.c_str(), NULL);
}
// ---------------- NOTIFICATION ----------------
void notify(const std::string& msg) {
NOTIFYICONDATAA nid = {0};
nid.cbSize = sizeof(nid);
nid.hWnd = GetConsoleWindow();
nid.uFlags = NIF_INFO;
strcpy_s(nid.szInfo, msg.c_str());
strcpy_s(nid.szInfoTitle, "Screenshot Pro");
nid.dwInfoFlags = NIIF_INFO;
Shell_NotifyIconA(NIM_ADD, &nid);
}
// ---------------- GET FILE NAME ----------------
std::string getFileName() {
time_t now = time(0);
return savePath + "\\cap_" + std::to_string(now) + ".png";
}
// ---------------- SAVE PNG (GDI+) ----------------
void savePNG(HBITMAP hBitmap, HDC hdc) {
Bitmap bmp(hBitmap, NULL);
CLSID clsid;
CLSIDFromString(L"{557CF406-1A04-11D3-9A73-0000F81EF32E}", &clsid);
std::wstring wpath(savePath.begin(), savePath.end());
wpath += L"\\temp.png";
bmp.Save(wpath.c_str(), &clsid, NULL);
std::string finalName = getFileName();
std::wstring wfinal(finalName.begin(), finalName.end());
MoveFileW(wpath.c_str(), wfinal.c_str());
notify("Captura guardada correctamente");
}
// ---------------- SCREENSHOT ----------------
void takeScreenshot() {
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
HDC screen = GetDC(NULL);
HDC mem = CreateCompatibleDC(screen);
HBITMAP bmp = CreateCompatibleBitmap(screen, x, y);
SelectObject(mem, bmp);
BitBlt(mem, 0, 0, x, y, screen, 0, 0, SRCCOPY);
savePNG(bmp, mem);
DeleteObject(bmp);
DeleteDC(mem);
ReleaseDC(NULL, screen);
}
// ---------------- HOTKEY ID ----------------
#define HOTKEY_ID 1
// ---------------- MAIN ----------------
int main() {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
loadConfig();
ensureFolder();
saveConfig();
std::cout << "Screenshot PRO activo...\n";
std::cout << "Hotkey: CTRL + SHIFT + " << (char)hotkey << "\n";
// CTRL + SHIFT + S (ejemplo)
RegisterHotKey(NULL, HOTKEY_ID, MOD_CONTROL | MOD_SHIFT, hotkey);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_HOTKEY) {
takeScreenshot();
}
}
GdiplusShutdown(gdiplusToken);
return 0;
}