发布时间:2026-09-28 19:29:35 浏览次数:1
首先,我们需要了解什么是DotNetBar。DotNetBar是一个用于Windows.Forms应用程序的控件库,它提供了一系列美观、容易使用的控件和工具栏,并支持自定义皮肤、打印和报表、图像处理、XML等。它由 DevComponents 公司开发并维护。
接下来,我们将详细讲解如何使用DotNetBar制作圆角窗体和圆角控件。
this.FormBorderStyle = FormBorderStyle.None;this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));其中,CreateRoundRectRgn是一个Windows API函数,用于创建一个圆角矩形区域。在此示例中,我们设置左上角和右上角的半径为20像素。
例如,我们可以使用DotNetBar库中的PanelEx控件,将其设置为圆角,并添加其他控件作为其子控件。具体示例如下:
private void InitializeComponent(){ // ... this.panelEx1 = new DevComponents.DotNetBar.PanelEx(); this.labelX1 = new DevComponents.DotNetBar.LabelX(); this.textBoxX1 = new DevComponents.DotNetBar.Controls.TextBoxX(); this.buttonX1 = new DevComponents.DotNetBar.ButtonX(); // ... // // panelEx1 // this.panelEx1.CanvasColor = System.Drawing.SystemColors.Control; this.panelEx1.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled; this.panelEx1.Controls.Add(this.buttonX1); this.panelEx1.Controls.Add(this.textBoxX1); this.panelEx1.Controls.Add(this.labelX1); this.panelEx1.Location = new System.Drawing.Point(12, 12); this.panelEx1.Name = "panelEx1"; this.panelEx1.Size = new System.Drawing.Size(260, 237); this.panelEx1.Style.Alignment = System.Drawing.StringAlignment.Center; this.panelEx1.Style.BackColor1.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground; this.panelEx1.Style.BackColor2.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground2; this.panelEx1.Style.Border = DevComponents.DotNetBar.eBorderType.SingleLine; this.panelEx1.Style.BorderColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBorder; this.panelEx1.Style.ForeColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelText; this.panelEx1.Style.GradientAngle = 90; this.panelEx1.TabIndex = 0; this.panelEx1.CornerRadius = 20; // 设置圆角半径 // // labelX1 // this.labelX1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.labelX1.Location = new System.Drawing.Point(14, 23); this.labelX1.Name = "labelX1"; this.labelX1.Size = new System.Drawing.Size(85, 23); this.labelX1.TabIndex = 0; this.labelX1.Text = "用户名:"; // ...}在这个示例中,我们使用了PanelEx控件来包含其他控件,并设置了其CornerRadius属性为20像素,从而使其拥有圆角。
using System.Drawing;using System.Windows.Forms;namespace MyControls{ public class MyTextBox : TextBox { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; // 获取控件区域的大小和位置 Rectangle rect = new Rectangle( ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1); // 画圆角矩形 using (Pen border = new Pen(Color.Gray, 1.0f)) { g.DrawArc(border, rect.X, rect.Y, 10, 10, 180, 90); g.DrawArc(border, rect.Right - 10, rect.Y, 10, 10, 270, 90); g.DrawArc(border, rect.Right - 10, rect.Bottom - 10, 10, 10, 0, 90); g.DrawArc(border, rect.X, rect.Bottom - 10, 10, 10, 90, 90); g.DrawLine(border, rect.X + 5, rect.Y, rect.Right - 5, rect.Y); g.DrawLine(border, rect.Right, rect.Y + 5, rect.Right, rect.Bottom - 5); g.DrawLine(border, rect.Right - 5, rect.Bottom, rect.X + 5, rect.Bottom); g.DrawLine(border, rect.X, rect.Bottom - 5, rect.X, rect.Y + 5); } // 填充控件内部 using (Brush fill = new SolidBrush(Color.White)) { g.FillRectangle(fill, rect); } // 画文本 using (Brush textBrush = new SolidBrush(Color.Black)) { g.DrawString(Text, Font, textBrush, ClientRectangle.X + 10, ClientRectangle.Y + 5); } } }}在这个示例中,我们使用了一个自定义控件MyTextBox,并在其OnPaint方法中重绘了控件。我们使用了Graphics类来绘制一个圆角矩形,并填充其内部。然后我们又在矩形的左上角画了文本。
在窗体的构造函数中添加以下代码,将自定义控件添加到窗体中:
MyControls.MyTextBox myTextBox1 = new MyControls.MyTextBox();myTextBox1.Location = new Point(12, 12);myTextBox1.Size = new Size(200, 30);Controls.Add(myTextBox1);在此示例中,我们创建了一个MyTextBox实例,设置其位置和大小,并将其添加到窗体的Controls集合中。