Скриншот экрана + мб кодировка

Скриншот экрана + мб кодировка
This commit is contained in:
Vladimir Golubev 2023-03-12 22:39:35 +03:00
parent e5eb712517
commit a5e6d46015
2 changed files with 80 additions and 6 deletions

View File

@ -50,7 +50,7 @@
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings"> <ImportGroup Label="ExtensionSettings">

View File

@ -1,15 +1,83 @@
#include <Windows.h> #include <Windows.h>
#include <chrono> #include <chrono>
#include <Dwmapi.h> #include <Dwmapi.h>
#include <iostream>
#include <fstream>
#pragma comment(lib, "dwmapi.lib") #pragma comment(lib, "dwmapi.lib")
#pragma warning(disable : 4996) #pragma warning(disable : 4996)
const MARGINS margins = { -1 ,-1, -1, -1 }; const MARGINS margins = { -1 ,-1, -1, -1 };
const wchar_t g_szClassName[] = L"overlay"; const char g_szClassName[] = "overlay";
bool isVisible = true; bool isVisible = true;
void addWatermark(HDC hdc, HDC hdcDIB, BITMAPINFO bmi)
{
HFONT hFont;
LOGFONT lf = { 0 };
lf.lfHeight = -MulDiv(20, GetDeviceCaps(hdc, LOGPIXELSY), 72); // ðàçìåð øðèôòà
lf.lfWeight = FW_NORMAL; // æèðíîñòü øðèôòà
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_TT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
strcpy_s(lf.lfFaceName, "Arial");
hFont = CreateFontIndirect(&lf);
SelectObject(hdcDIB, hFont);
std::string watermark = "vos.team";
SetTextColor(hdcDIB, RGB(255, 0, 255, 128));
SetBkMode(hdcDIB, TRANSPARENT);
TextOut(hdcDIB, 10, 10, watermark.c_str(), watermark.length());
DeleteObject(hFont);
}
void captureScreen(std::string file_Name)
{
HDC hdcScreen;
HDC hdcDIB;
HBITMAP hbmDIB;
BITMAPINFO bmi;
LPVOID lpvBits;
HANDLE hFile;
DWORD dwBytesWritten;
hdcScreen = GetDC(NULL);
hdcDIB = CreateCompatibleDC(hdcScreen);
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = GetSystemMetrics(SM_CXSCREEN);
bmi.bmiHeader.biHeight = GetSystemMetrics(SM_CYSCREEN);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
hbmDIB = CreateDIBSection(hdcScreen, &bmi, DIB_RGB_COLORS, &lpvBits, NULL, 0);
SelectObject(hdcDIB, hbmDIB);
BitBlt(hdcDIB, 0, 0, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, hdcScreen, 0, 0, SRCCOPY);
// Äîáàâëåíèå âàòåðìàðêè íà êîíòåêñò
addWatermark(hdcScreen, hdcDIB, bmi);
hFile = CreateFile(("C:\\Users\\" + (std::string)std::getenv("USERNAME") + "\\Documents\\vos.team\\" + file_Name).c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
BITMAPFILEHEADER hdr = { };
hdr.bfType = 0x4d42; // Áóêâû 'B' è 'M'
hdr.bfOffBits = sizeof(hdr) + sizeof(BITMAPINFOHEADER);
hdr.bfSize = hdr.bfOffBits + bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 3;
WriteFile(hFile, &hdr, sizeof(hdr), &dwBytesWritten, NULL);
WriteFile(hFile, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, lpvBits, bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight * 3, &dwBytesWritten, NULL);
CloseHandle(hFile);
DeleteObject(hbmDIB);
DeleteDC(hdcDIB);
ReleaseDC(NULL, hdcScreen);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
std::string file_Name = "screenshot_" + std::to_string(time(NULL)) + ".bmp";
switch (msg) switch (msg)
{ {
case WM_HOTKEY: case WM_HOTKEY:
@ -22,6 +90,10 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
DestroyWindow(hwnd); DestroyWindow(hwnd);
} }
else if (wParam == 3)
{
captureScreen(file_Name);
}
break; break;
case WM_PAINT: case WM_PAINT:
{ {
@ -35,10 +107,10 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
auto currentTime = std::chrono::system_clock::now(); auto currentTime = std::chrono::system_clock::now();
auto currentTime_t = std::chrono::system_clock::to_time_t(currentTime); auto currentTime_t = std::chrono::system_clock::to_time_t(currentTime);
wchar_t timeStr[100]; char timeStr[100];
wcsftime(timeStr, 100, L"Time: %H:%M:%S", std::localtime(&currentTime_t)); strftime(timeStr, 100, "Time: %H:%M:%S", std::localtime(&currentTime_t));
TextOut(hdc, x, y, timeStr, wcslen(timeStr)); TextOut(hdc, x, y, timeStr, strlen(timeStr));
EndPaint(hwnd, &ps); EndPaint(hwnd, &ps);
} }
break; break;
@ -85,6 +157,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
RegisterHotKey(hwnd, 1, MOD_ALT, 0x5A); RegisterHotKey(hwnd, 1, MOD_ALT, 0x5A);
RegisterHotKey(hwnd, 2, MOD_ALT, 0x58); RegisterHotKey(hwnd, 2, MOD_ALT, 0x58);
RegisterHotKey(hwnd, 3, MOD_ALT, 0x50);
while (GetMessage(&Msg, NULL, 0, 0) > 0) while (GetMessage(&Msg, NULL, 0, 0) > 0)
{ {
@ -101,7 +174,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
UnregisterHotKey(hwnd, 1); UnregisterHotKey(hwnd, 1);
UnregisterHotKey(hwnd, 2); UnregisterHotKey(hwnd, 2);
UnregisterHotKey(hwnd, 3);
exit(0); exit(0);
return Msg.wParam; return Msg.wParam;
} }