Author Topic: DWM Blur for Mica/Acrylic  (Read 170 times)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 2004
    • zapsolution
DWM Blur for Mica/Acrylic
« on: October 30, 2025, 09:26:25 pm »
A compact demo of real DWM blur with an image overlay, gradient tint, and DPI-aware text rendering.
All packed in a stand-alone 24 KB /MT binary linked with TClib.lib.

Main Highlights
  • True DWM blur toggle (Right-Click) with instant switch — no transition flash.
  • Optional image overlay (Middle-Click), scaled and clipped to cover the client while preserving aspect.
  • Mouse-wheel–driven alpha control with speed-sensitive step (base 8 → up to 64).
  • DPI-aware (Per-Monitor-V2) rendering, sharp at any scaling.
  • Seamless maximize/restore via off-viewport reposition — no white flash or DWM fade.
  • Off-screen GDI+ compositing (double-buffered).
  • Two-color linear gradient overlay via GradientPaintDC, variable opacity / angle.
  • Private font collection with dynamic memory allocation (use-and-forget).
  • Text orientation: horizontal, bottom-up, or top-down, perfectly centered and clipped.
  • Black-theme caption bar with adaptive text color.
  • Custom small rounded corners replacing Windows 11’s exaggerated defaults.
  • Compatible with Windows 10 & 11 (Mica / Acrylic supported).

Controls
Mouse Right-Click Toggle DWM blur
Mouse Middle-Click Toggle overlay image
Mouse Wheel Adjust overlay alpha (speed-based)


Technical Notes
  • Pure Win32 / GDI+ C++ (Visual Studio 2022).
  • Static link /MT with TClib.lib → stand-alone 24 KB executable.
  • Direct-linked DPI APIs (SetThreadDpiAwarenessContext, GetDpiForWindow, etc.).
  • No external runtime or dependencies.
  • GDI+ text pipeline: DrawStringFormatedEx + BlurTextPlus.
  • Gradient painting handled by GradientPaintDC (two-color ARGB, any angle).
  • Caption bar color auto-adapts to Windows theme.
  • Custom region defines small uniform corner radius for balanced frame geometry.
  • Works seamlessly with DWM blur, Mica, or Acrylic on Windows 11.





Developer Notes

Project goal
To recreate the original Vista-style blur aesthetic with a modern, DPI-aware GDI+ implementation, packaged in a self-contained 24 KB binary (no runtime dependencies).
The design emphasizes simplicity, instant response, and pixel-perfect rendering at any scale.


Rendering architecture
  • All drawing is performed on an off-screen buffer created via GDI+ (Graphics object) to guarantee flicker-free updates.
  • Final presentation uses BitBlt to the window DC in WM_PAINT.
  • WM_ERASEBKGND always returns 1 to avoid background flashes.
  • Composition order:
    • DWM blur background (if enabled)
    • Background image (aspect-preserving cover fit, clipped to client)
    • Gradient overlay via GradientPaintDC (two-color linear fill with alpha)
    • Text and caption rendering (horizontal or rotated)


Image handling
  • Loaded once at WM_CREATE using GDI+ (GdipCreateBitmapFromFile).
  • Auto-fit logic computes the largest uniform scale to cover the client area while preserving aspect ratio.
  • Clipping handled with IntersectClipRect.
  • The image alpha channel is applied dynamically with GdipApplyImageAlpha(img, imgAlpha).


Alpha control
  • WM_MOUSEWHEEL adjusts imgAlpha with a base step of 8, automatically scaled for faster wheel spins (up to 64).
  • Alpha change triggers a re-composite via InvalidateRect (no erase).


Gradient overlay
  • Implemented by GradientPaintDC.
  • Simple two-color linear gradient (ARGB start → end), angle-based, with full opacity control.
  • Blended using SourceOver compositing on top of the image background.


Text engine
  • Uses custom DrawStringFormatedEx and BlurTextPlus for precise GDI+ text layout.
  • Supports three orientations: horizontal, bottom-up, and top-down.
  • Centered rotation around the text bounding box, with automatic clipping and no duplication artifacts.
  • Anti-aliased text rendering via TextRenderingHintAntiAlias.
  • Optional soft shadow or blur behind text.


Font management
  • Uses GDI+ Private Font Collection API (GdipPrivateAddFontFile / GdipPrivateAddMemoryFont).
  • Fonts are loaded dynamically, kept in memory only while needed ("use-and-forget").
  • Supports multiple concurrent fonts without leaks or persistent registry installs.


DPI awareness
  • Fully Per-Monitor-V2 compliant; no manifest required.
  • Enabled through direct calls to SetProcessDpiAwarenessContext and SetThreadDpiAwarenessContext.
  • Scale factor (dpi_scale) computed from GetDpiForSystem or GetDpiForWindow as fallback.


Window behavior
  • Maximize handling: the window is temporarily moved off-viewport (y = -5000) during SC_MAXIMIZE to suppress DWM transition flashes, then restored normally on SIZE_RESTORED.
  • Rounded corners: replaced the default Windows 11 exaggeration with a small, uniform radius using a custom region.
  • Caption bar: custom draw with adaptive color — black for dark theme, light for bright.
  • Theme changes: WM_SETTINGCHANGE updates caption and text colors automatically.


Performance & build
  • Compiled with Visual Studio 2022 /MT.
  • Linked with TClib.lib for minimal runtime footprint.
  • Final binary: approximately 24 KB stand-alone executable.
  • No heap allocations during painting (only during initialization).
  • Compatible with Windows 10 and 11, both light and dark modes.


  • The full VS2022 C/C++ 64-bit project is attached to this post.
« Last Edit: October 31, 2025, 01:56:04 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)