Dopo aver imparato a gestire l’opacità nel tutorial sulle finestre trasparenti in Windows, vediamo un effetto ancora più spettacolare. Con l’SDK Win32 puoi infatti ritagliare veri e propri buchi dentro l’area della finestra per vedere lo schermo sottostante.
Questa tecnica si basa sulla funzione GDI SetWindowRgn, già introdotta anche nell’articolo sulle finestre rotonde di Windows:
int SetWindowRgn(HWND hWnd, HRGN hRgn, BOOL bRedraw);
A differenza del clipping su un singolo Device Context (DC), questa funzione applica un’area di ritaglio (HRGN) all’intera finestra. Tutto ciò che cade fuori dalla regione sparisce e lascia visibile e cliccabile ciò che sta sotto.
Header CorsoVC.h del progetto Win32
Creiamo ora un’applicazione C++ Win32 pura con un buco a forma di serratura al centro. Iniziamo dal file di intestazione CorsoVC.h con costanti e prototipo della window procedure.
// File di intestazione CorsoVC.h
#include <windows.h>
#define APP_NAME "Finestra con il buco"
#define IDM_EXIT 102
#define IDM_ABOUT 103
LRESULT CALLBACK AppWndProc(HWND, UINT, WPARAM, LPARAM);
Codice sorgente CorsoVC.cpp e gestione delle regioni
Nel file sorgente CorsoVC.cpp gestiamo l’avvio in WinMain, il ridimensionamento e la costruzione della regione bucata dentro SetClipArea.
// File sorgente CorsoVC.cpp
#define WIN32_LEAN_AND_MEAN
#include "CorsoVC.h"
static HWND m_hWndMain;
static HINSTANCE m_hInst;
static HRGN m_hRgnView;
static RECT m_rcHole;
static int InitApplication(HINSTANCE);
static int InitInstance(HINSTANCE);
static int SetClipArea(HWND);
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPreInst,
LPSTR lpszCmdLine, int iShowCmd)
{
MSG msg;
if(!InitApplication(hInst))
return FALSE;
if (!InitInstance(hInst))
return FALSE;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (m_hRgnView)
DeleteObject(m_hRgnView);
return TRUE;
}
LRESULT CALLBACK AppWndProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_EXIT:
SendMessage(m_hWndMain, WM_CLOSE, 0, 0L);
break;
}
break;
case WM_SIZE:
case WM_MOVE:
DefWindowProc(hWnd, uMsg, wParam, lParam);
SetClipArea(hWnd);
return 0;
case WM_CLOSE:
DestroyWindow(m_hWndMain);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
DrawEdge(ps.hdc, &m_rcHole, EDGE_SUNKEN, BF_RECT);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int InitApplication(HINSTANCE hInst)
{
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)AppWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wc.lpszClassName = "HOLE_CLASS";
wc.lpszMenuName = NULL;
return RegisterClass(&wc);
}
int InitInstance(HINSTANCE hInst)
{
m_hInst = hInst;
m_hWndMain = CreateWindowEx(0, "HOLE_CLASS", APP_NAME,
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hInst, NULL);
if (!m_hWndMain)
return FALSE;
SetClipArea(m_hWndMain);
ShowWindow(m_hWndMain, SW_SHOW);
UpdateWindow(m_hWndMain);
return TRUE;
}
int SetClipArea(HWND hWnd)
{
RECT rcHole, rcArea, rcCirc;
POINT pt[3];
HRGN hRgnHole, hRgnWnd, hRgnCirc, hRgnTriangle;
POINT ptOffset;
GetWindowRect(hWnd, &rcArea);
rcArea.right -= rcArea.left;
rcArea.bottom -= rcArea.top;
rcArea.left = 0;
rcArea.top = 0;
CopyRect(&rcHole, &rcArea);
rcHole.top += GetSystemMetrics(SM_CYCAPTION);
ptOffset.x = GetSystemMetrics(SM_CXSIZEFRAME) +
GetSystemMetrics(SM_CXEDGE);
ptOffset.y = GetSystemMetrics(SM_CYSIZEFRAME) +
GetSystemMetrics(SM_CYEDGE);
InflateRect(&rcHole, ptOffset.x, ptOffset.y);
InflateRect(&rcHole, -rcArea.right / 6, -rcArea.bottom / 8);
m_hRgnView = CreateRectRgn(0, 0, 10, 10);
hRgnWnd = CreateRectRgnIndirect(&rcArea);
hRgnHole = CreateRectRgnIndirect(&rcHole);
CopyRect(&rcCirc, &rcHole);
InflateRect(&rcCirc, -
(rcHole.right - rcHole.left) / 4, 0);
rcCirc.bottom = rcHole.top +
(rcHole.bottom - rcHole.top) / 3;
hRgnCirc = CreateEllipticRgnIndirect(&rcCirc);
pt[0].x = rcCirc.left +
(rcHole.right - rcHole.left) / 4;
pt[0].y = rcCirc.top +
(rcCirc.bottom - rcCirc.top) / 2;
pt[1].x = rcHole.right;
pt[1].y = rcHole.bottom;
pt[2].x = rcHole.left;
pt[2].y = rcHole.bottom;
hRgnTriangle = CreatePolygonRgn((LPPOINT)&pt,
3, ALTERNATE);
CombineRgn(hRgnHole, hRgnCirc, hRgnTriangle, RGN_OR);
CombineRgn(m_hRgnView, hRgnWnd, hRgnHole, RGN_DIFF);
DeleteObject(hRgnHole);
DeleteObject(hRgnCirc);
DeleteObject(hRgnTriangle);
DeleteObject(hRgnWnd);
SetWindowRgn(hWnd, m_hRgnView, TRUE);
return TRUE;
}
Come funziona la combinazione delle regioni GDI
La funzione SetClipArea costruisce il profilo della serratura combinando tre forme geometriche elementari:
CreateRectRgnIndirect: crea la regione rettangolare dell’intera finestra.CreateEllipticRgnIndirect: genera la parte superiore circolare del buco.CreatePolygonRgn: disegna il triangolo inferiore della serratura con tre vertici.CombineRgnconRGN_OR: unisce cerchio e triangolo in una sola forma.CombineRgnconRGN_DIFF: sottrae la serratura dal rettangolo della finestra.
Infine chiamiamo SetWindowRgn(hWnd, m_hRgnView, TRUE) per applicare il ritaglio e ridisegnare subito la finestra. Il risultato mostra lo sfondo del desktop proprio attraverso il foro centrale.

Note moderne: DWM e DPI awareness su Windows 10 e 11
Su Windows 10 e Windows 11, il compositor DWM supporta ancora SetWindowRgn. Tuttavia disattiva gli angoli arrotondati e l’ombra predefinita sui bordi ritagliati. Inoltre, dopo una chiamata riuscita, Windows diventa proprietario dell’HRGN e non devi più chiamare DeleteObject.
Se compili su monitor HiDPI, usa GetSystemMetricsForDpi al posto di GetSystemMetrics per calcolare correttamente bordi e barra del titolo. Nel prossimo episodio della serie applicheremo la stessa tecnica per creare finestre di ogni forma per splash screen.
Vuoi scoprire altri trucchi, controlli e guide di programmazione? Esplora tutti gli articoli nella categoria Windows.


