electroduck

Drawing custom desktop background (Windows)

Jan 2nd, 2019
1,004
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Note: Loading image resources and processing keyboard input is
  2. // not required, that's just how I demoed it. After FindWindowA,
  3. // just make a loop where you draw whatever you want at any interval
  4. // - don't worry, Windows won't try to draw over you.
  5. // Does not work with Wallpaper Engine running.
  6.  
  7. // Edited 06-Jun-2019 for Windows 10 compatibility
  8.  
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <Windows.h>
  11. #include <assert.h>
  12. #include "resource.h"
  13.  
  14. BOOL CALLBACK WorkerEnumProc(HWND hWnd, LPARAM lParam);
  15.  
  16. DWORD __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
  17.     PSZ pszCmdLine, int nCmdShow) {
  18.     HANDLE hDC, hMemDC;
  19.     HGDIOBJ hOldBitmap;
  20.     HWND hWndDesktop;
  21.     HBITMAP hBitmapImage;
  22.     BITMAP bmImage;
  23.     SHORT sShiftLast = 0xFFFF, sShiftCur;
  24.     int iOldMode, iScreenW, iScreenH;
  25.  
  26.     hBitmapImage = LoadImageA(hInstance, MAKEINTRESOURCEA(IDB_RAW),
  27.         IMAGE_BITMAP, 0, 0, 0);
  28.     assert(hBitmapImage);
  29.     GetObject(hBitmapImage, sizeof(BITMAP), &bmImage);
  30.  
  31.     iScreenW = GetSystemMetrics(SM_CXSCREEN);
  32.     iScreenH = GetSystemMetrics(SM_CYSCREEN);
  33.  
  34.     hWndProgman = FindWindowA("Progman", NULL);
  35.     if (!hWndProgman) {
  36.         MessageBoxA(NULL, "Unable to find program manager window",
  37.             "Error", MB_ICONERROR);
  38.         ExitProcess(1);
  39.     }
  40.  
  41.     if ((GetVersion() & 0x0000FFFF) == 0x0206) {
  42.         // Windows 8.1+
  43.         OutputDebugStringA("Windows 8.1 or later detected\r\n");
  44.         SendMessage(hWndProgman, 0x052C, 0, 0);
  45.         EnumWindows(WorkerEnumProc, (LPARAM)&hWndDesktop);
  46.     } else {
  47.         // Any earlier version
  48.         OutputDebugStringA("Windows 8 or earlier detected\r\n");
  49.         hWndDesktop = hWndProgman;
  50.     }
  51.  
  52.     if (!hWndDesktop) {
  53.         MessageBoxA(NULL, "Unable to find desktop window",
  54.             "Error", MB_ICONERROR);
  55.         ExitProcess(1);
  56.     }
  57.  
  58.     // my loop. put whatever you want in yours.
  59.     // all you need is to GetDC(hWndDesktop), draw, and ReleaseDC(hWndDesktop, hDC) each time
  60.     while (!(GetAsyncKeyState(VK_END) & 0x8000)) {
  61.         sShiftCur = GetAsyncKeyState(VK_SHIFT) & 0x8000;
  62.         if (sShiftCur != sShiftLast) {
  63.             hBitmapImage = LoadImageA(hInstance,
  64.                 MAKEINTRESOURCEA(sShiftCur ? IDB_ACTIVATED : IDB_RAW),
  65.                 IMAGE_BITMAP, 0, 0, 0);
  66.             assert(hBitmapImage);
  67.             GetObject(hBitmapImage, sizeof(BITMAP), &bmImage);
  68.             sShiftLast = sShiftCur;
  69.  
  70.  
  71.             hDC = GetDC(hWndDesktop);
  72.             hMemDC = CreateCompatibleDC(hDC);
  73.             hOldBitmap = SelectObject(hMemDC, hBitmapImage);
  74.             iOldMode = SetStretchBltMode(hDC, HALFTONE);
  75.             StretchBlt(hDC, 0, 0, iScreenW, iScreenH, hMemDC, 0, 0,
  76.                 bmImage.bmWidth, bmImage.bmHeight, SRCCOPY);
  77.             SetStretchBltMode(hDC, iOldMode);
  78.             SelectObject(hMemDC, hOldBitmap);
  79.             ReleaseDC(hWndDesktop, hDC);
  80.         }
  81.  
  82.  
  83.         Sleep(50);
  84.     }
  85.  
  86.     return 0;
  87. }
  88.  
  89. BOOL CALLBACK WorkerEnumProc(HWND hWnd, LPARAM lParam) {
  90.     HWND hWndDefView;
  91.  
  92.     hWndDefView = FindWindowExA(hWnd, NULL, "SHELLDLL_DefView",
  93.         NULL);
  94.     if (hWndDefView) {
  95.         *(HWND*)lParam = FindWindowExA(NULL, hWnd, "WorkerW",
  96.             NULL);
  97.         return FALSE;
  98.     }
  99.  
  100.     return TRUE;
  101. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×