创建 Windows 圆角矩形和圆形窗体
使用 Visual Studio 2010 设计圆角窗体。

引言
本文档将指导您如何创建具有圆角窗体。如果您想创建具有圆角的窗体,但却卡住了,不明白该怎么做,那么本文将对您有所帮助。现在,无需惊慌。这个项目将向您展示如何创建一个能够创建圆角窗体的程序。让您的窗体与众不同,并以吸引用户的方式改变应用程序的外观。
Using the Code
主要代码
首先,启动一个新项目,选择 Windows 窗体应用程序,并将其保存为“sampleprogram
”。您将看到一个名为“Form1.vb”的窗体被创建。在解决方案资源管理器中将其重命名为“frmmain
”(如果您没有找到解决方案资源管理器,请从菜单“视图->解决方案资源管理器”中选择它。或者,您可以使用快捷键,例如 ctrl+alt+L)。双击frmmain.vb以查看设计视图。现在,在继续之前,设置窗体 'frmmain
' 的以下属性:
MaximizeBox
:False
MinimizeBox
:False
StartPosition
:CenterScreen
Size
:500,500
FormBorderStyle
:None
现在,单击“查看代码”,并将以下代码写入 frmmain
的 paint
方法中。
即,编写子例程 "frmmain_Paint
"。
Public Class frmmain
Private Sub frmmain_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim gp As New System.Drawing.Drawing2D.GraphicsPath
If cirOrec = 0 Then 'If Rounded Rectangle is selected
If intval = 0 Then
intval = 200
End If
Dim chgcorn As Integer = intval 'Change the value and get the
'desired Rounded corner,
'keep in mind that the value should be divisible by 10.
' check whether the entered number is divisible by 10 or not,
' if not then make it.
If chgcorn Mod 10 <> 0 Then
chgcorn = chgcorn - (chgcorn Mod 10)
End If
Dim r1 As New Rectangle(0, Me.Height - chgcorn, chgcorn, chgcorn)
Dim r2 As New Rectangle(Me.Width - chgcorn + 1, _
Me.Height - chgcorn, chgcorn, chgcorn)
'creating the upper Arc
gp.AddArc(0, 0, chgcorn, chgcorn, 180, 90)
gp.AddArc(Me.Width - chgcorn + 1, 0, chgcorn, chgcorn, 270, 90)
'Creating the Body
gp.AddRectangle(New Rectangle(0, chgcorn / 2, Me.Width, Me.Height - chgcorn))
'Creating the lower Arc
gp.AddArc(r1, -270, 90)
gp.AddArc(r2, 360, 90)
Me.BackColor = Color.Black
Else 'If Circular form type is selected
If intval = 0 Then
intval = Me.Width
End If
If intval2 = 0 Then
intval2 = Me.Height
End If
gp.AddEllipse(New Rectangle(0, 0, intval, intval2))
Me.BackColor = Color.IndianRed
End If
Region = New Region(gp)
End Sub
'Now code for closing the Form1
Private Sub frmmain_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.close()
End Sub
End Class
...
注意:从组合框中选择您想要创建的窗体类型。对于圆角矩形窗体,输入圆角值,然后单击创建按钮。
但是,该值应小于 400,因为我已将窗体大小设置为 500 X 500。
要获取完整的代码,请下载源代码。
动机
发布本文的动机是帮助那些想要创建圆角窗体但没有得到任何帮助的人。我在互联网上搜索,但没有得到预期的结果(许多网站只显示窗体的上半部分圆角,即仅显示左上角和右上角。但是,窗体的下半部分呢?)。这个项目显示了窗体的所有四个角都是圆角的。
历史
- 2011年5月18日:初始发布
- 2011年5月19日:文章更新 - 通过这些更改,您只需要更改
chgcorn
的值,所有四个角都会相应地进行圆角处理。 - 2011年6月10日:文章更新