Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Created December 8, 2025 20:42
Show Gist options
  • Select an option

  • Save PlugFox/9e16afe8fcf8032525b8b83d04a8dfb6 to your computer and use it in GitHub Desktop.

Select an option

Save PlugFox/9e16afe8fcf8032525b8b83d04a8dfb6 to your computer and use it in GitHub Desktop.
Rust Windows Transparent Overlay
[package]
name = "transparent_overlay"
version = "0.1.1"
edition = "2021"
[dependencies]
windows = { version = "0.58", features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_UI_WindowsAndMessaging",
"Win32_System_Com"
] }
windows-core = "0.58"
#![windows_subsystem = "windows"]
use windows::{
core::{Result, w, Error},
Win32::{
Foundation::{HWND, LRESULT, WPARAM, LPARAM, COLORREF},
System::Com::{CoInitializeEx, COINIT_APARTMENTTHREADED},
UI::WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, GetMessageW, RegisterClassW, SetLayeredWindowAttributes,
ShowWindow, TranslateMessage, DispatchMessageW, PostQuitMessage, MSG, WNDCLASSW,
WS_EX_LAYERED, WS_EX_TRANSPARENT, WS_POPUP, LWA_COLORKEY, SW_SHOW, WM_DESTROY,
WM_CLOSE, WINDOW_EX_STYLE, WINDOW_STYLE,
},
},
};
fn main() -> Result<()> {
unsafe {
CoInitializeEx(None, COINIT_APARTMENTTHREADED).ok()?;
let class_name = w!("transparent_overlay_class");
let wnd_class = WNDCLASSW {
lpfnWndProc: Some(wnd_proc),
lpszClassName: class_name,
..Default::default()
};
RegisterClassW(&wnd_class);
let hwnd = CreateWindowExW(
WINDOW_EX_STYLE(WS_EX_LAYERED.0 | WS_EX_TRANSPARENT.0),
class_name,
w!("Overlay"),
WINDOW_STYLE(WS_POPUP.0),
100,
100,
500,
400,
None,
None,
None,
None,
);
let hwnd = hwnd.ok().expect("Failed to create window");
SetLayeredWindowAttributes(hwnd, COLORREF(0), 0, LWA_COLORKEY)
.ok()
.expect("Failed to set layered window attributes");
ShowWindow(hwnd, SW_SHOW);
let mut msg = MSG::default();
while GetMessageW(&mut msg, None, 0, 0).into() {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
Ok(())
}
extern "system" fn wnd_proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
match msg {
WM_CLOSE | WM_DESTROY => {
unsafe { PostQuitMessage(0); }
LRESULT(0)
}
_ => unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) },
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment