Delphi VCL表单的边框是否可以进行圆角处理?

4
我希望VCL窗体的圆角可以像下图一样:rounded corners(目前只需要圆角,不需要阴影)。 我已经尝试了一些代码,但貌似没有任何变化...
unit UMainWindow;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TFMainWindow = class(TForm)
  procedure FormCreate(Sender: TObject);
  end;

var
  FMainWindow: TFMainWindow;

implementation

procedure TFMainWindow.FormCreate(Sender: TObject);
var
  rgn: HRGN;
begin
  rgn := CreateRoundRectRgn(0, 0,ClientWidth,ClientHeight,40,40);
  SetWindowRgn(Handle, rgn, True);
end;

end.

这是我的结果: 不是真正的圆角

我不知道为什么你的SetWindowRgn没有起作用。它返回0吗?OI中有任何非默认属性吗? - Sertac Akyuz
4
OnCreate 事件不是创建 HRGN 的最佳位置。应该重写虚拟方法 CreateWnd()CreateWindowHandle()。在 OnCreate 事件后,窗体的 HWND 可以重新创建。此外,如果窗体可调整大小,应使用 OnResize 事件为新尺寸重新创建 HRGN - Remy Lebeau
检查 API 调用是否存在错误非常重要。文档解释了它们如何表示成功和失败。 - David Heffernan
@Matthias,我按原样在10.2 VCL上尝试了你的代码,它运行良好。然而,在以前的版本中,我曾经遇到过一些问题,通过禁用一些StyleElemens优先级(可能是seClient),这些问题得以解决,但我不记得了。 - Reron
1个回答

0

使用以下方法(从下面提到的Win11Forms.pas复制)现在可以在Windows 11上实现:

uses
  Winapi.Dwmapi, VCL.Dialogs, System.SysUtils;

const
  DWMWCP_DEFAULT    = 0; // Let the system decide whether or not to round window corners (default)
  DWMWCP_DONOTROUND = 1; // Never round window corners
  DWMWCP_ROUND      = 2; // Round the corners if appropriate
  DWMWCP_ROUNDSMALL = 3; // Round the corners if appropriate, with a small radius

  DWMWA_WINDOW_CORNER_PREFERENCE = 33; // WINDOW_CORNER_PREFERENCE controls the policy that rounds top-level window corners

var
  CornerPreference: Cardinal; // set to 0..3 from DWMWCP consts above

Winapi.Dwmapi.DwmSetWindowAttribute(Handle, DWMWA_WINDOW_CORNER_PREFERENCE, @CornerPreference, sizeof(CornerPreference));

在 GitHub 上有两个项目可用来以两种不同的方式执行此操作。https://github.com/checkdigits/rounded_corners 提供一个函数,您可以通过传递一个句柄来使用它(它只是包装了上面的函数),而 https://github.com/marcocantu/DelphiSessions/tree/master/Win11_Delphi11/Win11Round 包含一个名为“Win11Forms”的单元,该单元添加了对 vcl.forms TForm 类的功能扩展,以设置默认值并允许覆盖默认值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接