首页  编辑  

Autohotkey实现马赛克隐私屏幕保护

Tags: /计算机文档/脚本,批处理/   Date Created:
如果你想在某些特殊的情况下,不想让别人看清楚你的屏幕内容,但是又不想关闭屏幕或者锁定屏幕,也不想最小化所有桌面,那么可以考虑下面的AHK代码,它会抓取当前桌面,然后使用马赛克特效处理,然后用处理后的图片遮盖整个屏幕。该脚本可以支持多个显示器。
效果:

该脚本需要 gdi+ AHK 的支持。mosaic.ahk:
#include gdip.ahk

HBitmapFromScreen(X, Y, W, H) {
   HDC := DllCall("GetDC", "Ptr", 0, "UPtr")
   HBM := DllCall("CreateCompatibleBitmap", "Ptr", HDC, "Int", W, "Int", H, "UPtr")
   PDC := DllCall("CreateCompatibleDC", "Ptr", HDC, "UPtr")
   DllCall("SelectObject", "Ptr", PDC, "Ptr", HBM)
   DllCall("BitBlt", "Ptr", PDC, "Int", 0, "Int", 0, "Int", W, "Int", H
                   , "Ptr", HDC, "Int", X, "Int", Y, "UInt", 0x00CC0020)
   DllCall("DeleteDC", "Ptr", PDC)
   DllCall("ReleaseDC", "Ptr", 0, "Ptr", HDC)
   Return HBM
}

#!b::
	SysGet, xPrimary, 76  ; 获取主屏幕左上角X坐标
	SysGet, yPrimary, 77  ; 获取主屏幕左上角Y坐标
	SysGet, wScreen, 78   ; 获取屏幕宽度
	SysGet, hScreen, 79   ; 获取屏幕高度

	pToken := Gdip_Startup()
	mskSize = 20

	; 获取整个屏幕的截图
	hBitmap := HBitmapFromScreen(xPrimary, yPrimary, wScreen, hScreen)
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hBitmap)
	pBitmapOut := Gdip_CreateBitmap(wScreen, hScreen)
	Gdip_PixelateBitmap(pBitmap, pBitmapOut, mskSize)

	Gui, Msk:New, +LastFound +AlwaysOnTop +ToolWindow -Caption
	Gui, Msk:Show, x0 y0 w%wScreen% h%hScreen%, 马赛克
	hWnd := WinExist()
	hdc := GetDC(hWnd)

	; 绘制马赛克后的位图到窗口
	graphics := Gdip_GraphicsFromHDC(hdc)
	Gdip_DrawImage(graphics, pBitmapOut, 0, 0, wScreen, hScreen)
return

#IfWinActive, 马赛克 ahk_class AutoHotkeyGUI
	WheelUp::
		mskSize := (mskSize - 5 < 5) ? 5 : mskSize - 5
		Gdip_PixelateBitmap(pBitmap, pBitmapOut, mskSize)
		Gdip_DrawImage(graphics, pBitmapOut, 0, 0, wScreen, hScreen)
		return
	WheelDown::
		mskSize := mskSize + 5 > 50 ? 50 : mskSize + 5
		Gdip_PixelateBitmap(pBitmap, pBitmapOut, mskSize)
		Gdip_DrawImage(graphics, pBitmapOut, 0, 0, wScreen, hScreen)
	return
#If

MskGuiEscape:
MskGuiClose:
MskGuiCancel:
	Gdip_DeleteGraphics(graphics)
	Gdip_DisposeImage(pBitmapOut)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
	DeleteObject(hBitmap)
	Gui, Destroy
	return
mosaic.ahk (2.0KB)