anchor.diff:    final paddingX = (parentWidth - childWidth) ~/ 2;
     final paddingY = (parentHeight - childHeight) ~/ 2;
   
  -  child.ref.left = parent.ref.left + paddingX;
  -  child.ref.top = parent.ref.top + paddingY;
  -  child.ref.right = child.ref.left + childWidth;
  -  child.ref.bottom = child.ref.top + childHeight;
  +  child
  +    ..ref.left = parent.ref.left + paddingX
  +    ..ref.top = parent.ref.top + paddingY
  +    ..ref.right = child.ref.left + childWidth
  +    ..ref.bottom = child.ref.top + childHeight;
  +}
  +
  +/// Paints title bar buttons. Returns the left most extent of the buttons within
  +/// the titleBarRect.
  +int paintButtons(int hwnd, int hdc, Pointer<PAINTSTRUCT> ps,
  +    Pointer<RECT> titleBarRect, int titleBarItemColor) {
  +  final titleBarHoverColor = RGB(130, 180, 160);
  +  final titleBarHoverBrush = CreateSolidBrush(titleBarHoverColor);
  +  final closeButtonColor = RGB(0xCC, 0x00, 0x00);
  +  final buttonIconBrush = CreateSolidBrush(titleBarItemColor);
  +  final buttonIconPen = CreatePen(PS_SOLID, 1, titleBarItemColor);
  +  final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  +
  +  final dpi = GetDpiForWindow(hwnd);
  +  final iconDimension = dpiScale(10, dpi);
  +
  +  // Paint minimize button
  +  final minimizeButtonRect = getMinimizeRect(hwnd, titleBarRect);
  +  final minimizeIconRect = calloc<RECT>()
  +    ..ref.right = iconDimension
  +    ..ref.bottom = 1;
  +
  +  try {
  +    if (hoveredButton == HoveredButton.minimize) {
  +      FillRect(hdc, minimizeButtonRect, titleBarHoverBrush);
  +    }
  +
  +    centerRectInParent(minimizeIconRect, minimizeButtonRect);
  +    FillRect(hdc, minimizeIconRect, buttonIconBrush);
  +  } finally {
  +    free(minimizeIconRect);
  +    free(minimizeButtonRect);
  +  }
  +
  +  // Paint maximize button
  +  final maximizeButtonRect = getMaximizeRect(hwnd, titleBarRect);
  +  final maximizeIconRect = calloc<RECT>()
  +    ..ref.right = iconDimension
  +    ..ref.bottom = iconDimension;
  +
  +  try {
  +    if (hoveredButton == HoveredButton.maximize) {
  +      FillRect(hdc, maximizeButtonRect, titleBarHoverBrush);
  +    }
  +
  +    centerRectInParent(maximizeIconRect, maximizeButtonRect);
  +    SelectObject(hdc, buttonIconPen);
  +    SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
  +    Rectangle(hdc, maximizeIconRect.ref.left, maximizeIconRect.ref.top,
  +        maximizeIconRect.ref.right, maximizeIconRect.ref.bottom);
  +  } finally {
  +    free(maximizeIconRect);
  +    free(maximizeButtonRect);
  +  }
  +
  +  // Paint close button
  +  final closeButtonRect = getCloseRect(hwnd, titleBarRect);
  +  final closeIconRect = calloc<RECT>()
  +    ..ref.right = iconDimension
  +    ..ref.bottom = iconDimension;
  +  try {
  +    int? customPen;
  +    if (hoveredButton == HoveredButton.close) {
  +      final fillBrush = CreateSolidBrush(closeButtonColor);
  +      FillRect(hdc, closeButtonRect, fillBrush);
  +      DeleteObject(fillBrush);
  +      customPen = CreatePen(PS_SOLID, 1, RGB(0xFF, 0xFF, 0xFF));
  +      SelectObject(hdc, customPen);
  +    }
  +
  +    centerRectInParent(closeIconRect, closeButtonRect);
  +    MoveToEx(hdc, closeIconRect.ref.left, closeIconRect.ref.top, nullptr);
  +    LineTo(hdc, closeIconRect.ref.right + 1, closeIconRect.ref.bottom + 1);
  +    MoveToEx(hdc, closeIconRect.ref.left, closeIconRect.ref.bottom, nullptr);
  +    LineTo(hdc, closeIconRect.ref.right + 1, closeIconRect.ref.top - 1);
  +    if (customPen != null) DeleteObject(customPen);
  +  } finally {
  +    free(closeIconRect);
  +    free(closeButtonRect);
  +  }
  +
  +  DeleteObject(titleBarHoverBrush);
  +  DeleteObject(buttonIconBrush);
  +  DeleteObject(buttonIconPen);
  +
  +  return minimizeButtonRect.ref.left;
  +}
  +
  +/// Draws the title bar caption within the given rectangle.
  +void drawWindowCaption(
  +    int hwnd, int hdc, Pointer<RECT> titleBarTextRect, int titleBarItemColor) {
  +  final logicalFont = calloc<LOGFONT>();
  +  final titleText = wsalloc(256);
  +  final drawThemeOptions = calloc<DTTOPTS>()
  +    ..ref.dwSize = sizeOf<DTTOPTS>()
  +    ..ref.dwFlags = DTT_TEXTCOLOR
  +    ..ref.crText = titleBarItemColor;
  +
  +  try {
  +    int? savedFont;
  +
  +    final hTheme = getWindowThemeHandle(hwnd);
  +    if (SUCCEEDED(GetThemeSysFont(hTheme, TMT_CAPTIONFONT, logicalFont))) {
  +      final themeFont = CreateFontIndirect(logicalFont);
  +      savedFont = SelectObject(hdc, themeFont);
  +    }
  +    CloseThemeData(hTheme);
  +
  +    GetWindowText(hwnd, titleText, 256);
  +    DrawThemeTextEx(
  +        hTheme,
  +        hdc,
  +        0,
  +        0,
  +        titleText,
  +        -1,
  +        DT_VCENTER | DT_SINGLELINE | DT_WORD_ELLIPSIS,
  +        titleBarTextRect,
  +        drawThemeOptions);
  +
  +    if (savedFont != null) SelectObject(hdc, savedFont);
  +  } finally {
  +    free(drawThemeOptions);
  +    free(titleText);
  +    free(logicalFont);
  +  }
  +}
  +
  +/// Paints the contents of the window in response to a WM_PAINT message.
  +void paintWindow(int hwnd) {
  +  final titleBarColor = RGB(150, 200, 180);
  +  final titleBarFocusedItemColor = RGB(33, 33, 33);
  +  final titleBarInactiveItemColor = RGB(127, 127, 127);
  +  final windowBackgroundColor = RGB(200, 250, 230);
  +  final shadowColor = RGB(100, 100, 100);
  +
  +  final hasFocus = GetFocus() != FALSE;
  +  final titleBarItemColor =
  +      hasFocus ? titleBarFocusedItemColor : titleBarInactiveItemColor;
  +
  +  final ps = calloc<PAINTSTRUCT>();
  +  final hdc = BeginPaint(hwnd, ps);
  +  const psRectOffset = 12;
  +  final psRect = Pointer<RECT>.fromAddress(ps.address + psRectOffset);
  +
  +  // Paint window client area background
  +  final windowBackgroundBrush = CreateSolidBrush(windowBackgroundColor);
  +  FillRect(hdc, psRect, windowBackgroundBrush);
  +  DeleteObject(windowBackgroundBrush);
  +
  +  // Paint title bar background
  +  final titleBarRect = getTitlebarRect(hwnd);
  +  final titleBarBrush = CreateSolidBrush(titleBarColor);
  +  FillRect(hdc, titleBarRect, titleBarBrush);
  +  DeleteObject(titleBarBrush);
  +
  +  // Paint buttons
  +  final leftButtonExtent =
  +      paintButtons(hwnd, hdc, ps, titleBarRect, titleBarItemColor);
  +
  +  // Draw window caption
  +  const textPadding = 10;
  +  final titleBarTextRect = cloneRect(titleBarRect)
  +    ..ref.left += textPadding
  +    ..ref.right = leftButtonExtent - textPadding;
  +  drawWindowCaption(hwnd, hdc, titleBarTextRect, titleBarItemColor);
  +  free(titleBarTextRect);
  +
  +  // Paint fake top shadow.
  +  final fakeTopShadowColor = hasFocus
  +      ? shadowColor
  +      : RGB(
  +          (GetRValue(titleBarColor) + GetRValue(shadowColor)) ~/ 2,
  +          (GetGValue(titleBarColor) + GetGValue(shadowColor)) ~/ 2,
  +          (GetBValue(titleBarColor) + GetBValue(shadowColor)) ~/ 2);
  +  final fakeTopShadowBrush = CreateSolidBrush(fakeTopShadowColor);
  +  final fakeTopShadowRect = getFakeShadowRect(hwnd);
  +  FillRect(hdc, fakeTopShadowRect, fakeTopShadowBrush);
  +  DeleteObject(fakeTopShadowBrush);
  +  free(fakeTopShadowRect);
  +
  +  EndPaint(hwnd, ps);
  +  free(titleBarRect);
  +  free(ps);
   }
   
   int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
  -  final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  -
     switch (msg) {
       case WM_NCCALCSIZE:
         // Handling this event allows us to extend the client (paintable) area
         // into the title bar region.
         //
  -      // Per https://docs.microsoft.com/en-us/windows/win32/dwm/customframe:
  +      // Per https://docs.microsoft.com/en-us/windows/win32/dwm/customframe :
         //
         // "To remove the standard window frame, you must handle the WM_NCCALCSIZE
         // message, specifically when its wParam value is TRUE and the return
         // value is 0. By doing so, your application uses the entire window region
         // as the client area, removing the standard frame."
  -      if (wParam == 0) return DefWindowProc(hwnd, msg, wParam, lParam);
  +      if (wParam == FALSE) break;
  +
         final dpi = GetDpiForWindow(hwnd);
   
         final frameX = GetSystemMetricsForDpi(SM_CXFRAME, dpi);
  
positive.code:         }
  
          // Since we are drawing our own caption, this needs to be a custom test
          getTitlebarRect(hwnd, titlebarRect);
          final titlebarBottom = titlebarRect.ref.bottom;
          if (cursorPoint.ref.y < titlebarBottom) {
            return HTCAPTION;
          }
  
          return HTCLIENT;
        } finally {
          free(titlebarRect);
          free(cursorPoint);
        }
  
      case WM_PAINT:
        final hasFocus = GetFocus() != 0;
  
        final ps = calloc<PAINTSTRUCT>();
        final hdc = BeginPaint(hwnd, ps);
  
        final bgBrush = CreateSolidBrush(RGB(200, 250, 230));
        final psRect = Pointer<RECT>.fromAddress(ps.address + 12);
        FillRect(hdc, psRect, bgBrush);
        DeleteObject(bgBrush);
  
        final hThemeData = getWindowThemeDataHandle(hwnd);
        final titleBarColor = RGB(150, 200, 180);
        final titleBarBrush = CreateSolidBrush(titleBarColor);
        final titleBarHoverColor = RGB(130, 180, 160);
        final titleBarHoverBrush = CreateSolidBrush(titleBarHoverColor);
  
        final titleBarRect = calloc<RECT>();
        getTitlebarRect(hwnd, titleBarRect);
        FillRect(hdc, titleBarRect, titleBarBrush);
        DeleteObject(titleBarBrush);
  
        final titleBarItemColor = hasFocus ? RGB(33, 33, 33) : RGB(127, 127, 127);
        final buttonIconBrush = CreateSolidBrush(titleBarItemColor);
        final buttonIconPen = CreatePen(PS_SOLID, 1, titleBarItemColor);
  
        final dpi = GetDpiForWindow(hwnd);
        final iconDimension = dpiScale(10, dpi);
  
        // Minimize button
        final minimizeButtonRect = getMinimizeRect(hwnd, titleBarRect);
        if (hoveredButton == CustomTitleBarHoveredButton.minimize) {
          FillRect(hdc, minimizeButtonRect, titleBarHoverBrush);
        }
  
        final minimizeIconRect = calloc<RECT>()
          ..ref.right = iconDimension
          ..ref.bottom = 1;
        centerRectInParent(minimizeIconRect, minimizeButtonRect);
        FillRect(hdc, minimizeIconRect, buttonIconBrush);
  
        free(minimizeIconRect);
        free(minimizeButtonRect);
  
        // Maximize button
        final maximizeButtonRect = getMaximizeRect(hwnd, titleBarRect);
  
        if (hoveredButton == CustomTitleBarHoveredButton.maximize) {
          FillRect(hdc, maximizeButtonRect, titleBarHoverBrush);
        }
        final maximizeIconRect = calloc<RECT>()
          ..ref.right = iconDimension
          ..ref.bottom = iconDimension;
        centerRectInParent(maximizeIconRect, maximizeButtonRect);
        SelectObject(hdc, buttonIconPen);
        SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
        Rectangle(hdc, maximizeIconRect.ref.left, maximizeIconRect.ref.top,
            maximizeIconRect.ref.right, maximizeIconRect.ref.bottom);
  
        free(maximizeIconRect);
        free(maximizeButtonRect);
  
        // Close button
        final closeButtonRect = getCloseRect(hwnd, titleBarRect);
        var customPen = 0;
        if (hoveredButton == CustomTitleBarHoveredButton.close) {
          final fillBrush = CreateSolidBrush(RGB(0xCC, 0, 0));
          FillRect(hdc, closeButtonRect, fillBrush);
          DeleteObject(fillBrush);
          customPen = CreatePen(PS_SOLID, 1, RGB(0xFF, 0xFF, 0xFF));
          SelectObject(hdc, customPen);
        }
        final closeIconRect = calloc<RECT>()
          ..ref.right = iconDimension
          ..ref.bottom = iconDimension;
        centerRectInParent(closeIconRect, closeButtonRect);
        MoveToEx(hdc, closeIconRect.ref.left, closeIconRect.ref.top, nullptr);
        LineTo(hdc, closeIconRect.ref.right + 1, closeIconRect.ref.bottom + 1);
        MoveToEx(hdc, closeIconRect.ref.left, closeIconRect.ref.bottom, nullptr);
        LineTo(hdc, closeIconRect.ref.right + 1, closeIconRect.ref.top - 1);
        if (customPen != 0) DeleteObject(customPen);
  
        free(closeIconRect);
        free(closeButtonRect);
  
        DeleteObject(titleBarHoverBrush);
        DeleteObject(buttonIconBrush);
        DeleteObject(buttonIconPen);
  
        // Draw window title
        final logicalFont = calloc<LOGFONT>();
        var oldFont = NULL;
        if (SUCCEEDED(
            GetThemeSysFont(hThemeData, TMT_CAPTIONFONT, logicalFont))) {
          final themeFont = CreateFontIndirect(logicalFont);
          oldFont = SelectObject(hdc, themeFont);
        }
        final titleText = wsalloc(256);
        GetWindowText(hwnd, titleText, 256);
        const textPadding = 10;
        final titleBarTextRect = cloneRect(titleBarRect)
          ..ref.left += textPadding
          ..ref.right = minimizeButtonRect.ref.left - textPadding;
        final drawThemeOptions = calloc<DTTOPTS>()
          ..ref.dwSize = sizeOf<DTTOPTS>()
          ..ref.dwFlags = DTT_TEXTCOLOR
          ..ref.crText = titleBarItemColor;
        DrawThemeTextEx(
            hThemeData,
            hdc,
            0,
            0,
            titleText,
            -1,
            DT_VCENTER | DT_SINGLELINE | DT_WORD_ELLIPSIS,
            titleBarTextRect,
            drawThemeOptions);
        if (oldFont != 0) SelectObject(hdc, oldFont);
        free(drawThemeOptions);
        free(titleBarTextRect);
        free(titleText);
        free(logicalFont);
        CloseThemeData(hThemeData);
  
        // Paint fake top shadow.
        final shadowColor = RGB(100, 100, 100);
        final fakeTopShadowColor = hasFocus
            ? shadowColor
            : RGB(
                (GetRValue(titleBarColor) + GetRValue(shadowColor)) ~/ 2,
                (GetGValue(titleBarColor) + GetGValue(shadowColor)) ~/ 2,
                (GetBValue(titleBarColor) + GetBValue(shadowColor)) ~/ 2);
        final fakeTopShadowBrush = CreateSolidBrush(fakeTopShadowColor);
        final fakeTopShadowRect = calloc<RECT>();
        getFakeShadowRect(hwnd, fakeTopShadowRect);
        FillRect(hdc, fakeTopShadowRect, fakeTopShadowBrush);
        DeleteObject(fakeTopShadowBrush);
        free(fakeTopShadowRect);
  
        EndPaint(hwnd, ps);
        break;
  
      case WM_NCMOUSEMOVE:
        final cursorPoint = calloc<POINT>();
        GetCursorPos(cursorPoint);
        ScreenToClient(hwnd, cursorPoint);
  
        final titleBarRect = calloc<RECT>();
        getTitlebarRect(hwnd, titleBarRect);
        final closeRect = getCloseRect(hwnd, titleBarRect);
        final maximizeRect = getMaximizeRect(hwnd, titleBarRect);
        final minimizeRect = getMinimizeRect(hwnd, titleBarRect);
  
        var newHoveredButton = CustomTitleBarHoveredButton.none;
        if (PtInRect(closeRect, cursorPoint.ref) != FALSE) {
          newHoveredButton = CustomTitleBarHoveredButton.close;
        } else if (PtInRect(maximizeRect, cursorPoint.ref) != FALSE) {
          newHoveredButton = CustomTitleBarHoveredButton.maximize;
        } else if (PtInRect(minimizeRect, cursorPoint.ref) != FALSE) {
          newHoveredButton = CustomTitleBarHoveredButton.minimize;
        }
        if (newHoveredButton != hoveredButton) {
          InvalidateRect(hwnd, closeRect, FALSE);
          InvalidateRect(hwnd, maximizeRect, FALSE);
          InvalidateRect(hwnd, minimizeRect, FALSE);
          SetWindowLongPtr(hwnd, GWLP_USERDATA, newHoveredButton);
        }
  
        free(minimizeRect);
        free(maximizeRect);
        free(closeRect);
        free(titleBarRect);
        free(cursorPoint);
        break;
  
      case WM_MOUSEMOVE:
        if (hoveredButton != CustomTitleBarHoveredButton.none) {
          final titleBarRect = calloc<RECT>();
          getTitlebarRect(hwnd, titleBarRect);
          InvalidateRect(hwnd, titleBarRect, FALSE);
          SetWindowLongPtr(hwnd, GWLP_USERDATA, CustomTitleBarHoveredButton.none);
        }
        break;
  
  
negative.code:       //
        // Ideally you also want to check that the mouse hasn't moved out or too much
        // between DOWN and UP messages.
        final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
        if (hoveredButton != HoveredButton.none) return 0;
  
        break;
  
      case WM_NCLBUTTONUP:
        final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  
        // Map button clicks to the right messages for the window
        switch (hoveredButton) {
          case HoveredButton.close:
            PostMessage(hwnd, WM_CLOSE, 0, 0);
            return 0;
          case HoveredButton.minimize:
            ShowWindow(hwnd, SW_MINIMIZE);
            return 0;
          case HoveredButton.maximize:
            final mode = isWindowMaximized(hwnd) ? SW_NORMAL : SW_MAXIMIZE;
            ShowWindow(hwnd, mode);
            return 0;
        }
        break;
  
      case WM_DESTROY:
  