Finestre Windows di Ogni Forma per Splash Screen

Riprendiamo a parlare di come creare delle finestre Windows di qualsiasi forma, in questo articolo si spiegano soltanto i principi base, ma se adattati è possibile creare finestre circolari, a forma di stella o altro per il nostro splash screen.

Nello scorso articolo abbiamo bucato una finestra usando la funzione SDK SetWindowRgn. Ora usiamo la stessa API per costruire finestre rotonde, a stella o di qualsiasi forma per gli splash screen.

Header CorsoVC.h per lo splash screen

Iniziamo dal file di intestazione CorsoVC.h, che include <windows.h>, gli identificativi del menu contestuale e la firma della callback principale.

// File di intestazione CorsoVC.h

#include <windows.h>

#define APP_NAME "Finestra per Splash"

#define IDM_EXIT  102
#define IDM_ABOUT 103

LRESULT CALLBACK AppWndProc(HWND, UINT, WPARAM, LPARAM);

Sorgente CorsoVC.cpp: finestra ellittica e testo GDI

Nel file CorsoVC.cpp creiamo una finestra senza barra del titolo, applichiamo una regione ellittica e disegniamo il testo bicolore tramite i path GDI.

// File sorgente CorsoVC.cpp

#define WIN32_LEAN_AND_MEAN

#include "CorsoVC.h"
#include <stdlib.h>
#include <math.h>

static HWND m_hWndMain;
static HINSTANCE m_hInst;
static HRGN m_hRgnSplash;

static int InitApplication(HINSTANCE);
static int InitInstance(HINSTANCE);
static void SetClipArea(HWND);
static void ContextMenu(HWND);
static void DrawSplash(HWND, LPCSTR);

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);
   }

   return TRUE;
}

LRESULT CALLBACK AppWndProc(HWND hWnd, UINT uMsg,
   WPARAM wParam, LPARAM lParam)
{
   long lVal;

   switch(uMsg)
   {
   case WM_COMMAND:
      switch(LOWORD(wParam))
      {
       case IDM_EXIT:
        SendMessage(m_hWndMain, WM_CLOSE, 0, 0L);
        break;
       case IDM_ABOUT:
        MessageBox(m_hWndMain,
          "Lo splash della RGPSoft di Rossi Giampaolo",
          "Splash Window", MB_OK|MB_ICONINFORMATION);
        break;
      }
      break;
      case WM_CONTEXTMENU:
          ContextMenu(m_hWndMain);
          break;
      case WM_SIZE:
          DefWindowProc(hWnd, uMsg, wParam, lParam);
          SetClipArea(hWnd);
          return 0;
       case WM_NCHITTEST:
          lVal = DefWindowProc(hWnd, uMsg, wParam, lParam);
          if (lVal == HTCLIENT)
          {
                if (GetAsyncKeyState(VK_LBUTTON))
                 return HTCAPTION;
         else
                 return lVal;
           }
           else
        return lVal;
       case WM_CLOSE:
        DestroyWindow(m_hWndMain);
        DeleteObject(m_hRgnSplash);
        break;
       case WM_PAINT:
        DrawSplash(m_hWndMain, "RGPSoft di Rossi Giampaolo");
        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 = "ROUND_CLASS";
   wc.lpszMenuName = NULL;

   return RegisterClass(&wc);
}

int InitInstance(HINSTANCE hInst)
{
   m_hInst = hInst;

   m_hWndMain = CreateWindowEx(0, "ROUND_CLASS", APP_NAME,
        WS_POPUP|WS_THICKFRAME,
        100, 100, 750, 500, NULL, NULL, hInst, NULL);

   if (!m_hWndMain)
        return FALSE;

   SetClipArea(m_hWndMain);

   ShowWindow(m_hWndMain, SW_SHOW);
   UpdateWindow(m_hWndMain);

   return TRUE;
}

Funzioni SetClipArea, ContextMenu e DrawSplash

Completiamo CorsoVC.cpp con tre funzioni dedicate. SetClipArea applica l’ellisse, ContextMenu apre il menu al clic destro e DrawSplash disegna il testo bicolore con i path GDI.

void SetClipArea(HWND hWnd)
{
   RECT rcArea;

   GetWindowRect(hWnd, &rcArea);
   rcArea.right -= rcArea.left;
   rcArea.bottom -= rcArea.top;
   rcArea.left = 0;
   rcArea.top = 0;

   InflateRect(&rcArea, -GetSystemMetrics(SM_CXSIZEFRAME),
       -GetSystemMetrics(SM_CYSIZEFRAME));

   m_hRgnSplash = CreateEllipticRgnIndirect(&rcArea);
   SetWindowRgn(hWnd, m_hRgnSplash, TRUE);
}

void ContextMenu(HWND hWnd)
{
   HMENU hMenu;
   POINT pt;

   GetCursorPos(&pt);
   hMenu = CreatePopupMenu();
   AppendMenu(hMenu, MF_STRING, IDM_ABOUT, "&Info");
   AppendMenu(hMenu, MF_STRING, IDM_EXIT, "&Esci");
   TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
   DestroyMenu(hMenu);
}

void DrawSplash(HWND hWnd, LPCSTR szText)
{
   HDC hdc;
   HFONT hFont, hOldFont;
   HBRUSH hBrush, hOldBrush;
   HPEN hPen, hOldPen;
   LOGFONT lf;
   SIZE sz;
   RECT rc;
   HRGN hRgn1, hRgn2;
   int x, y;

   GetClientRect(hWnd, &rc);
   hdc = GetDC(hWnd);
   int iMode = SetBkMode(hdc, TRANSPARENT);

   hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
   hBrush = CreateSolidBrush(RGB(255, 0, 0));

   memset(&lf, 0, sizeof(LOGFONT));
   lf.lfHeight = 48;
   lf.lfWeight = FW_BOLD;
   lstrcpy(lf.lfFaceName, "Arial");
   hFont = CreateFontIndirect(&lf);
   hOldFont = (HFONT)SelectObject(hdc, (HFONT)hFont);

   GetTextExtentPoint32(hdc, szText, lstrlen(szText), &sz);
   x = (rc.right - sz.cx) / 2;
   y = (rc.bottom - sz.cy) / 2;

   BeginPath(hdc);
   TextOut(hdc, x, y, szText, lstrlen(szText));
   EndPath(hdc);

   hRgn1 = CreateRectRgn(x, y, x + sz.cx, y + sz.cy / 2);
   SelectClipRgn(hdc, hRgn1);
   hOldPen = (HPEN)SelectObject(hdc, (HPEN)hPen);
   hOldBrush = (HBRUSH)SelectObject(hdc,
      (HBRUSH)GetStockObject(WHITE_BRUSH));
   StrokeAndFillPath(hdc);

   BeginPath(hdc);
   TextOut(hdc, x, y, szText, lstrlen(szText));
   EndPath(hdc);
   hRgn2 = CreateRectRgn(x, y + sz.cy / 2, x + sz.cx,
       y + sz.cy);
   SelectClipRgn(hdc, hRgn2);
   SelectObject(hdc, (HPEN)GetStockObject(WHITE_PEN));
   SelectObject(hdc, (HBRUSH)hBrush);
   StrokeAndFillPath(hdc);

   SelectClipRgn(hdc, (HRGN)NULL);
   SelectObject(hdc, (HPEN)hOldPen);
   SelectObject(hdc, (HFONT)hOldFont);
   SelectObject(hdc, (HBRUSH)hOldBrush);
   DeleteObject(hPen);
   DeleteObject(hBrush);
   DeleteObject(hFont);
   SetBkMode(hdc, iMode);
   ReleaseDC(hWnd, hdc);
}

Finestra WS_POPUP, trascinamento con WM_NCHITTEST e DWM

Creare uno splash screen rotondo richiede pochi passaggi mirati. Prima trasformi le coordinate della finestra rispetto alla client area ed elimini i bordi superflui.

Il programma usa lo stile WS_POPUP senza barra del titolo né barra dei menu. Aggiungiamo invece un menu contestuale sul clic destro per mostrare le informazioni e chiudere l’app.

Splash Screen Rotondo
Una finestra rotondeggiante

Come sposti una finestra priva di barra del titolo? Ecco i passaggi chiave usati nel codice:

  • Intercetta WM_NCHITTEST: quando il cursore tocca l’area client (HTCLIENT) e premi il tasto sinistro, restituisci HTCAPTION.
  • Simula la barra del titolo: Windows crede che stai trascinando la caption e sposta l’intera finestra ellittica.
  • Disegna testo vettoriale: BeginPath, EndPath e StrokeAndFillPath colorano metà superiore e inferiore del testo con due regioni diverse.

Note moderne per Windows 10 e Windows 11

Su Windows 10 e Windows 11, tieni presenti due accorgimenti pratici. In WM_PAINT conviene usare BeginPaint ed EndPaint invece di GetDC per validare l’area aggiornata. Inoltre, per bordi sfumati sotto DWM, usa WS_EX_LAYERED con UpdateLayeredWindow e un’immagine PNG trasparente.

Nel prossimo articolo della serie leggiamo i pixel dal display. Scopri subito come catturare lo schermo in una immagine bitmap.

Vuoi scoprire altri trucchi, controlli e guide di programmazione? Esplora tutti gli articoli nella categoria Windows.

Giampaolo Rossi
Giampaolo Rossi

Sviluppatore di software gestionale da oltre 30 anni.

Articoli: 327

RGPSoft© 2026 - Contenuti distribuiti con licenza CC BY 4.0