重载 CreateParams 方法[1]: 从一个例子开始(取消窗口最大化、最小化按钮的三种方法)...


方法1: 使用 TForm 的 BorderIcons 属性
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  BorderIcons := BorderIcons - [biMinimize, biMaximize];
end;

end.

 
 
 
 
 

 

 

  

方法2: 使用 SetWindowLong 函数
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  ws: Integer;
begin
  ws := GetWindowLong(Handle, GWL_STYLE);
  ws := ws xor WS_MINIMIZEBOX xor WS_MAXIMIZEBOX;
  SetWindowLong(Handle, GWL_STYLE, ws);
end;

end.

 
 
 
 
 

 

 

  

方法3: 重载 CreateParams 方法
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure CreateParams(var Params: TCreateParams); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited; //inherited CreateParams(Params);
  Params.Style := Params.Style xor WS_MINIMIZEBOX xor WS_MAXIMIZEBOX;
end;

end.

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/319502

为了封装一个通用的方法,让你可以在任何时候轻松地给Windows窗体中的任意控件应用无锯齿圆角效果,你可以创建一个自定义的控件或者包装类。这里提供一个简单的自定义控件示例,以`Control`为基础,增加无锯齿圆角功能: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class SmoothRoundedControl : Control { private const float CornersRadius = 10f; // 圆角半径,默认值 // 封装圆角大小的属性 [Category("Appearance")] [Description("设置无锯齿圆角半径")] public float Corners { get => CornersRadius; set { if (value > 0) { CornersRadius = value; UpdateLayout(); Invalidate(); } else { throw new ArgumentOutOfRangeException(nameof(Corners), "圆角半径必须大于0"); } } } protected override void OnPaint(PaintEventArgs pevent) { using (Graphics g = pevent.Graphics) { DrawSmoothRoundedRectangle(g, ClientRectangle, CornersRadius); } base.OnPaint(pevent); } private void DrawSmoothRoundedRectangle(Graphics g, Rectangle rect, float radius) { Pen pen = new Pen(Color.Black, 1f); SolidBrush brush = new SolidBrush BackColor); Region oldRegion = g.Clip; g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 创建一个区域,用于平滑边缘 Region region = new Region(rect); region = region.Combine(new Region(new Ellipse(rect.Location, rect.Size + new Size(radius * 4, radius * 4)))); g.Region = region; g.DrawRectangle(pen, rect); g.FillRectangle(brush, rect); g.RestoreClip(oldRegion); } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x28; // WS_EX_COMPOSITED,启用硬件加速和透明度 return cp; } } // 更新布局以反映新的圆角大小 protected override void UpdateLayout() { base.UpdateLayout(); Size newSize = ClientSize; if (CornersRadius != 0 && CornersRadius > Height / 2 || CornersRadius > Width / 2) { // 确保圆角不会超过控件本身的实际尺寸 CornersRadius = Math.Min(Math.Min(Height, Width) / 2, CornersRadius); } ClientSize = newSize; Location = new Point((newSize.Width - Width) / 2, (newSize.Height - Height) / 2); } } // 要使用此控件,只需在窗体上添加它,例如: public Form MainForm { public MainForm() { Controls.Add(new SmoothRoundedControl { Dock = DockStyle.Fill, Corners = 15 }); } } ``` 现在你就可以通过`Corners`属性来给任何继承了`SmoothRoundedControl`的控件设置无锯齿圆角。请注意,这个示例假设所有子窗体都开启了硬件加速。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值