Created
September 10, 2018 10:34
-
-
Save FRex/69c9a38afed8e1fa9ff849d9ef43b2aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "native.hpp" | |
#define NOMINMAX | |
#include <windows.h> | |
#include <vector> | |
#include <cstring> | |
#include <algorithm> | |
#include <SFML/System/String.hpp> | |
sf::Image takeDesktopScreenshot() | |
{ | |
const int w = GetSystemMetrics(SM_CXSCREEN); | |
const int h = GetSystemMetrics(SM_CYSCREEN); | |
HWND dwin = GetDesktopWindow(); | |
HDC ddc = GetDC(dwin); | |
HDC dc2 = CreateCompatibleDC(ddc); | |
HBITMAP bitmap = CreateCompatibleBitmap(ddc, w, h); | |
SelectObject(dc2, bitmap); | |
BitBlt(dc2, 0, 0, w, h, ddc, 0, 0, SRCCOPY | CAPTUREBLT); | |
BITMAPINFO bitmapinfo; | |
std::memset(&bitmapinfo, 0x0, sizeof(BITMAPINFO)); | |
bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
bitmapinfo.bmiHeader.biWidth = w; | |
bitmapinfo.bmiHeader.biHeight = -h; | |
bitmapinfo.bmiHeader.biPlanes = 1; | |
bitmapinfo.bmiHeader.biBitCount = 32; | |
bitmapinfo.bmiHeader.biCompression = BI_RGB; | |
std::vector<sf::Uint8> buff(w * h * 4); | |
std::memset(buff.data(), 0xff, buff.size()); | |
GetDIBits(dc2, bitmap, 0, h, buff.data(), &bitmapinfo, DIB_RGB_COLORS); | |
for(int i = 0; i < buff.size() / 4; ++i) | |
std::swap(buff[i * 4 + 0], buff[i * 4 + 2]); | |
DeleteObject(bitmap); | |
DeleteDC(dc2); | |
ReleaseDC(dwin, ddc); | |
sf::Image ret; | |
ret.create(w, h, buff.data()); | |
return ret; | |
} | |
void setDesktopWallpaper(const char * filename) | |
{ | |
const auto need = GetCurrentDirectoryW(0, NULL) - 1; | |
std::vector<WCHAR> buff(need + 100); | |
GetCurrentDirectoryW(buff.size(), buff.data()); | |
sf::String cwd = sf::String::fromUtf16(buff.data(), buff.data() + need); | |
cwd += '\\'; | |
cwd += filename; | |
SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0u, (void*)cwd.toWideString().c_str(), 0u); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment