回答你的问题,如何制作带有圆角的框架,你可以尝试像这样的方法,但是由于此处使用的
CreateRoundRectRgn没有反锯齿效果,所以你可能会对结果不满意。
type
TFrame1 = class(TFrame)
Edit1: TEdit;
Button1: TButton;
protected
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
end;
implementation
procedure TFrame1.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
Region: HRGN;
begin
inherited;
Region := CreateRoundRectRgn(0, 0, ClientWidth, ClientHeight, 30, 30);
SetWindowRgn(Handle, Region, True);
end;
更新:
由于GDI没有支持绘制带有抗锯齿的圆弧形状的功能,因此我在这里发布了一个纯填充的圆角矩形示例(仅使用GDI+实现,需要从这里
获取GDI+包装器)。
以下属性对其使用很重要:
- Color - 是形状的填充颜色(可以增强画笔颜色、梯度等)
- Radius - 是用于绘制圆角的圆的半径(以像素为单位)
- AlphaValue - 是呈现的圆角矩形的不透明度值(只是为了好玩^_^)
unit RoundShape;
interface
uses
SysUtils, Classes, Controls, Graphics, GdiPlus;
type
TCustomRoundShape = class(TGraphicControl)
private
FRadius: Integer;
FAlphaValue: Integer;
procedure SetRadius(Value: Integer);
procedure SetAlphaValue(Value: Integer);
protected
procedure Paint; override;
property Radius: Integer read FRadius write SetRadius default 10;
property AlphaValue: Integer read FAlphaValue write SetAlphaValue default 255;
public
constructor Create(AOwner: TComponent); override;
end;
TRoundShape = class(TCustomRoundShape)
public
property Canvas;
published
property Align;
property AlphaValue;
property Anchors;
property Color;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property Radius;
property ShowHint;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
constructor TCustomRoundShape.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 213;
Height := 104;
FRadius := 10;
FAlphaValue := 255;
end;
procedure TCustomRoundShape.SetRadius(Value: Integer);
begin
if FRadius <> Value then
begin
FRadius := Value;
Invalidate;
end;
end;
procedure TCustomRoundShape.SetAlphaValue(Value: Integer);
begin
if FAlphaValue <> Value then
begin
FAlphaValue := Value;
Invalidate;
end;
end;
procedure TCustomRoundShape.Paint;
var
GPPen: TGPPen;
GPColor: TGPColor;
GPGraphics: IGPGraphics;
GPSolidBrush: IGPSolidBrush;
GPGraphicsPath: IGPGraphicsPath;
begin
GPGraphicsPath := TGPGraphicsPath.Create;
GPGraphicsPath.Reset;
GPGraphicsPath.AddArc(0, 0, FRadius, FRadius, 180, 90);
GPGraphicsPath.AddArc(ClientWidth - FRadius - 1, 0, FRadius, FRadius, 270, 90);
GPGraphicsPath.AddArc(ClientWidth - FRadius - 1, ClientHeight - FRadius - 1,
FRadius, FRadius, 0, 90);
GPGraphicsPath.AddArc(0, ClientHeight - FRadius - 1, FRadius, FRadius, 90, 90);
GPGraphicsPath.CloseFigure;
GPColor.InitializeFromColorRef(ColorToRGB(Color));
GPColor.Alpha := FAlphaValue;
GPPen := TGPPen.Create(GPColor);
GPSolidBrush := TGPSolidBrush.Create(GPColor);
GPGraphics := TGPGraphics.Create(Canvas.Handle);
GPGraphics.SmoothingMode := SmoothingModeAntiAlias;
GPGraphics.FillPath(GPSolidBrush, GPGraphicsPath);
GPGraphics.DrawPath(GPPen, GPGraphicsPath);
end;
procedure Register;
begin
RegisterComponents('Stack Overflow', [TRoundShape]);
end;
end.
下面是使用 SmoothingModeAntiAlias
平滑模式的结果:
![图片描述](https://istack.dev59.com/eGHX4.webp)
虽然使用 GDI+ 来处理这样微小的事情会带来一些额外负担,但如果不启用反锯齿,使用纯 GDI 渲染得到的效果则比较丑陋。以下是同一个圆角矩形使用纯 GDI 渲染的示例:
![图片描述](https://istack.dev59.com/eji86.webp)
TFrame
不是正确的父类,我会使用普通的TCustomControl
。框架是用于在设计时组合可视控件而不是在编译时。但是编译自己的自定义控件是更可靠和弹性的解决方案。 - Warren PTLMDShapeControl
,它可以正常运行。但最终,我放弃了这些样式,因为我的客户讨厌这些非本地的样式,并希望它们消失。 - Warren PTShape
可以有圆角,但输出结果非常不稳定。我没有团队,所以我是那个调查需求、制作项目、编写代码和设计系统及数据库的人,但我还没有成为其中任何一项的专家... 还! :-) - NaNCreateRoundRectRgn
也是同样的情况。GDI不支持任何类型的抗锯齿,因此您无法获得平滑的圆角。如果我是您,我会像Warren建议的那样使用一个TCustomControl
,例如这样
。然后就只有两个问题,一个是此类控件的透明度,另一个是需要一个能够呈现平滑弧线的库来处理您的圆角。 - TLama