You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不丢失原生Windows边框的情况下创建带平滑圆角的Delphi VCL窗体

Can I Create a Delphi VCL Form with Smooth Rounded Corners While Keeping the Native Windows Border?

First off, the short answer is yes—you absolutely can pull this off, and it can work alongside your DelphiUCL library setup if you plan carefully. Let’s break down the details, compatibility notes, and practical steps to make it happen:

Key Context for Your Goal

You want to retain three critical things:

  • The native Windows border (including all its default interactions: drag-to-resize, minimize/maximize/close buttons, system menu, etc.)
  • Smooth rounded corners on the form
  • DelphiUCL’s UWP-style appearance without losing the bsResizable behavior

Implementation Options by Windows Version

The approach shifts a bit based on which Windows version you’re targeting, since newer OSes have built-in API support for window corners.

Windows 11 added native rounded corner support via the DWM (Desktop Window Manager) API. This is the cleanest way to add rounded corners while preserving every native border behavior.

Code Example

Add this to your form’s OnCreate event (include Winapi.DwmApi in your uses clause):

uses Winapi.Windows, Winapi.DwmApi, Vcl.Forms;

procedure TMainForm.FormCreate(Sender: TObject);
var
  CornerPref: Integer;
begin
  // Pick your corner style:
  // DWMWCP_ROUND = Standard rounded corners
  // DWMWCP_ROUNDSMALL = Smaller rounded corners
  // DWMWCP_DONOTROUND = Disable corners (default)
  CornerPref := DWMWCP_ROUND;
  
  // Apply the setting to your form
  if DwmSetWindowAttribute(Handle, DWMWA_WINDOW_CORNER_PREFERENCE, @CornerPref, SizeOf(CornerPref)) <> S_OK then
    ShowMessage('Failed to set window corners (requires Windows 11+)');
end;

This works flawlessly with bsResizable forms—resizing, dragging, and all native behaviors stay intact, and the corners automatically adjust as the window changes size.

2. Windows 10 and Earlier

For older Windows versions, you’ll need to use the SetWindowRgn API to manually define a rounded region for your form. The catch here is you’ll need to update the region every time the window resizes (since the rounded corners need to scale with the window).

Code Example

Add these methods to your form:

uses Winapi.Windows, Vcl.Forms;

procedure TMainForm.UpdateWindowCorners;
var
  Rgn: HRGN;
  FormRect: TRect;
begin
  GetWindowRect(Handle, FormRect);
  OffsetRect(FormRect, -FormRect.Left, -FormRect.Top);
  
  // Create a rounded region (adjust the 20 value to control corner radius)
  Rgn := CreateRoundRectRgn(FormRect.Left, FormRect.Top, FormRect.Right, FormRect.Bottom, 20, 20);
  
  // Apply the region to the window
  SetWindowRgn(Handle, Rgn, True);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  UpdateWindowCorners;
end;

procedure TMainForm.FormResize(Sender: TObject);
begin
  UpdateWindowCorners;
end;

Note: This works with bsResizable forms, but you might notice minor visual glitches during resizing (since the region updates after the resize event fires). For smoother results, you could hook into the window’s resize messages directly, but that adds a bit more complexity.

Compatibility with DelphiUCL

Since you’re using DelphiUCL to get a UWP-style look while keeping bsResizable behavior, here’s what to check:

  • If DelphiUCL only modifies the client area (e.g., controls, colors), the rounded corner methods above will work perfectly alongside it—just apply the corner settings after initializing DelphiUCL.
  • If DelphiUCL replaces the non-client area (title bar, border) with custom drawing, you’ll lose native border behavior if you want to keep its UWP style. In this case, you have two options:
    • Disable DelphiUCL’s non-client area customization to retain the native border, then apply the rounded corner API.
    • Use DelphiUCL’s built-in options (if available) to add rounded corners to its custom-drawn border, while ensuring it maintains bsResizable behavior.

Final Notes

  • Always test across your target Windows versions to ensure consistent behavior.
  • For Windows 11, the rounded corner API respects system settings (e.g., if the user has disabled rounded corners system-wide, your form will follow that preference).

内容的提问来源于stack exchange,提问作者Roberto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 12:59:08